Factory mode of design mode (c + +)

Source: Internet
Author: User

The Factory mode has two major functions:

(1), defines the interface to create objects, encapsulates the creation of objects;

(2), so that the materialized class work is deferred to the sub-class.

//Product.h#ifndef _product_h_#define_product_h_classproduct{ Public: virtual void~Product ();protected: void Product ();Private:   }classConcreteproduct: Publicproduct{ Public: void Concreteproduct (); void~concreteproduct ();protected:Private:}#endif //_product_h_
// Product.cpp " Product.h "  <iostream>usingnamespace  std; void Product::P roduct () {} void product::~Product () {}void  concreteproduct::concreteproduct () {}void concreteproduct::~concreteproduct () {}
//Factory.h#ifndef _factory_h_#define_factory_h_classProduct;classfactory{ Public:    Virtual void~Factory (); VirtualProduct *createproduct () =0; protected:    voidFactory ();Private:};classConcretefactory: Publicfactory{ Public:    voidconcretefactory (); void~concretefactory (); Product*createproduct ();protected:Private:    }#endif //_factory_h_
//Fatory.cpp#include"Factory.h"#include"Product.h"#include<iostream>using namespacestd;voidfactory::factory () {}voidfactory::~Factory () {}voidconcretefactory::concretefactory () {}voidconcretefactory::concretefactory () {}product concretefactory::createproduct () {concreteproduct cproduct=Newconcreteproduct; returnproduct;}
//main.cpp#include"Factory.h"#include"Product.h"#include<iostream>using namespacestd;intMainintargcChar*argv[]) {Factory*FAC =Newconcretefactory; Product*p = fac->createproduct (); return 0;}

Factory mode of design mode (c + +)

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.