Design Patterns In WinJS #2: Abstract Factory

As you well know from my latest blog post "Design Patterns in WinJS #1: Introduction to Series", I've decided to write more OOP articles in WinJS library regarding the use of Design Patterns we all love!

Let's start with what this design pattern means. 

What is Abstact Factory Design Pattern?

It creates objects bound by the same purpose, theme, and responsibility. By Factory, it's actually an object that creates other objects.

Let us make an example. Assume that we have 2 factories that construct the same type of furniture but with different colors.(Red and Black)The items produced there are the same but different colors. This would be a nice example of Abstract Factory.

As you well know, we would be calling these factories  Abstract Factory.

Let's make another example:

Assume that we have Developers in our company, team or group. Every individual has a different set of skills, experience and knowledge. If we could name Developers as Products of Factory, the group they are in would be Abstract Factory.

Here's an example that explains:


The objects created from Developer are named as Abstract Developer. It's mainly an interface in modern OOP languages.
  • Developer is a class.
  • ConcreteFactory is another class derived from Developer class.
  • DeveloperFactory is another interface. It has mainly the functions for creating Developer objects.
So, since Interfaces are in JavaScript OOP world, our job is to use ConcreteFactory and Developer objects.
Let me make an example in WinJS to show you how they are implemented.

Before start coding JavaScript, I'd advice you to create another JavaScript file and link it. 


Let's create our Developer objects in JS using WinJS library:
  1. var Developer = WinJS.Class.define(  
  2.     function (name) {  
  3. = name;  
  4.         this.Shout = function () {  
  5.             Helper.Func.add("I'm an employee named " + name);  
  6.         }  
  7.     });  
This is a simple class, that has a function named "Shout" which sends message to our Helper class (which I will introduce later)

Now,lets define our ConcreteFactory:
  1. var DeveloperFactory = WinJS.Class.derive(Developer,  
  2.     function () {  
  3.         this.Wave = function (name) {  
  4.             return new Developer(name);  
  5.         }  
  6. });  
Be careful about notation, I named it as its ConcreteFactory. As its not AbstractFactory, but the Design Pattern is named Abstract Factory. It may be confusing at first but in JS Abstract Factory is 2 objects pattern. We derived Developer classes from Factory here.

Before creating our Helper Class, let's create a namespace for it:
  1. WinJS.Namespace.define("Helper");  
Fill in the functionality of this Helper namespace with 2 functions that gets and sets items:
  1. var log = "";  
  3. Helper.Func =   
  4. {  
  5.     add: function (msg) { log += msg + "\n"; },  
  6.     show: function () { console.log(log); log = ""; }  
  7. };  
I've decided to show the output on JavaScript Console.But its up to you.

Next we'll be creating a new Factory object and call the Shout function which will write some output for a developer individual.
  1. var devfactory = new DeveloperFactory();  
  3. devfactory.Wave("Ibrahim Ersoy").Shout();  
We've created a new developer object named "Ibrahim Ersoy" and as a last step,we'll be calling our show function:
After you build & run the project,you'll be getting the output like this:


You can create as much Product and Factory object as you like. It's all up to your software design

If I wanted to extend it, I'd add Companies and Company Factory:
  1. var Company = WinJS.Class.define(function (name) {  
  2. = name;  
  3.     this.Shout = function() {  
  4.         Helper.Func.add("This is " + name);  
  5.     }  
  6. });  
  9. var CompanyFactory = WinJS.Class.derive(Company,  
  10.     function () {  
  11.         this.Wave = function (name) {  
  12.             return new Company(name);  
  13.         }  
  14. });  
To access and create new objects from it I'd initialize it:
  1. var compfactory = new CompanyFactory();  
  3. compfactory.Wave("Microsoft").Shout();  
  4. compfactory.Wave("MCN").Shout();  
  5. compfactory.Wave("Google").Shout();   
And here's the result after build & run:


Why should you use it?
Abstract Factory is useful to implement in project "if you have the same kind of products but different kinds of objects"  and with ConcreteFactory we can create many products with it. 

Araf Global is a software consultancy company founded in 2016 focusing on cutting edge technologies.