Design Pattern (1): abstract factory pattern -- Creation Pattern

Source: Internet
Author: User

1. Definition

Provides an interface for creating a set of related or interdependent objects without specifying their specific classes.

  

2. Applicable scenarios

1. A system must be created, combined, and expressed independently of its products. 2. A system must be configured by one of multiple product generations. 3. When you want to emphasize the design of a series of related product objects for joint use. 4. When you provide a product library, you only want to display their interfaces instead of implementations.

  

3. Comment

1. it separates the specific class 2. it makes it easy to switch product series 3. it facilitates product consistency 4. difficult to support new types of products 5. the "open-closed" principle requires that the system be open to expansion and closed to modification. To enhance its functions through expansion. For systems involving multiple product families and multi-product hierarchical structures, the function enhancement includes two aspects: Adding product families: Abstract Factory supports the "open-closed" principle. Add the hierarchical structure of New Products: All factory roles need to be modified, and the "open-closed" principle is not well supported. In combination, the abstract factory model supports the addition of new products in a skewed manner, which facilitates the addition of new product families, however, it is not convenient to add a new product level structure.

  

To facilitate the introduction of the abstract factory model, a new concept is introduced: product family ). A product family is a family of products with different product levels and functions.

 

The figure shows four product families distributed in three different product levels. You only need to specify the product family of a product and its hierarchical structure.

 

4. Mode Structure

1. Abstract Factory role: this role is at the core of the factory method model and is irrelevant to the business logic of the application system. 2. The specific factory role: this role directly creates a product instance under the client call. This role contains the logic for selecting appropriate product objects, which is closely related to the business logic of the application system. 3. Abstract Product role: the class that assumes this role is the parent class of the object created in the factory method mode or the interfaces they share. 4. The role of a specific product: Any product object created in the abstract factory model is an instance of a specific product category. This is what the client eventually needs, and it must be filled with the commercial logic of the application system.

 

Abstract Factory refers to all objects in a product family that can be created by a factory level structure into different product levels. If you use a diagram to describe it, for example:

 

 

5. Case Study

(1)

In the abstract factory model, each concretecreator produces only one specific product. It is like a BMW production workshop that only produces BMW cars, rather than Mercedes-Benz cars, the production workshop of the same Mercedes-Benz can only produce Mercedes-Benz cars instead of BMW cars. Here, the production workshops of BMW and Mercedes-Benz use the same construction model, that is, they all inherit the creator, for product, I only know that a Mercedes-Benz is needed, but I don't care which workshop the car is produced. The difference between factory pattern and abstract factory pattern can be compared by BMW and Mercedes. In factory pattern, the user only needs to provide a product from the Mercedes-Benz workshop or the BMW workshop, that is, the product type has been specified by the user. In abstract factory pattern, the user will not specify whether the product I want is from the Workshop of BMW or Mercedes.

(2)

 

6. code example

The code in [5] is more detailed.

 

7. Reference

[1] gof: design mode.

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

[3] http://www.360doc.com/content/11/1013/15/7909375_155752540.shtml.

[4] http://blog.csdn.net/sanmaofly/article/details/5085133.

[5] http://wuhongyu.iteye.com/blog/2024092.

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.