Design Pattern Factory Three Sisters

Source: Internet
Author: User

Recently for the design model of the factory three sisters-elder sister (abstract Factory), second sister (Factory mode), three elder sister (simple Factory), further study, the following are some of my understanding.

First, Simple factory

Using a separate class will create an instance of the process independent, this is the simple factory class, such as: simple operation of the factory class, as for the use of the process, if you increase the operation of operations, you need to modify the operation of the factory class, which violates the

Open-close principle (make the code as open as possible, change the closure, that is, the changes to the program by adding code, rather than changing the existing code).

Second, the factory model


The factory subclass is added to the simple factory relative to the above, so that an instance of a class is deferred to the subclass, so that when the operation is added, it is only necessary to increase the corresponding operation factory, which solves the disadvantage that the simple factory does not conform to the open-closed principle.

Simple Factory vs Factory method

The factory approach solves the hassle of simple factory plus case judgment, but it is also complicated to add the relevant factory to the code and then modify the client for each additional operation.

In fact, we can use simple Factory + reflection, which is more simple than the factory method alone.

Third, abstract factory


Abstract factory easy to exchange product family (an application only needs to occur once during the initialization process, so it is easy to change the application's specific factory), and the creation of the instance process is separated from the client, and the specific class name of the product is separated by the specific factory implementation.

But if you add functionality, the abstract factory needs to modify a lot of places, and here is a simple factory instead of all the specific factories.

The problem comes out again, using a simple factory will encounter case to determine the branch of trouble, but it doesn't matter, the previous mention of the use of reflection is easy to solve.


For the factory three sisters at present just understand these, believe will in the future programming on their understanding more profound, look forward to meet again.

Design Pattern Factory Three Sisters

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.