Design Model ------ three sisters in the factory

Source: Internet
Author: User

After learning the design patterns, I learned them one by one based on the order in the big talk book. Three of them contain the words "Factory" and both belong to the creation model, they are "three sisters in the factory". Let's take a look at the three sisters through the example of a calculator.

Simple factory mode:


Code in the simple factory class:

We can see that when a user requests an addition operation, only the Z token is required? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcGVyYXRpb25GYWN0b3J5LmNyZWF0ZU9wZXJhdGUo" + "), the factory will automatically give an addition class instance, users do not have to deal with specific computing classes at all, and the object creation process is encapsulated. However, if we want to add a power operation, we should not only add a subclass of the power operation under the operation class to expand, but also change the factory class and add a new case, in this way, the modification is also made available. The simple factory model is not a real design model because it violates the open and closed principle. The simple factory mode is a good choice when the factory class is responsible for creating a small number of objects without increasing or decreasing the number of objects, and the customer only knows the parameters passed into the factory class.

Factory method mode:


Compared with the simple factory model, we can clearly see that there are more factory sub-classes for specific operations under the factory class. The task of the factory class interface is not so heavy. There is only one method to create an abstract product:


All factories that produce specific products must implement this interface. The client code becomes like this:

IFactory operFactory = new AddFactory (); Operation = operFactory. CreateOperation ();

In this way, let the subclass decide to instantiate a specific class, the division of labor is more detailed, and the responsibility is to people. At this time, we only need to expand the power operation under the factory class and operation class, instead of modifying the Code, to make up for the shortcomings of the simple factory mode.

When we need to produce a series of products, for example, there are two levels of calculators, one is our common arithmetic calculator, and the other is a scientific calculator, which can calculate, subtract, multiply, divide, the factory method is only a calculator. At this time, we need to use the abstract factory model. It can be said that there are mountains outside the mountains, and there are people outside of the people.

Abstract Factory mode:


(Please forgive me for your own examples)

In this way, the customer can change the product series and want the arithmetic calculator, ifacloud = new ArithmeticFactory (), and scientific type, ifacloud = new ScienceFactory (). The difference between the factory method mode and the abstract factory mode is actually two sentences: the former has only one abstract product class, while the latter has multiple. The former can only create one instance of a specific product type, while the abstract factory mode can create multiple instances.

Looking at these three models, the waves go forward. But do not think that the abstract factory mode is perfect. What if we want to add a multiplication operation? There are not only extensions but also changes. The three sisters should be flexible in learning. They can help each other and let the simple factory model improve the abstract factory model. In fact, reflection technology can be considered to remove switch or if in all the places where simple factories are used to remove the coupling caused by Branch judgment. There are always more difficult methods.

This is where the sisters and sisters are introduced. Today, they are familiar with each other, and there will be a lot of days to deal with them.




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.