Design mode discussion Abstract Factory articles

zhaozj2021-02-11  216

Design mode discussion Abstract Factory articles

Some people have said, "Do not write 1M line code, can not watch design mode." But it seems that the design model is not so magical, and the following uses the author's thoughts, discuss the main part of the Abstract Factory design mode.

First, why should design mode? The GOF design pattern is mainly used to reduce coupling and improvement. The main principle of object-oriented is to private, do not protected; you can protected, you don't want public. Try not to leak some important details to your customers, so as not to unnecessary errors. Of course, this is from inheritance; and from the polymorphism, it is the interface of the base class of the client class, and the interface referred to here is the public portion of the base class. The customer class knows the type of specific subclass of abstract base classes, which is more coupled. Only the interface of the abstract base class is provided to the outside, improve the abstract level, which is versatility, which is multiplexed.

The design pattern is advocated: "Priority use combination, not inheritance." However, in the Abstract Factory mode, no combination is used, but only dependent (instantiation), that is, the inside of the Abstract Factory mode is still unwavered.

The main benefit of Abstract Factory is that the customer code not only does not know the specific type of factory class, and does not know the specific product type, the specific product type information is encapsulated into the specific factory. Therefore, the customer class only manipulates the factory interface and product interface. I don't know the details of the realization of the specific factories. The complexity created is packaged.

The main goal of the design model is to "turn the changed and unchanged separation, improve elasticity". However, in Abstract Factory, flexibility is limited. That is, you can derive different specific factories from abstract base classes to achieve different specific products, but all of this is limited by your interface. If the product changes very large, your interface has changed, then customer class The code also has to change. That is, the reduction of coupling failed.

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

New Post(0)