Design Mode note-factory method mode

Source: Internet
Author: User

Factory Method: defines an interface used to create objects, so that the subclass decides to instantiate the class. The Factory Method extends the instantiation of a class to its subclass.

UML class diagram:

The basic code for the factory method mode is as follows:

/*************************************** * **************************** Filename: factorymethod. hcreated: 2012-09-21author: firehoodpurpose: design Mode of firehood-factory method mode ******************************* **************************************/# pragma once # include <iostream> using namespace STD; // abstract product class product {public: product () {} virtual ~ Product () {}}; // specific product Aclass concreteproducta: Public Product {public: concreteproducta () {cout <"production product a" <Endl;} virtual ~ Concreteproducta () {cout <"Destroy product a" <Endl ;}}; // product-specific bclass concreteproductb: Public Product {public: concreteproductb () {cout <"production product B" <Endl;} virtual ~ Concreteproductb () {cout <"Destroy product B" <Endl ;};// abstract factory class creator {public: creator () {}; virtual ~ Creator () {}; product * createproduct () {return factorymethod () ;}protected: Virtual Product * factorymethod () = 0 ;}; // specific factory Aclass concretecreatora: public creator {public: concretecreatora () {cout <"create factory a" <Endl;} virtual ~ Concretecreatora () {cout <"Destroy factory a" <Endl;} protected: Virtual Product * factorymethod () {return New concreteproducta ();}}; // The actual factory bclass concretecreatorb: Public creator {public: concretecreatorb () {cout <"create factory B" <Endl;} virtual ~ Concretecreatorb () {cout <"Destroy factory B" <Endl;} protected: Virtual Product * factorymethod () {return New concreteproductb ();}};

Client call code:

Int main (int argc, char * argv []) {cout <"*********************************** ** "<endl; cout <"firehood learning design model-factory method model" <endl; cout <"************************************ * "<endl; creator * factoryA = new ConcreteCreatorA (); Product * productA = factoryA-> CreateProduct (); Creator * factoryB = new ConcreteCreatorB (); Product * productB = factoryB-> CreateProduct (); delete productA; delete factoryA; delete productB; delete factoryB; system ("pause"); return 0 ;}

Running result:

Firehood learning design model-factory method model
Create Factory
Production product
Create factory B
Production product B
Destroy product
Destruction plant
Destroy product B
Destroy factory B

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: 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.