Design Patterns learning notes -- Abstract Factory patterns

Source: Internet
Author: User
Tags switch case

Design Patterns learning notes -- Abstract Factory patterns

 

After several days of struggle, I finally understood some differences between the abstract factory and the factory method model.

 

Abstract Factory is similar to the factory method mode. They all follow the open-closed principle and use the factory interface to create a derived factory and then create the corresponding object. But the abstract factory is deeper, that is, a factory can create a series of products, while the factory method mode is generally a factory that only creates one product. In other words, an abstract factory is a product line that can produce a series of products. Each product line can produce a variety of products, but with different properties. When we expand, we expand a product line. In the factory method model, we expand a product. A simple factory is not conducive to expansion because of the condition such as switch case, but it is relatively simple.

 

Definition:

Abstract Factory mode is the most abstract and general factory mode in all forms. Abstract Factory mode refers to a factory mode used when multiple abstract roles exist. Abstract Factory mode provides an interface to the client to create product objects in multiple product families without specifying the specific product.

 

Example:

 

 

// Design mode Demo. cpp: defines the entry point of the console application. // # Include stdafx. h # include
 
  
# Include
  
   
Using namespace std;/* using the game engine U3D UE and graphics library D3D and OpenGL as two engines, the underlying invocation can be D3D or OpenGL. Although there are two products, the product family is different. Use the abstract factory mode * // graphics library base class Graphic {public: virtual void draw () = 0 ;}; // D3D Graphics Library class D3D: public Graphic {public: void draw () override {cout <
   
    
UseD3D ()-> draw (); engine-> useOpenGL ()-> draw (); // instantiate UE factory engine = new UEEngine (); // two products: engine-> useD3D ()-> draw (); engine-> useOpenGL ()-> draw (); getchar (); return 0 ;}
   
  
 

 

Result:

U3DEngine use D3D!
D3D draw!
U3DEngine use OpenGL!
OpenGL draw!
UEEngine use D3D!
D3D draw!
UEEngine use OpenGL!
OpenGL draw!

 

 

Differences between the factory method mode and the abstract factory mode:

 

Factory method mode:
An abstract product class can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
Each factory class can only create one instance of a specific product class.


Abstract Factory mode:
Multiple abstract product classes. Each abstract product class can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
You can create multiple product instances for each specific factory class.

Differences:
The factory method mode has only one abstract product class, while the abstract factory mode has multiple.
The factory method mode can only create one instance for a specific product type, while the abstract factory mode can create multiple instances.

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.