Factory Design Pattern by Majid Ahmaditabar
just define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate

Factory Design Pattern

Majid Ahmaditabar

--

Factory pattern comes under creational pattern as this pattern provides one of the best ways to create an object.it provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.

Factory Pattern allow you to create an object without needing direct access to the creation logic.

A Factory Pattern says that just define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate

What is Factory?

The Factory Method pattern suggests that you replace direct object construction calls (using the new operator) with calls to a special Factory method. Don’t worry: the objects are still created via the new operator, but it’s being called from within the factory method. Objects returned by a factory method are often referred to as “products.”

use factory design pattern :

  • When a class doesn’t know what sub-classes will be required to create
  • When a class wants that its sub-classes specify the objects to be created.
  • When the parent classes choose the creation of objects to its sub-classes.

Problem : Different Users Types

our application has 3 type of users : managers , employees and shoppers, we implement each of them and now we want to use our classes. The following code is the implementation of 3 types of users with different arguments via factory pattern:

class Manager() {
constructor(name ,email ,access){
this.name = name
this.email = email
this.access =access
}
do_something(){
...
}
}
class Employee(){
constructor(name , email , salary , job_title){
this.name = name
this.email = email
this.salary = salary
this.job_title = job_title
}
do_something(){
...
}
}
class Shopper(){

constructor(name , email , money ){
this.name = name
this.email = email
this.money = money
}
do_something(){
...
}
}
class UserFactory {
constructor(type , name , email, access =[] , salary="" , job_title= "" , money = 0 ){
if (type =="manager"){
return new Manager(name ,email ,access)
}
if (type =="employee"){
return new Employee(name , email , salary , job_title)
}

if (type =="shopper"){
return new Shopper(name , email , money)
}
}
}

when we need user class in our code instead of declare each type separately we just need to user UserFactory class

tom_manager = new UserFactory("manager", "tom","tom@email.com",["products", "articles"])john_employee = new UserFactory("employee","john" , "john@email.com" , "5000")mari_shopper = new UserFactory("shopper", "mari" , "julia@email.com" , "1000")

Pros

Loose Coupling and Segregation of Responsibilities

Factory pattern hides the implementation details from the client.it just need to have an object created via factory method.

Factory pattern eliminate the need to bind application-specific classes into your code.

Single Responsibility Principle. You can move the user creation code into one place in the program, making the code easier to support.

Open/Closed Principle. You can introduce new types of user into the program without breaking existing client code.

Cons

Makes code more difficult to read as all of your code is behind an abstraction that may in turn hide abstractions.

The code may become more complicated since you need to introduce a lot of new subclasses to implement the pattern. The best case scenario is when you’re introducing the pattern into an existing hierarchy of creator classes.

Can be classed as an anti-pattern when it is incorrectly used, for example some people use it to wire up a whole application when using an IOC container, instead use Dependency Injection.

--

--