Product catalog creation-Abstract Factory model-Abstract Factory

Source: Internet
Author: User

Product catalog creation-Abstract Factory model-Abstract Factory
The factory method model solves the problem of excessive factory responsibilities in the simple factory model by introducing the factory level structure. However, in the factory method model, each factory only produces one type of products, there may be a large number of factory classes in the system, which will inevitably increase the overhead of the system. At this time, you can consider forming a "Product License" for some related products and producing them in a unified manner in the same factory. This is the basic idea of the abstract factory model.


Product level structure and product catalog sometimes expect a factory to provide multiple product objects, rather than a single product object. For example, an electrical appliance factory can produce television sets, refrigerators, air conditioners, and other electrical appliances, rather than just one electrical appliance.
Product level structure (1) product level structure refers to the inheritance structure of products. For example, an abstract class is a TV set. Its sub-classes include Haier TV, Hisense TV, and tcl TV, the abstract TV set and the TV set of a specific brand constitute a product level structure. The abstract TV set is the parent class, and the TV set of a specific brand is its sub-class. (2) product family. In the abstract factory model, a product family is a group of products produced by the same factory in different product levels. For example, Haier TV set and Haier refrigerator produced by Haier Electric Appliance Factory. Haier TV is located in the TV product level structure, Haier refrigerator is located in the refrigerator product level structure, Haier TV, Haier refrigerator constitute a product family.
The main difference between the abstract factory mode and the factory method mode is that the factory method mode only targets a product level structure, while the abstract factory needs to face multiple product level structures, A factory level structure can be used to create multiple product objects in different product levels. When a factory level structure can create all objects in a product family with different product levels, the abstract factory mode is simpler and more efficient than the factory method mode.


In Figure 6-4, each specific factory can produce all products belonging to the same product family, such as square, circle, and oval shapes with the same color, the products produced are in different product level structures. If the factory method mode is used, 15 specific factories are required, while the abstract factory mode requires only five specific factories, greatly reducing the number of classes in the system.
Abstract Factory mode overview Abstract Factory mode: provides an interface for creating a series of related objects without specifying their specific classes. In the abstract factory model, each specific factory provides multiple factory methods to produce a variety of different types of products, which constitute a product family. Abstract factory pattern structure


The abstract factory schema structure contains the following four roles. (1) AbstractFactory (Abstract Factory): it declares a set of methods used to create a family of products. Each method corresponds to a product. (2) ConcreteFactory (specific factory): it implements the method of creating products declared in the abstract factory to produce a group of specific products which constitute a product family, each product is in a product level structure. (3) AbstractProduct: it declares interfaces for each product and declares the business methods of the product in the abstract product. (4) ConcreteProduct: it defines the specific product objects produced by a specific factory and implements the Business Methods declared in the abstract product interface.
The inclination of the Open and Close principles (1) increase the product family. For new product families, the abstract factory mode supports the open and closed principle. You only need to add a specific product and add a new factory. You do not need to make any changes to the existing code. (2) Add a new product level structure. To add a new product level structure, you need to modify the factory role, including the abstract factory class. In the factory class, you need to add methods to produce new products, which violates the open and closed principles.
Abstract Factory mode summary main advantages (1) when multiple objects in a product family are designed to work together, it ensures that the client always uses only objects in the same product family. (2) It is convenient to add a new product family. You do not need to modify the existing code to comply with the open and closed principles.
The main disadvantage is that it is difficult to add a new product level structure. The original system needs to be greatly improved, and even the abstract layer code needs to be modified, which violates the open and closed principles.
Application Scenario (1) the system has multiple product families, and only one of them is used at a time. (2) It is convenient to add a new product family. You do not need to modify the existing system and the system complies with the open and closed principles.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.