Start to learn 23 design models: three major factories (simple factory, factory method, abstract factory)

Source: Internet
Author: User

During development, we often use three design modes to help us solve the scalability of project code.

In simple factory, factory method, abstract factory, the code is actually very simple, mainly to understand and use.

Simple Factory:

To put it bluntly, the switch is used for instantiation Based on the passed parameters.

 

Factory method:

The factory method is used to upgrade a simple factory with swicth added each time. Provide a factory class for each product.

 

Abstract Factory:

Abstract Factory, I think it is also a re-upgrade of the factory method. The factory method can only create one product at a time, and the abstract factory is the product family of the product line.

To sum up, find a reply from the Internet:

I don't think the simple factory model is "powerless to add new products", because if a simple factory is used to produce "things", then any "things" subclass, such as cars, bicycles, ships, and shampoos can all be produced, but the pressure on simple factories is too high here. Any sub-classes of "things" can be produced, and the burden is too heavy, so there is also a kind of name for the simple factory class, called "God class". The factory method model reduces the burden on the factory class, and submits a class/class to a factory for production, while adding a class of "stuff" does not need to be modified, you only need to add a factory that produces such "things", so that the factory class complies with the open-closed principle. For the classification of "things", sometimes it is not just horizontal classification, but it can also be classified from another perspective. I don't know whether the meaning of this sentence can be understood, for example: cars can be divided into Mercedes-Benz and Audi according to the brand, or they can be divided into common Sedan Cars and SUV cars according to the category. If we use the factory method to describe them, the Mercedes-Benz factory has a way to produce Mercedes-Benz cars, the Audi car factory has a way to produce Audi Cars. However, in the case of multiple categories, this writing is not enough and does not meet the actual needs. In this case, the abstract factory model is used, that is to say, there are two methods for the Audi Car Factory: one is to produce the ordinary sedan AUDI car, and the other is to produce the suv audi car. The Mercedes-Benz factory has two methods: one is to produce an ordinary three-compartment Mercedes-Benz and the other is to produce an SUV Mercedes-Benz. The above is the application scenario of the factory method mode and the abstract factory mode, because the two are very similar, so it is not easy to distinguish the concept, you can say that, the factory method mode is an abstract factory mode in extreme situations, while the abstract factory mode can be seen as a promotion of the factory method mode. Let's talk about the abstract factory model. The Abstract Factory interface here should have two methods: one is to generate a common sedan car and the other is to produce a SUV car, the "things" produced are already limited, so you cannot produce anything other than a certain brand of cars. therefore, it can be understood that the abstract factory model cannot be used to add new "things" (In simple factory and factory methods, we can theoretically add any "things)

I feel quite right.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.