Tuesday, May 31, 2022

Design Pattern: Factory Pattern

Chapters

Factory Pattern

Factory Pattern is a design pattern that provides a platform for creating objects in superclass, but sub-classes determine the structure of the created objects. This pattern consists of factory class, factory method, object to be created and classes that extends/implements factory class.

Factory class is a class that contains factory method. Factory method is a method that is overriden by factory subclasses of factory super class. Then overriding methods generate an object that we wanna create such as buttons, etc. This example demonstrates implementation of Factory pattern in java.
import buttonfactory;

public class ClientCode{

  public static void main(String[] args){
    
    ButtonFactory factory = 
    ButtonFactory.
    selectButtonFactory("Undecorated");
    Button createdButton = null;
    
    if(factory != null){
      createdButton = 
      factory.createButton("MyButton");
      System.out.println
      ("Created Button: " + 
       createdButton.getName());
    }
  }
}

/*
Assume classes below are in buttonfactory package
*/

public interface ButtonFactory{
  
  Button createButton(String name);
  
  static ButtonFactory selectButtonFactory(String name){
    ButtonFactory factory = null;
    switch(name){
    
      case "Standard":
      factory = 
      new CreateStandardButton();
      break;
      
      case "Undecorated":
      factory = 
      new CreateUndecoratedButton();
      break;
    }
    return factory;
  }
}

class CreateStandardButton implements ButtonFactory{
  
  CreateStandardButton(){}
  
  @Override
  public Button createButton(String name){
    return new StandardButton(name);
  }
}

class CreateUndecoratedButton implements ButtonFactory{
  
  CreateUndecoratedButton(){}
  
  @Override
  public Button createButton(String name){
    return new UndecoratedButton(name);
  }
}

public abstract class Button{
  private String name;
  
  public String getName(){
    return name;
  }
  
  Button(String name){
    this.name = name;
  }
  
}

class StandardButton extends Button{
  StandardButton(String name){
    super(name);
    System.out.println(getName() + 
    " standard button has been created!");
  }
}

class UndecoratedButton extends Button{
  UndecoratedButton(String name){
    super(name);
    System.out.println(getName() + 
    " undecorated button has been created!");
  }
}

Result
MyButton undecorated button has been created!
Created Button: MyButton
Typically, factory superclass and objects superclass should be non-instantiable. In short, they can't be instantiated.

One of the advantages of this pattern is that we can add objects, such as new button type, and another factory class subclass without affecting other button types and subclasses of factory class. We can also modify factory sub class and object subclass with little to no effect on other subclasses.

One of the disadvantages of this pattern is that adding more objects or subclasses of factory class can make this pattern complicated real quick.

For more information, you may visit this website.

No comments:

Post a Comment