Minimalist design pattern factory pattern

Keywords: Laravel

Factory Pattern

sketch

There are three types of factory modes.

    1. Simple factory mode.
    2. Factory method mode.
    3. Abstract factory pattern.

Principles and ideas of design

Encapsulate the creation of objects in factory classes to decouple the creation and use of objects.

Product hierarchy and product family

Product hierarchy : 
    Inheritance structure is the same kind. For example, Huawei mobile phones, Xiaomi mobile phones and Meizu mobile phones are concrete implementation classes, and abstract classes are mobile phones.  

Product family : 
    It's a product of the same brand,They are all produced by the same factory. For example, Huawei mobile phones, Huawei tablets and Huawei computers constitute a product family of Huawei.

Simple Factory Pattern

definition

1. A factory class that returns different instances according to different parameters.
2. The method used to create instances in the simple factory pattern is static(static)Method, so the simple factory pattern is also called static factory method(Static Factory Method)pattern.

Summarize the design pattern in one sentence

A static method returns the product object.

Roles contained in structure

1. Factory(Factory role)
2. Product(Abstract product role)
3. ConcreteProduct(Specific product roles)

Minimum expressible code

// product
abstract class Product {}
class ConcreteProductA extends Product {}
class ConcreteProductOther extends Product {}

// Simple factory
class SimpleFactory {
    public static getProduct($param) : Product
    {  
        if ("A" == $param) {
            return new ConcreteProductA();  
        }  

        return new ConcreteProductOther();
    }  
}

advantage

  1. The product can be created according to the parameters without knowing the specific implementation of the product.
  2. Separate the creation and use of objects.

shortcoming

  1. With the passage of time, the specific categories of products gradually increase, and the logic of the factory will become more and more complex, which is not conducive to the expansion and maintenance of the system.
  2. To add a product grade structure, you need to add the corresponding factory class. To add a product family, you must also modify the factory class.

When to use

  1. Different objects are generated according to different conditions, and fewer objects are created, such as drivers configured in the framework.
  2. The creation of an object is complex.

Practical application scenario

  1. Database driver in PDO. Mysql, DB2, or Oracle.

Factory method pattern - Factory Method Pattern

definition

  1. An interface for creating objects, allowing subclasses to decide which class to instantiate (a factory instantiates a product class).
  2. Factory method pattern is also referred to as factory pattern, which can also be called virtual constructor pattern or polymorphic factory pattern.

Summarize the design pattern in one sentence

Different factories return different product objects.

Roles contained in structure

  1. Product (abstract product)
  2. ConcreteProduct (specific product)
  3. Factory (Abstract Factory)
  4. ConcreteFactory (specific factory)

Minimum expressible code

// product
interface Product {}
class ConcreteProductA implements Product {}
class ConcreteProductOther implements Product {}

// factory
interface Factory {
    public function createProduct() : Product;
}
class ConcreteFactoryA implements Factory {  
    public public createProduct() : Product
    {  
        return new ConcreteProductA();
    }     
}
class ConcreteFactoryOther implements Factory {  
    public public createProduct() : Product
    {  
        return new ConcreteProductOther();
    }     
}

advantage

  1. The client only needs to know the factory corresponding to the product, and does not need to know the class name of the specific product.
  2. For new products, you only need to add a specific factory and specific products, and the scalability is very good.

shortcoming

  1. New products will lead to a large number of factory classes in the system.
  2. Abstraction is used, and reflection or IOC may be used during implementation, which increases the complexity of the system.

When to use

  1. The factory method pattern is actually a collection of several simple factory patterns. Therefore, when there are too many products in a simple factory, the factory method mode can be used for decoupling.
  2. The runtime dynamically determines the required classes. The class name of a specific factory class can be stored in the configuration file and instantiated by reflection at run time. Although a simple factory can do it, it violates the opening and closing principle.

Practical application scenario

  1. The commodities in the mall include selected commodities, collected commodities and the commodities with the highest sales. Each kind of commodity is different. The factory method can be used for pattern design.
  2. In the framework, instantiate classes through configuration. For example, in laravl, select the cache (redis or memcached) driver through config/cache configuration.

Abstract Factory Pattern

definition

Provide an interface to create a series of related or interdependent objects (each factory is responsible for creating the whole product family), without specifying their specific classes.

Summarize the design pattern in one sentence

A factory has different methods, and different methods return different product objects. Simple is that a factory returns a series of products.

Roles contained in structure

  1. AbstractFactory (Abstract Factory)
  2. ConcreteFactory (specific factory)
  3. AbstractProduct (abstract product)
  4. ConcreteProduct (specific product)

Minimum expressible code

// Mobile phone products
interface Phone {}  
class HuaWeiPhone implements Phone {}   
class XiaoMiPhone implements Phone {}  

// Notebook products
interface NoteBook {}  
class HuaWeiNoteBook implements NoteBook {}   
class XiaoMiNoteBook implements NoteBook {}  

// factory
interface Factory {  
    public function createPhone() : Phone;  
    public function createNoteBook() : NoteBook; 
}  
// Huawei factory
class HuaWeiFactory implements Factory {  
    public function createPhone() {  
        return new HuaWeiPhone();  
    }  
    public function createNoteBook() {  
        return new HuaWeiNoteBook();  
    }
} 
// Millet factory
class XiaoMiFactory implements Factory {  
    public function createPhone()
    {  
        return new XiaoMiPhone();  
    }  
    public function createNoteBook() {  
        return new XiaoMiNoteBook();  
    }  
}

advantage

  1. Adding a new product family (adding specific factories and products) does not need to modify the existing system and conforms to the opening and closing principle.

shortcoming

  1. Adding a new product hierarchy (all specific factories need to be modified, and adding Abstract products and specific products) requires major modifications to the original system.

When to use

  1. The abstract factory pattern is actually a collection of multiple factory method patterns. Therefore, when there are multiple product hierarchies in the factory method pattern, the abstract factory pattern can be used.
  2. Products of the same product family are packaged and used together.

Practical application scenario

  1. Products of the same product family. qq skin, window and menu all belong to the same theme.
  2. Programs for different operating systems. Both buttons and text boxes belong to an operating system.

Posted by Ruiser on Thu, 11 Nov 2021 11:15:03 -0800