Design Mode (4): simple factory mode-Creation Mode

Source: Internet
Author: User
The simple factory mode is also called the static factory method mode. After renaming, we can see that this mode must be very simple. Its purpose is to define an interface for creating objects.

  

2. Applicable scenarios

If a customer wants a BMW, the general practice is to create a BMW and use it. Then there was an industrial revolution. You do not need to create a BMW. Because the customer has a factory to help him create a BMW. He wants a car, this factory can be built. For example, you want a 320i series car. The factory creates this series of vehicles. That is, the factory can create products.

  

3. Comment

Advantages:

The factory class is the key to the entire model. it contains the necessary logical judgment to determine the specific class object to be created based on the information given by the outside world. by using the factory class, the outside world can get rid of the embarrassing situation of directly creating specific product objects, and only need to be responsible for "consuming" objects. Without having to worry about how these objects are created and organized, they clearly define their respective responsibilities and rights, which is conducive to the optimization of the entire software architecture.

Disadvantages:

Because the factory class integrates the creation logic of all instances, in violation of the High Cohesion responsibility allocation principle, all creation logic is centralized into a factory class; the class it can create can only be considered in advance, if you need to add a new class, you need to change the factory class.

When the number of product categories in the system increases, the requirements for the factory category to create different instances based on different conditions may arise. this kind of judgment on the condition is intertwined with the judgment on the specific product type, and it is difficult to avoid the spread of module functions, which is very unfavorable for system maintenance and expansion;

These shortcomings have been overcome in the factory method model.

 

4. Mode Structure

1. Factory roles: this is the core of this model and contains some commercial logic and judgment logic. 2. Abstract Product role: it is generally the parent class or implemented interface inherited by a specific product. 3. Specific product role: the object created by the factory class is the instance of this role.

 

5. Case Study

(1) Gg invited his girlfriend and many beautiful women to dinner, but Gg could not cook or cook badly. This shows that GG does not need to create various food objects; all beautiful women have their own interests. After arriving at McDonald's, they can simply order what they like. McDonald's is a factory that produces all kinds of food. At this time, GG doesn't have to do it by herself, you can also invite so many beautiful women to dinner. All you need to do is pay O (shopping _ shopping) O Haha ~, The UML diagram is as follows:

 

 

6. Reference

[1] gof: design mode.

[2] Baidu Encyclopedia: http://baike.baidu.com/view/1227908.htm.

[3] http://blog.csdn.net/hguisu/article/details/7505909.

[4] http://www.cnblogs.com/debuglzq/archive/2012/02/29/2373609.html.

[5] http://blog.csdn.net/weiwenlongll/article/details/6918164.

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.