Design Patterns - simple factory and abstract factory

1, Simple factory mode

1. Introduction to simple factory mode
Definition: the simple factory pattern belongs to the creation pattern, also known as the static factory method pattern. It is an instance of a product class created by a factory object.

  • Factory: factory class, the core of simple factory pattern, is responsible for implementing the internal logic of creating all instances. The method of creating product class of factory class can be directly called by the outside world to create the required product object.
  • IProduct: abstract product class, the parent class of all objects created by the simple factory pattern, which is responsible for describing the common interface shared by all instances.
  • Product: specific product class is the creation target of simple factory pattern

2. Simple factory mode
Let's take the production of computers as an example. Suppose there is a OEM manufacturer of computers that can produce Lenovo computers. With the expansion of business, this OEM manufacturer will also produce HP and ASUS computers, so we need to use a separate class to produce computers, which uses the simple factory model. Let's implement the simple factory mode:

  • Abstract product class:
public abstract class Computer {
    /**
     * Abstract methods of products, implemented by specific product classes
     */
    public abstract void start();
}
  • Specific product categories:
public class LenovoComputer extends Computer{
    @Override
    public void start() {
        System.out.println("Lenovo computer starts");
    }


public class HpComputer extends Computer{
    @Override
    public void start() {
        System.out.println("HP computer startup");
    }
}


public class AsusComputer extends Computer {
    @Override
    public void start() {
        System.out.println("ASUS computer starts");
    }
}

3. Scenarios and advantages and disadvantages of using simple factory mode

  • Usage scenarios
    The factory class is responsible for creating fewer objects. The customer only knows the parameters passed in to the factory class and doesn't care about how to create the object (logic).
  • Advantages and disadvantages of simple factory model
    Advantages: it enables users to obtain corresponding class instances according to parameters, avoids directly instantiating classes, and reduces coupling.
    Disadvantage: the type that can be instantiated has been determined during compilation. If you add a new type, you need to modify the factory, which violates the open closure principle (ASD). A simple factory needs to know all types to be generated. It is not suitable to use when there are too many subclasses or too many subclass levels.

2, Abstract factory pattern

1. Abstract factory pattern definition
Provides an interface for creating a set of related or interdependent objects without specifying their concrete classes.

  • AbstractFactory: an abstract factory that declares the methods used to create different products.
  • ConcreteFactory: a concrete factory that implements the method of creating products defined in the abstract factory.
  • AbstractProduct: an abstract product that declares business methods for each product. For example, AbstractProductA and AbstractProductB in the figure above.
  • Concrete product: concrete product, which defines specific products produced by specific factories and implements business methods defined in abstract products.

2. Simple implementation of abstract factory pattern
Lenovo's and HP's computers are divided into two product lines: a desktop computer and a notebook. In order to solve the problem of adding product lines, we use abstract factory pattern to implement.

  • Abstract product class
public abstract class DesktopComputer {
  public abstract void start();
}
public abstract class NotebookComputer {
   public abstract void start();
}
  • Specific products
public class LenovoDesktopComputer extends DesktopComputer {
    @Override
    public void start() {
        System.out.println("Lenovo desktop starts");
    }
}

public class HpDesktopComputer extends DesktopComputer {
    @Override
    public void start() {
        System.out.println("HP desktop startup");
    }
}

public class LenovoNotebookComputer extends NotebookComputer {
    @Override
    public void start() {
        System.out.println("Lenovo laptop starts");
    }
}
public class HpNotebookComputer extends NotebookComputer {
    @Override
    public void start() {
        System.out.println("HP laptop startup");
    }
}

  • Abstract factory class
public abstract class ComputerFactory {
    public abstract DesktopComputer createDesktopComputer();
    public abstract NotebookComputer createNotebookComputer();
}
  • Specific factory
public class LenovoFactory extends ComputerFactory {
    @Override
    public DesktopComputer createDesktopComputer() {
        return new LenovoDesktopComputer();
    }
    @Override
    public NotebookComputer createNotebookComputer() {
        return new LenovoNotebookComputer();
    }
}

public class HpFactory extends ComputerFactory {
    @Override
    public DesktopComputer createDesktopComputer() {
        return new HpDesktopComputer();
    }

    @Override
    public NotebookComputer createNotebookComputer() {
        return new HpNotebookComputer();
    }
}

Client call

public class Client {
    public static void main(String[]args) {
        ComputerFactory lenocoFactory=new LenovoFactory();
        lenocoFactory.createDesktopComputer().start();
        lenocoFactory.createNotebookComputer().start();
        ComputerFactory hpFactory=new HpFactory();
        hpFactory.createDesktopComputer().start();
        hpFactory.createNotebookComputer().start();
    }
}

Next, the UML diagram of this example is given, which is easier to understand, as shown below.

3. Advantages and disadvantages of abstract factory mode
Advantages: the instance creation process of specific classes is separated from the client. The client manipulates the instance through the abstract interface of the factory. The client does not know who the specific implementation is.
Disadvantage: if you add a new product family, you also need to modify the abstract factory and all the concrete factories.

4. Use scenario of abstract factory mode

  • A system does not depend on the details of how product line instances are created, combined, and expressed.
  • There are more than one product line in the system, and only one product line is used at a time.
  • A product line (or a set of unrelated objects) has the same constraints.

Tags: Programming

Posted on Fri, 10 Jan 2020 01:03:20 -0800 by alevsky