"Design Mode Design Pattern" reading notes

xiaoxiao2021-03-06  67

"Design Model Design Pattern" reading notes II Abstract Factory mode

Alias

Kit

Structural advantages

1. By providing only the interface to the client, the Client is separated from the interface, so that the client does not need to consider the specific implementation of the interface.

2, make the replacement product series (Product Families) is more convenient, just replacing a variable of a ConcreteFactory.

3. Through the concept of Factory, it guarantees the consistency of each product (Product) in a family. That is to say, a series of products can only be produced and used in its series, and other series cannot be produced and used.

Disadvantages 1, new additive Product is not easy.

A) Typically, for each class of Product, AbstractFactory needs to create it by assigning an operation (Operation); if the new class of Product, AbstractFactory needs to add an operation (Operation), which is changed, so that the interface has changed, Just affect all classes related to AbstractFactory (need to recompile).

B) a solution is that all Product public uses an operation (such as: make) to create them. By incurring a parameter to determine what type of Product (however, add a category of a parameter does not affect all classes related to AbstractFactory? However, there is a simple technical means to solve.)

Prerequisites: This solution requires each product to derive the same base class (as a return type); or return type to void *, and then the client can securely enforce the type of transformation (COERCE). Even if each product is derived from a base class, there is still a inheritance problem, when the client gets the base class pointer, it is necessary to use a member of the derived class, it is necessary to use Downcast (ie, Dynamic-Cast in C ), although The security of the type can be guaranteed, but Downcast has the possibility of failure, so there is still a problem with security.

C) It can be seen that it is necessary to make a hierarchy between flexibility and security.

2, the book is not talked about, AbstractFactory provides operational interfaces for the production of various ProductFactory, need to ensure that all CONCRETEFACTORY requirements can be guaranteed. For example, the number of CreateProducta () and the type of CRETEFACTORYA is satisfied, but if you don't satisfy ConcreteFactoryb, you can't do it.

Extended 1, AbstractFactory is not designed to be a pure virtual class, you can act at the same time as the functionality of General ConcreteFactory.

2. A program usually only need a ConcreteFactory object, at this time, you can use the Singleton mode.

3. As shown above, in general, it is to use the Factory Method mode to produce Product, the advantage is relatively simple. However, there is no bigity of each series, if the difference between the series is not very large, there will be a lot of waste; at this time, you can use the prototype mode to be solved. For each series, you need to derive a problem with a ConcreteFactory class. Reference: "Design Pattern" Gang-Of-4 1997

Happy Mid-Autumn Festival!

Jady Leung

September 28, 2004

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

New Post(0)