Design patterns explained in simple language - factory pattern

content

factory pattern

introduce

accomplish

output log


factory pattern

The Factory Pattern is one of the most commonly used design patterns in development. This type of design pattern is a creational pattern, which provides an optimal way to create objects. In the factory pattern, we do not expose the creation logic to the client when creating objects, and we use a common interface to point to the newly created objects.

introduce

Intent: Define an interface for creating an object, let its subclasses decide which factory class to instantiate, and the factory pattern delays the creation process to subclasses.
Main solution: mainly solve the problem of interface selection.
When to use: When we explicitly plan to create different instances under different conditions.
How to solve: Let its subclass implement the factory interface, and the returned product is also an abstract product.
Critical code: The creation process is performed in its subclasses.
Application example:  You need a car, which can be picked up directly from the factory, regardless of how the car is made and the specific implementation in the car.
Advantages:  1. If a caller wants to create an object, it only needs to know its name. 2. High scalability. If you want to add a product, you only need to extend a factory class. 3. The specific implementation of the shielding product is shielded, and the caller only cares about the interface of the product. |
Disadvantages: Every time a product is added, a specific class and object implementation factory need to be added, which doubles the number of classes in the system, increases the complexity of the system to a certain extent, and also increases the specific classes of the system. dependency. This is not a good thing.

Note: As a class creation pattern, you can use the factory method pattern anywhere you need to generate complex objects. One thing to note is that complex objects are suitable for using the factory pattern, while simple objects, especially those that can be created only through new, do not need to use the factory pattern. If you use the factory pattern, you need to introduce a factory class, which will increase the complexity of the system.

accomplish

We will create a  Shape  interface and   an entity class that implements the Shape interface. The next step is to define the factory class  ShapeFactory .

//创建一个接口:
public interface Shape
{
    void draw();
}
//创建实现接口的实体类。
public class Rectangle : Shape
{
    public void draw()
    {
        Debug.Log("画三角形");
    }
}
public class Circle : Shape
{
    public void draw()
    {
        Debug.Log("画圆圈");
    }
}
//创建一个工厂,生成基于给定信息的实体类的对象。
public class ShapeFactory
{
    //使用 getShape 方法获取形状类型的对象
    public Shape getShape(string shapeType)
    {
        if (shapeType == null)
        {
            return null;
        }
        if (shapeType.Equals("CIRCLE"))
        {
            return new Circle();
        }
        else if (shapeType.Equals("RECTANGLE"))
        {
            return new Rectangle();
        }
        return null;
    }
}

The FactoryPattern class uses  ShapeFactory  to obtain  Shape  objects.  It will pass information to  ShapeFactory in order to get the type of object it needs.

public class FactoryPattern : MonoBehaviour
{
    private void Start()
    {
        ShapeFactory shapeFactory = new ShapeFactory();

        //获取 Circle 的对象,并调用它的 draw 方法
        Shape shape1 = shapeFactory.getShape("CIRCLE");

        //调用 Circle 的 draw 方法
        shape1.draw();

        //获取 Rectangle 的对象,并调用它的 draw 方法
        Shape shape2 = shapeFactory.getShape("RECTANGLE");

        //调用 Rectangle 的 draw 方法
        shape2.draw();
    }

}

output log

おすすめ

転載: blog.csdn.net/qq_37310110/article/details/123247261