Factory, factory design mode, C + + description

Source: Internet
Author: User

analysis and implementation of common design patterns(c + +)One-factoryModefunction:
defines an interface for creating objects, letting subclasses decide which class to instantiate. Factory Methoddefer instantiation of a class to its subclasses
abstract base class: 1) Product: The abstract base class for objects created. 2) Factory the abstract base class for the factory method that created the object. interface functions: 1) Creator::factorymethod: pure virtual function, implemented by a derived class, creates a corresponding Product. parsing: in this pattern, there are two abstract base classes, one is the abstract base class for objects created by Product,One is that Factory is the abstract base class of the factory, and in collaboration with each other is the corresponding Factory factionClass to generate a derived class of product, i.e. if you want to add a new productA new Factory, created by the process entrusted to the Factory. That means a Factory.and a Product is a relationship that corresponds to one by one. remark: The factory class is named Creator on the demo diagram of the design pattern, and the following implementation inherits the name.
#ifndef __factory_h__ #define __FACTORY_H__ #include <iostream> using namespace std; class Product { Public : product () {cout<< "product ()" <<endl; } Virtual ~product () {cout<< "~product ()" <<endl; } }; class A { Public : A () {cout<< "a ()" <<endl; } ~a () {cout<< "~a ()" <<endl; } }; class B { Public : B () {cout<< "B ()" <<endl; } ~b () {cout<< "~b ()" <<endl; } }; Class Concreteproduct:public Product { Public : concreteproduct (* pa,b* pb): _pa (PA), _PB (PB)                 { cout<< "concreteproduct ()" <<endl;                 } ~concreteproduct () {cout<< "~concreteproduct ()" <<endl; } Private: A * _PA; b* _PB; }; class factory { Public : Factory () {cout<< "factory ()" <<endl; } //abstract class virtual product* Create () = 0; Virtual ~factory () {cout<< "~factory ()" <<endl; } }; Class Concretefactory:public Factory { Public : concretefactory () {cout<< "concretefactory ()" <<endl; } product* Create (); ~concretefactory () {cout<< "~concretefactory ()" <<endl; } }; product* concretefactory::create () { cout<< "concretefactory::create ()" <<endl; A * pa = new A (); b* PB = new B (); product* Pro = new Concreteproduct (PA,PB); return pro; } endif #include <iostream> #include "factory.h" using namespace std; int main () { //Factory mode is primarily used to create complex objects //One product corresponds to a factory class factory* FAC = new Concretefactory (); product* Pro = Fac->create (); delete FAC; Delete Pro; return 0; }






Factory, factory design mode, C + + description

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.