In-depth exploration of Factory mode is different from prototype mode (continued)

zhaozj2021-02-16  48

The reason why this sequel is written, because the last article logs out, he discovered that the last time compared to the model and prototype mode, it is only limited to the use of prototype modes to imitate the functionality of the factory model, but did not point out The real difference between factory model and prototype mode. Therefore, in this way, the original intention is now, this will now continue.

In the original article, I mentioned that you can use prototype to implement Factory functionality. But in fact, the function of the factory model imitating the prototype is just "shades like the god." We don't really use prototypes to replace Factory's features.

Why do you say this? Let us first take a look at the integration of Factory mode: Factory introduced the purpose of transferring the duties of the creation to another place. For example, if you don't want the client to have the ability to create a certain type of object, you can use the factory mode to transfer the responsibilities of the creation of the class from the client to the server side (or system). The most successful application of this example is in COM, customers can only create an instance of classes through the factory method without having to create an instance of the class directly.

Ok, I understand this, let's take a look at the functionality of the factory model implemented by Prototype mode! Indeed, Prototype model does have the ability to create class objects, but this ability does not meet the purpose of plant mode use, will create the responsibilities of the object. Because we know, if you want to use Prototype to create a product, then we must be a prototype product before creating a specific product. But in fact, prototype products have been in line with our requirements. In this sense, there is no value through prototype products to clon a product. Furthermore, since the prototype product must be exposed to the customer, this also violates the principle of factory model: 即 Responsibility separation.

In summary, if we look at both from the purpose of Prototype mode and Factory mode, then these two modes are not the same thing. Although the Prototype mode seems to implement the function of the creation of the Factory mode, this similar ability is just a "similar to God."

Therefore, in the future we are using these two modes, we must clear our true purpose. This way, you can better choose the pattern to facilitate our design.

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

New Post(0)