Design pattern simple factory Abstract Factory

Keywords: Design Pattern

1 simple factory mode

1.1 definitions

Simple factory mode is also called static factory mode, but it does not belong to one of the 23 design modes.

Simply put, there is a class that provides a creation method createProduct to determine which class to instantiate according to the received value, but the classes that can be instantiated by this simple factory class must be subclasses of the same abstract class

1.2 practical application scenarios

This pattern is widely used in the JDK class library, such as DateFormat. You can see that subclasses of different DateFormat classes are returned according to different passed parameters

1.3 manual implementation

In order to facilitate learning, the code is written in one file

public class SimpleFactory {

    /**
     * Provide a createProduct method to determine which class object to create according to the passed in parameters
     * @param type
     * @return
     */
    public static Product createProduct(String type) {
        if ("A".equals(type)) {
            return new ProductA();
        } else {
            return new ProductB();
        }
    }

    public static void main(String[] args) {
        Product product = SimpleFactory.createProduct("A");
        product.print();
    }

}
    /**
     * Create an abstract class and declare an abstract method
     */
    abstract class Product{
        public abstract void print();
    }

    /**
     * Create subclass A of the abstract class to implement the abstract method
     */
    class ProductA extends Product{

        @Override
        public void print() {
            System.out.println("product A Created");
        }
    }

    /**
     * Create a subclass B of the abstract class to implement the abstract method
     */
    class ProductB extends Product{

        @Override
        public void print() {
            System.out.println("product B Created");
        }
    }


1.4 advantages and disadvantages

  • Expansion: opening and closing principle

The program is open to expansion and closed to modification. The core view is that expansion is better than modification

Advantages: it realizes the separation of object creation and use. The creation is completely in the charge of a special factory class. The client programmer does not need to care about how to create the object, but only how to use it

Disadvantages: the simple factory mode is not flexible enough. Each time a new product class is added, the judgment logic of the factory class must be modified. If there are many product classes, the logic will be very complex. At the same time, modifying the factory class violates the opening and closing principle

2 factory mode

2.1 definitions

Simply put, only one factory class interface is defined, and then different sub factory classes are obtained by implementing the interface. The sub factory classes are responsible for the creation of different product classes, and these product classes still belong to different implementation classes under the same interface

2.2 comparison of simple factory model

Simple factory mode

Create a factory class and modify the factory class whenever a product class is added

Factory mode

Define a factory interface and only give the methods to be implemented by the factory class, instead of creating objects. Instead, each implementation class of the factory interface creates objects. Whenever a product class is added, only a new factory class needs to be created

2.3 practical application scenarios

The most common collection class collection is only an interface, which corresponds to a factory interface. A specific subclass, such as Arraylist, implements the collection interface, which is a factory class that implements the factory interface.

ArrayList can create instances of itr, which is the implementation class of Iterator, so the whole process is the application of a factory pattern.

The class diagram is as follows

collection is an abstract factory, the specific factory is LinkedList, ArrayList, iterator is an abstract product, and the specific products are ListItr and Itr, which are created by LinkedList and ArrayList respectively

2.4 manual implementation

In order to facilitate learning, put the code under one file

public class FactoryPattern {
    public static void main(String[] args) {
        Factory factory = new ProductAFactory();
        Product product = factory.create();
        product.print();
    }
}

/**
 * Create product class interface
 */
interface Product{
    void print();
}

/**
 * Create product subclass A and implement the interface
 */
class ProductA implements Product{

    @Override
    public void print() {
        System.out.println("product A Was created");
    }
}

/**
 * Create product subclass B and implement the interface
 */
class ProductB implements Product{

    @Override
    public void print() {
        System.out.println("product B Was created");
    }
}

/**
 * Create factory interface
 */
interface Factory{
    Product create();
}

/**
 * Create factory subclass A and produce product subclass A
 */
class ProductAFactory implements Factory{

    @Override
    public Product create() {
        return new ProductA();
    }
}

/**
 * Create factory subclass B and produce product subclass B
 */
class ProductBFactory implements Factory{

    @Override
    public Product create() {
        return new ProductB();
    }
}

2.5 advantages and disadvantages

Advantages: it solves the problem that the simple factory mode violates the opening and closing principle

Disadvantages: each sub factory class of factory mode can only create a fixed product class. Whenever a new product class is added, a new sub factory class must be added

3 Abstract Factory Pattern

3.1 definitions

To put it simply, the original one-to-one relationship between factory classes and product classes has become one to many. However, if you want to extend the product class, you still need to modify the factory interface and the code of each sub factory class, which violates the opening and closing principle

3.2 comparison factory mode

  1. Abstract factory mode solves the disadvantage of one-to-one correspondence between factory classes and product classes of factory mode. Each factory class can create other product classes, but the extensibility of abstract factory mode is as poor as that of simple factory mode, which will violate the opening and closing principle.

  2. The factory method pattern is extensible for specific products, and the abstract factory pattern is extensible for specific product families

  3. Abstract factory pattern is only an extension of factory pattern. If there is only one product class, the abstract factory pattern will degenerate into factory pattern

3.3 manual implementation

In order to facilitate learning, put the code under one file

/**
 * Abstract factory
 */
interface AbstractFactory{
    Phone createPhone(String param);
    Mask createMask(String param);
}

/**
 * Specific factories can create products A and B, that is, factory classes and product classes realize one to many
 */
class SuperFactory implements AbstractFactory{

    @Override
    public Phone createPhone(String param) {
        if ("A".equals(param)) {
            return new iphone();
        }else{
            return new huawei();
        }
    }

    @Override
    public Mask createMask(String param) {
        if ("C".equals(param)){
            return new N95();
        }else {
            return new normal();
        }
    }
}

/**
 * Abstract product A
 */
interface Phone{}

/**
 * Specific products A and B
 */
class iphone implements Phone{}
class huawei implements Phone{}

/**
 * Abstract product B
 */
interface Mask{}
/**
 * Specific products C and D
 */
class N95 implements Mask{}
class normal implements Mask{}

Posted by scottd on Sun, 31 Oct 2021 09:14:48 -0700