Abstract Factory mode

xiaoxiao2021-03-19  194

Create a series of related or interdependent objects (product family) interface without having to specify their specific classes

1. Need to use a series of products in a lot of product lines, you cannot use several series products, and this mode can be used to force this.

2. The user only uses the abstract factory and the interface provided by the abstract product, the user does not know the name of the specific product (class name), which does not appear in the client code. The user is going to switch to another product family, just switch to another specific factory class.

3. Generally, the factory is implemented as Singleton

4. Difference to Factory Method: In the factory method, a specific factory is only responsible for creating a type of product, and a specific factory in the abstract factory is responsible for a series of products (a product family). Increasing a new product family should have to add a specific factory to create them.

5. It is difficult to support new types of products. If an abstract product is added, it is also an additional product in each product family. In this case, you need to modify the interface of the abstract factory (add an interface, such as CREATATINERPRODUCT, and then implement the interface in all subclats. . Does not satisfy the opening and closing principle. One solution is to let the abstract factory only provide an interface: Product * AbstractFactory :: Creat (String ProductType), indicating what product to create, such as Button, TextField, etc. But doing so, forced type conversion for the result of returning, the product * is converted down to the type of ProductType.

6. It can be convenient to add a new product family: Increase a new product family to create a specific factory to create them. But adding a new type of product is comparison. That is, the above 4th, 5 points.

转载请注明原文地址:https://www.9cbs.com/read-130141.html

New Post(0)