Design pattern--4. Abstract Factory mode

Source: Internet
Author: User

1. Mode motivation

In the factory method model the specific factory is responsible for the production of specific products, each specific plant corresponds to a specific product, the factory method is also unique, in general, a specific factory only one factory method or a set of overloaded factory methods. But sometimes we need a factory that can provide multiple product objects, rather than a single Product object.

In order to understand the factory method pattern more clearly, two concepts need to be introduced first:

Product Hierarchy Structure : Product hierarchy structure is the product's inheritance structure, such as an abstract class is a television, its sub-category has Haier TV, Hisense TV, TCL TV, then the abstract TV and the specific brand of television constitute a product hierarchy structure, abstract TV is the parent class, and the specific brand of television is its sub-category.

Product Family : In the abstract factory model, product family refers to a group of products produced by the same factory, located in different product grade structure, such as Haier TV Haier electric factory production, Haier Refrigerator, Haier TV set in the TV product hierarchy, Haier Refrigerator is located in the refrigerator product grade structure.

Abstract Factory mode is required when the specific product required by the system to be produced is not a simple object, but multiple specific products that are in different types of product hierarchy.

Abstract Factory mode is the most abstract and general form of all forms of Factory mode.

The biggest difference between the abstract factory model and the factory method pattern is that the factory method model is for a product hierarchy, while the abstract factory model requires multiple product hierarchy structures, and a factory hierarchy can be responsible for creating product objects in multiple different product hierarchy structures. Abstract Factory mode is simpler and more efficient than the factory method mode when a factory hierarchy can create all objects in a product family that belong to different product hierarchy structures.

2. Pattern definition

Abstract Factory Pattern: Provides an interface to create a series of related or interdependent objects without specifying their specific classes. Abstract Factory mode, also known as the kit mode, belongs to the object creation mode.

3. Pattern structure

The abstract factory pattern contains the following roles:

Abstractfactory: Abstract Factory

Concretefactory: Specific Factory

Abstractproduct: Abstract Products

Product: Specific Products

4. Timing Diagram

5. Code Analysis
#include <iostream>#include"AbstractFactory.h"#include"AbstractProductA.h"#include"AbstractProductB.h"#include"ConcreteFactory1.h"#include"ConcreteFactory2.h"using namespacestd;intMainintargcChar*argv[]) {Abstractfactory* FC =NewConcreteFactory1 (); Abstractproducta* PA = fc->createproducta (); ABSTRACTPRODUCTB* PB = Fc->CREATEPRODUCTB (); PA-Use (); PB-eat (); Abstractfactory* FC2 =NewConcreteFactory2 (); Abstractproducta* PA2 = fc2->createproducta (); ABSTRACTPRODUCTB* PB2 = fc2->CREATEPRODUCTB (); PA2-Use (); PB2->eat ();
 #include  concretefactory1.h  Span style= "color: #800000;" > "  #include  "  producta1.h    #include   " productb1.h  "  abstractproducta  * Concretefactory1::createproducta () { return   ProductA1 ();} ABSTRACTPRODUCTB  * CONCRETEFACTORY1::CREATEPRODUCTB () { return  new   ProductB1 ();}  
" ProductA1.h "  <iostream>usingnamespace  std; void Producta1::use () {    "useProduct A1" << Endl;}

Operation Result:

6. Advantages

Abstract Factory mode isolates the generation of specific classes so that customers do not need to know what is being created. Because of this isolation, it becomes relatively easy to replace a specific factory. All of the specific factories implement the common interfaces defined in the abstract factory, so you can change the behavior of the entire software system to some extent by simply changing the instance of the specific factory. In addition, the application of abstract Factory mode can realize the design goal of high cohesion and low coupling, so the abstract factory model has been widely used.

When multiple objects in a product family are designed to work together, it ensures that the client always uses only objects from the same product family. This is a very practical design pattern for some software systems that need to determine their behavior based on the current environment.

It is convenient to add new factories and product families, and it is not necessary to modify the existing system to meet the "open and closed principle".

7. Disadvantages

When adding a new product object, it is difficult to extend the abstract factory to produce a new kind of product, because the abstract factory role specifies all the product collections that may be created, to support the new kind of product means to extend the interface, which involves the modification of the abstract factory role and all its subclasses. Obviously, it will bring about great inconvenience.

The tilt of the open/closed principle (adding new plant and product families is easy, adding new product grade structure trouble).

8. Applicable environment

You can use the abstract Factory mode in the following situations:

A system should not rely on the details of how a product class instance is created, composed, and expressed, which is important for all types of factory models.

There are more than one product family in the system, and only one of the product families is used at a time.

Products belonging to the same product family will be used together, and this constraint must be reflected in the design of the system.

The system provides a library of product classes, with all products appearing on the same interface, so that clients do not depend on the implementation.

9. Mode application

In many software systems you need to change the theme of the interface, the interface buttons, text boxes, background color, and so on, you can use the Abstract Factory mode design.

10. Mode expansion

The inclination of the "opening and closing principle"

The "open and close principle" requires the system to be opened to expand, to modify the closed, through the expansion to enhance its function. For systems that involve multiple product families and multiple product hierarchies, the enhancements include two aspects:

Add product Family: For the addition of new product family, the factory method model is very good support of the "open and close principle", for the newly added product family, only need to add a new specific factory, there is no need to make any changes to the existing code.

Add a new product hierarchy: To add a new product hierarchy, you need to modify all the plant roles, including the Abstract factory class, in all factory classes need to increase the production of new products, not well support the "open and close principle."

This nature of the abstract factory pattern is called the tilt of the "open and close" principle, and abstract Factory mode supports the addition of new products in a slanted manner, which facilitates the addition of new product families, but does not provide such convenience for the increase in the level structure of newer products.

Degradation of the factory model

When each specific factory class in the abstract factory pattern creates only one product object, that is, when there is only one product hierarchy, the abstract factory model degrades into a factory method pattern; When the factory method pattern is combined with a specific factory, a unified factory is provided to create the Product object, And when the factory method of creating objects is designed as a static method, the factory method pattern degrades into a simple Factory mode.

11. Summary

Abstract Factory mode provides an interface to create a series of related or interdependent objects without specifying their specific classes. Abstract Factory mode, also known as the kit mode, belongs to the object creation mode.

The abstract factory pattern contains four roles: The abstract factory is used to declare the method of generating abstract products; The factory implements an abstract factory declaration of the method of generating abstract products, a set of specific products, which constitute a product family, each product is located in a product hierarchy; Abstract products declare interfaces for each product, The abstract business method of the product is defined in the abstract product, and the specific product defines the specific product objects produced by the specific factory, and realizes the business method defined in the abstract product interface.

Abstract Factory mode is the most abstract and general form of all forms of Factory mode. The biggest difference between the abstract factory model and the factory method pattern is that the factory method model is for a product hierarchy, while the abstract factory model requires multiple product hierarchies.

The main advantage of the abstract factory pattern is that it isolates the generation of specific classes so that customers do not need to know what is being created, and each time it is possible to create multiple objects in a product family through a specific factory class, it is convenient to add or replace product families, and to add new factories and product families. The main drawback is that the addition of a new product hierarchy is complicated by the need to modify the abstract factory and all the specific factory classes, and the support for the "open and close principle" is tilted.

The application of the abstract Factory pattern includes: A system should not rely on the details of how a product class instance is created, composed, or expressed; there are more than one product family in the system, and only one product family is used at a time; products belonging to the same product family will be used together; The system provides a library of product classes, All products appear on the same interface, so that the client does not rely on a specific implementation.

Design pattern--4. Abstract Factory mode

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.