Design pattern C + + implementation 11: Abstract Factory mode

Source: Internet
Author: User
Tags idf

Abstract Factory: Provides an interface to create a series of related or interdependent objects without specifying their specific classes.

Pros: Easy to exchange product system, because the specific factory class, in an application only need to initialize the time to appear once, which makes it very easy to change the specific factory of an application, only need to change the specific factory can use different product configurations. It lets the concrete creation process and the client detach, the client is through their abstract interface operation instance, the product's specific class name is also separated by the concrete factory implementation, does not appear in the client code.

#ifndef abstractfactory_h#define abstractfactory_h#include<iostream> #include <string>using namespace Std;class idepartment{int id;string depname;public:virtual void Insert (Idepartment * dp) =0;virtual IDepartment * getdepartment (int id) = 0;}; Class Sqlserverdepartment:p ublic idepartment{public:void Insert (Idepartment * dp); Idepartment * getdepartment (int id); };void Sqlserverdepartment::insert (Idepartment * dp) {cout << adds a record to the department table in SQL Server. \ n ";} Idepartment * sqlserverdepartment::getdepartment (int id) {cout << "gets a record of the department table in SQL Server based on the ID. \ n "; return NULL;} Class Accessdepartment:p ublic idepartment{public:void Insert (Idepartment * dp); Idepartment * getdepartment (int id);}; void Accessdepartment::insert (Idepartment * dp) {cout << adds a record to the department table in Access. \ n ";} Idepartment * accessdepartment::getdepartment (int id) {cout << "gets a record of the department table in access based on the ID. \ n "; return NULL;} Class iuser{public:virtual void Insert (Iuser *) = 0;virtual Iuser* GETUSER (int id) = 0;}; Class Sqlserveruser:p ublic iuser{public:void Insert (Iuser *); Iuser * GETUSER (int id);}; void Sqlserveruser::insert (Iuser * in) {cout << adds a record to the user table in SQL Server. \ n ";} Iuser * SQLSERVERUSER::GETUSER (int id) {cout << "gets a record from the user table in SQL Server based on the ID. \ n "; return NULL;} Class Accessuser:p ublic iuser{public:void Insert (Iuser *); Iuser * GETUSER (int id);}; void Accessuser::insert (Iuser * in) {cout << adds a record to the user table in Access. \ n ";} Iuser * ACCESSUSER::GETUSER (int id) {cout << "gets a record from the user table in Access based on the ID. \ n "; return NULL;} Class Ifactory{public:virtual Iuser * CreateUser () = 0;virtual idepartment * createdepartment () = 0;}; Class Sqlserverfactory:p ublic Ifactory{public:iuser * CreateUser (); Idepartment * Createdepartment ();};i User * Sqlserverfactory::createuser () {return new Sqlserveruser;} Idepartment * Sqlserverfactory::createdepartment () {return new Sqlserverdepartment;} Class Accessfactory:p ublic Ifactory{public:iuser * CreateUser (); Idepartment * CreatedepartmenT ();};i User * Accessfactory::createuser () {return new Accessuser;} Idepartment * Accessfactory::createdepartment () {return new Accessdepartment;} #endif

#include "AbstractFactory.h" int main () {Iuser * useri = new Sqlserveruser;idepartment * Dpti = new SQLSERVERDEPARTMENT;IFAC Tory * factory = new Sqlserverfactory;iuser * user = Factory->createuser ();//idepartment * DPT = Factory->createdepa Rtment (); User->insert (Useri); User->getuser (1); Idepartment * DPT = factory->createdepartment ();DP t-> Insert (DPTI);DP t->getdepartment (1); Idepartment * Adpti = new Accessdepartment;ifactory * afactory = new Accessfactory ; Idepartment * Idf=afactory->createdepartment (); Idf->insert (Adpti); idf->getdepartment (1); IUser *AuserI = New Accessuser;iuser * AUf = Afactory->createuser (); Auf->insert (Auseri); Auf->getuser (1); return 0;}


Design pattern C + + implementation 11: Abstract Factory mode

Related Article

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.