JavaScript – Abstract Factory

GitHub Code


The Abstract Factory pattern is a more complex variation of the Factory pattern (previous post – https://erichelin.wordpress.com/2017/04/27/javascript-factory-pattern).   Instead of just having one factory handle one object, this pattern makes use of one factory that has multiple output streams.  Put another way, there is still an abstract factory that makes objects based on an abstract object.  But instead of just making one, it makes multiple variations of the same product.  Reference #1 calls them a ‘family of objects’.

First, run the project, make a get to the correct endpoint and view the output.

Screen Shot 2017-07-08 at 11.56.06 AM

The actors are (per reference #1):

  • Client – This object is the ‘glue’ that ties it all together.  The client has a reference to each type of the related objects and the output is added to it from the factory that is injected into the constructor at run time.  My implementation of the client is a combination ‘Client.js’ and ‘initializeClient()’ in ‘abstractFactoryDriver.js’.  Instead of using the constructor from the example in reference #1, I chose to add a ‘setFactory(arg)’ method to inject the factory.  One complication was that I got a run time error that I wasn’t expecting.  In reference #1’s example, each object has the output from the supplied factory.  Because the factory was not set until run time, I opted to leave the object references as undefined and set them at run time as well.

Screen Shot 2017-07-08 at 11.59.38 AM

Screen Shot 2017-07-08 at 11.59.12 AM

  • Abstract/Concrete Factory – Similar to the factory pattern, this factory has a method that returns a specific object.  The only difference is that there is more than one.  My implementation is a combination of ‘AbsFactory.js’ and the ‘initializeFactory()’ method in ‘abstractFactoryDriver.js’.  In ‘initializedFactory()’, I use the Object.create(arg) to create the concrete factory.  Something to note is that I took a slightly different approach than reference #1’s example where I only created one factory instead of three for each object.  But with all things, there is always more than one way to do the same thing 😉

Screen Shot 2017-07-08 at 11.59.34 AM

Screen Shot 2017-07-08 at 11.59.12 AM

  • Abstract/Concrete Objects – Similar to the factory pattern, the object being created is based on an abstract object.  The only difference is that there is more than one.  Another difference I took was to use one abstract object to make my concrete objects.  I suppose how you do this this depends on the objects being created.  Since mine only differed by a couple of properties, I chose simplicity 😉  My implementation is a combination of ‘AbsObjectX.js’ and the ‘initializeFactory()’ method in ‘abstractFactoryDriver.js’.  In ‘initializedFactory()’, I use the Object.create(arg) to create the concrete objects.

Screen Shot 2017-07-08 at 11.59.29 AM

Screen Shot 2017-07-08 at 11.59.12 AM

Stay tuned!

References

  1. http://www.blackwasp.co.uk/AbstractFactory.aspx
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s