Design Pattern note-abstract factory pattern

Source: Internet
Author: User

Abstract Factory: provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.

The biggest benefit of the abstract factory model is that it is easy to switch the product series. Because the specific factory classes only appear once during initialization, it is very easy to change the specific factory of an application, it only needs to change the specific factory to use different product configurations. At the same time, it separates the process of creating an instance from the client. The client operates the instance through their abstract interfaces, and the specific class name of the product is also separated by the implementation of the specific factory, it does not appear in the Customer Code, so it fully follows the development-closed principle and dependency flip principle.

UML class diagram:


Abstract FactoryThe Basic Code of the mode is as follows:

/*************************************** * **************************** Filename: abstractFactory. hcreated: 2012-09-24author: firehoodpurpose: design Pattern of firehood-abstract factory pattern ******************************* **************************************/# pragma once # include <iostream> using namespace std; // abstract product Aclass AbstractProductA {public: AbstractProductA (void) {} virtual ~ AbstractProductA (void) {}}; // specific product A1, the first implementation of abstract product A class ConcreteProductA1: public AbstractProductA {public: ConcreteProductA1 (void) {cout <"production product A1" <endl;} virtual ~ ConcreteProductA1 (void) {cout <"Destroy product A1" <endl ;}}; // product A2: class ConcreteProductA2: public AbstractProductA {public: ConcreteProductA2 (void) {cout <"production product A2" <endl;} virtual ~ ConcreteProductA2 (void) {cout <"Destroy product A2" <endl ;}; // abstract product Bclass AbstractProductB {public: AbstractProductB (void) {} virtual ~ AbstractProductB (void) {}}; // product B1. class ConcreteProductB1: public AbstractProductB {public: ConcreteProductB1 (void) {cout <"production product B1" <endl;} virtual ~ ConcreteProductB1 (void) {cout <"Destroy product B1" <endl ;}}; // product B2: class ConcreteProductB2: public AbstractProductB {public: ConcreteProductB2 (void) {cout <"production product B2" <endl;} virtual ~ ConcreteProductB2 (void) {cout <"Destroy product B2" <endl ;}; // abstract factory class AbstractFactory {public: AbstractFactory (void) {} virtual ~ AbstractFactory (void) {} virtual AbstractProductA * CreateProductA () = 0; virtual AbstractProductB * CreateProductB () = 0 ;}; // Factory 1 class ConcreteFactory1: public AbstractFactory {public: concreteFactory1 (void) {cout <"create Factory 1" <endl;} virtual ~ ConcreteFactory1 (void) {cout <"Destroy Factory 1" <endl;} virtual AbstractProductA * CreateProductA () {// create product A1return new ConcreteProductA1 ();} virtual AbstractProductB * CreateProductB () {// create product B1return new ConcreteProductB1 () ;}; // factory 2 class ConcreteFactory2: public AbstractFactory {public: ConcreteFactory2 (void) {cout <"create factory 2" <endl;} virtual ~ ConcreteFactory2 (void) {cout <"Destroy factory 2" <endl;} virtual AbstractProductA * CreateProductA () {// create product A2return new ConcreteProductA2 ();} virtual AbstractProductB * CreateProductB () {// create product B2return new ConcreteProductB2 ();}};

The client call code is as follows:

# Include "AbstractFactory. h "# include <iostream> using namespace std; int main (int argc, char * argv []) {cout <"*********************************** ** "<endl; cout <"firehood learning design model --- abstract factory model" <endl; cout <"************************************ * "<endl; abstractFactory * factory = new ConcreteFactory1 (); // AbstractFactory * factory = new ConcreteFactory2 (); AbstractProductA * productA = factory-> CreateProductA (); abstractProductB * productB = factory-> CreateProductB (); delete productA; delete productB; delete factory; system ("pause"); return 0 ;}

The running result is as follows:

*************************************
Firehood-Abstract Factory Model
*************************************
Create Factory 1
Production product A1
Production product B1
Destroy product A1
Destroy product B1
Destroy Factory 1
Press any key to continue...

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.