coffee-factory0.1.3 • Public • Published
npm install --save coffee-factory
new Factory(klass, maxReuse, ctorArgs...) → Factory Instance
klassThe class to instantiate when
maxReuseThe maximum number of existing instance to keep at any one time. FIFO.
ctorArgsArguments passed to constructor when new instantiations created
initArgsArguments passed to
.initialize()of instance when
objAn instance to give the Factory for re-use
Factory = require 'coffee-factory'name: "default": ->@name = ""UserFactory = User10 # Create the factory# Get an instance.# No instances are stored in the factory, so a new one is createduser = UserFactorygetconsolelog username # Output: "" - because `.initialize()` is calleduser.name = "Foo"# We no longer want this instance, so let the factory reuse itUserFactoryput useruser = null# Normally, garbage collection would delete the object form memory# Get another instance. There is one stored, so that will be used.# Before returning, `.initialize` (if it exists) will be called on the instanceuser2 = UserFactorygetconsolelog username # Output: ""
Passing params to
Factory = require 'coffee-factory'name: "default":UserFactory = User10 # Create the factoryuser = UserFactoryget "Foo"consolelog username # Output: "Foo" - the param was passed to `.initialize()`
Passing params to the constructor
Factory = require 'coffee-factory'name: "default":# The "Foo" parameter will be passed to the constructor of User when `.get()` is calledUserFactory = User10"Foo"user = UserFactorygetconsolelog username # Output: "Foo"user2 = UserFactorygetconsolelog user2name # Output: "Foo"
Usage as a Default Constructor system
Factory = require 'coffee-factory'isStaff: false:# The "Foo" parameter will be passed to the constructor of User when `.get()` is calledStaffUser = User10trueceo = StaffUsergetconsolelog ceoisStaff # Output: true
Like what I've created? *So do I!* I develop this project in my spare time, free for the community. Donations
If you'd like to say thanks, buy me a beer by tipping with Dogecoin: DJLZccHAcz19ikSV1D5jY3WdFPU7Nqjmfk