I've decided to write more OOP articles in WinJS library regarding the use of Design Patterns we all love! You can learn my previous parts:
Let's start with what this design pattern means.
What is Factory Method Design Pattern?
Factory Method helps you create new objects by knowing the type of the object.The user may not know the details of each object's type and its details but Factory helps the user to select a type and instantiate.
According to DoFactory,Factory Method is:
Creator is our FactoryMethod here which creates objects with types defined.
AbstractProduct is an Interface and Interfaces arent supported.so its out of the picture.
Products are the objects created by FactoryMethod.
Factory Methods are frequently used by many Javascript scripts.The power of the Factory Method comes from its extensibility. We shall see it an example soon but before getting ahead of coding,I'd like to give a real world example for Factory Methods.
Think of a company.Think of its employees.A company and its employees are a fine example to Factory Method.
Why?
Every Individual(object created from Company class) is an object created with its type.Whenever a job needs to be assigned or you simply wanna chat,you get to that person or mail to the person.The person here has a type and he does a specific job.
The Company here is our Factory Method.This pattern will create Employees with its types of job they do and assign jobs to complete.
Implementation
Our example is sound and clear.We shall create our Company first:
- var Company = WinJS.Class.define(
- function() {
- this.createEmployee = function (type) {
- var employee;
-
- if (type === "Developer") {
- employee = new Developer();
- } else if (type === "ProjectManager") {
- employee = new ProjectManager();
- } else if (type === "Designer") {
- employee = new Designer();
- } else if (type === "Analyst") {
- employee = new Analyst();
- }
-
- employee.type = type;
-
- employee.say = function () {
- Helper.Func.add(this.type + " earns " + this.hourly + " per hour");
- }
-
- return employee;
- }
- });
According to the above code,we define jobs to complete according to the types created once createEmployee function is called.
Lets add our Employees now:
- var Developer = WinJS.Class.define(
- function () {
- this.hourly = "$20";
- });
-
-
- var ProjectManager = WinJS.Class.define(
- function () {
- this.hourly = "$25";
- });
-
- var Designer = WinJS.Class.define(
- function () {
- this.hourly = "$20";
- });
-
- var Analyst = WinJS.Class.define(
- function () {
- this.hourly = "$15";
- });
Here are the employees that completes specific jobs,you can extend it according to your needs.
These employee types have property name "hourly" which defines their hourly payment.
Now lets create our Helper class.Our helper class will never change in these series:
- WinJS.Namespace.define("Helper");
- var log = "";
-
- Helper.Func =
- {
- add: function (msg) { log += msg + "\n"; },
- show: function () { console.log(log); log = ""; }
- };
Our classes and Helper functions are defined. Now,its time to call them.
First we create our Company object:
- var company = new Company();
Then create our Employees,define their types as parameter and send their message to Helper Functions:
- company.createEmployee("Developer").say();
- company.createEmployee("ProjectManager").say();
- company.createEmployee("Designer").say();
- company.createEmployee("Analyst").say();
And last we show the message:
Finalize
Lets build&run the project,and see the output:
Each employee has a different type and the jobs they do can vary if you choose them to.You can set additional jobs for specific position. It's all a good show of extensibility of Factory Method. Use it well.
Why should you use it?
It's popular! It's used by many scripts and websites.Defines types and does specific jobs. Moreover it's extensible.You can extend the types and the jobs they need to complete according to your needs.