Big talk design pattern C + + implementation-15th chapter-Abstract Factory mode

Source: Internet
Author: User

First, UML diagram


Ii. the roles involved

(1) Abstract Factory

(2) Specific factory: including the specific factory 1 and the specific factory 2. Specific Factory 1 for the production of specific products A1 and specific products B1, specific factory 2 for the production of specific products A2 and specific product B2;

(3) Abstract products: including abstract product A and abstract product B;

(4) Specific products: including abstract product A corresponding to the specific product A1 and A2, as well as the abstract product B corresponding to the specific product B1 and B2.

Description: In "Big talk design mode", the above 1 and 2 represent SQL Server database and Access database respectively. The A and B above represent the user table and the Department table in the database respectively.

Third, the advantage

(1) Abstract Factory mode is an improvement to the factory method model. There is more than one type of case for dealing with products (in factory method mode, the product is only user, whereas in abstract Factory mode, the product includes both user and department).

(2) The use of abstract Factory mode should be considered in the following cases :

    • A system should not rely on the details of how a product class instance is created, composed, and expressed, which is important for all forms of Factory mode.
    • This system has more than one product family, and the system only consumes one of the product families.
    • Products belonging to the same product family are used together, and this constraint must be reflected in the design of the system.
    • The system provides a library of product classes, with all products appearing on the same interface, so that clients do not rely on implementations.

(3) Interpretation: in the example above, the product family consists of two: 1 and 2, which is SQL Server database and Access database. Each product family contains two types of products: A and B, the user table and the Department table. and the products in each product family to use together, that is, product family 1 in the two categories of products A and B to use together, that is, in SQL Server database Sqlserveruser table and sqlserverdepartment table to use together, Access database the same.

Iv. implementation of C + +

#include <iostream> #include <cstdlib>using namespace std;//database table entry: UserClass user{private:int id;string Name;public:int GetID () {return ID;} String GetName () {return name;} void SetID (int ID) {this->id=id;} void SetName (String NAME) {this->name=name;}};/ /database table entry: Departmentclass department{private:int id;string name;public:int GetID () {return ID;} String GetName () {return name;} void SetID (int ID) {this->id=id;} void SetName (String NAME) {this->name=name;}};/ /abstract product A:iuserclass iuser{public:virtual void Insert (user user) =0;virtual user* GetUser (int id) =0;};/ /Specific product A1:sqlserveruserclass sqlserveruser:public iuser{public:void Insert (user user) {cout<< "in SQL The server adds a record "&LT;&LT;ENDL;" to the user table. user* GetUser (int id) {cout<< "Get user table one record in SQL Server based on ID" <<endl;return null;}};/ /Specific product A2:accessuserclass accessuser:public iuser{public:void Insert (user user) {cout<< "added a record to the User table in Access" <<endl;} user* GetUser (int id) {cout<< "Gets the user table one record in Access by ID" <<endl;returN null;}};/ /abstract Products B:idepartmentclass idepartment{public:virtual void Insert (Department Department) =0;virtual department* getdepartment (int id) =0;};/ /Specific product B1:sqlserverdepartmentclass sqlserverdepartment:public idepartment{public:void Insert (Department Department) { cout<< "added a record to the department table in SQL Server" &LT;&LT;ENDL; department* getdepartment (int id) {cout<< "Get a record of Department table in SQL Server based on ID" <<endl;return null;}};/ /Specific product B2:accessdepartmentclass accessdepartment:public idepartment{public:void Insert (Department Department) {cout << "added a record to the department table in Access" &LT;&LT;ENDL; department* getdepartment (int id) {cout<< "Get a record of Department table in Access by id" <<endl;return null;}};/ /abstract Factory: Ifactoryclass ifactory{public:virtual iuser* CreateUser () =0;virtual idepartment* createdepartment () =0;};/ /Specific factory 1:sqlserverfactoryclass sqlserverfactory:public ifactory{public:iuser* CreateUser () {return new Sqlserveruser;} idepartment* createdepartment () {return new sqlserverdepartment;}};/ /Specific Factory 2:Accessfactoryclass accessfactory:public ifactory{public:iuser* CreateUser () {return new Accessuser;} idepartment* createdepartment () {return new accessdepartment;}};/ /client void Main () {User user;department department;ifactory* factory=new sqlserverfactory;iuser* iu=factory-> CreateUser (); Iu->insert (user); Iu->getuser (1); idepartment* id=factory->createdepartment (); Id->insert (department); Id->getdepartment (1); System ("Pause");}


Big talk design pattern C + + implementation-15th chapter-Abstract Factory mode

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.