[Design mode] -- Abstract Factory Mode

Source: Internet
Author: User

Abstract Factory provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.

 

Abstractproducta and abstractproductb are two abstract products. They are abstracted because they both have two different implementations, producta1, producta2, productb1, and productb2 are the implementation of the specific classification of the two abstract products. Abstractfactory is an abstract factory interface that should contain the Abstract METHODS created by all products, and concretefactroy1 and concretefactory2 are specific factories. Generally, a concretefactory class instance is created at runtime, and a specific factory creates a product object with a specific implementation, that is, to create different product objects, the client uses different factories.

Let's take a look at the example. The code structure is as follows:

# Include <iostream> using namespace STD; // user class user {PRIVATE: int _ id; string _ name; private: void setid (int id) {This-> _ id = ID;} int GETID () {return this-> _ id;} void setname (string name) {This-> _ name = Name ;} string getname () {return this-> _ name ;}; // iuser interface, used for client access. Class iuser {public: virtual void insert () {} virtual void getuser () {}}; // sqlserveruser class, used to access the SQL Server userclass sqlserveruser: Public iuser {public: void insert () {cout <"add a record to the user table in SQL Server" <Endl;} void getuser () {cout <"obtain a user table record by ID in SQL Server" <Endl ;}}; // accessuser class, used to access the access userclass accessuser: public iuser {public: void insert () {cout <"add a record to the user table in SQL Server" <Endl;} void getuser () {cout <"obtain a user table record by ID in SQL Server" <Endl ;}}; // Department class Department {PRIVATE: int _ id; string _ deptname; private: void setid (int id) {This-> _ id = ID;} int GETID () {return this-> _ id ;} void setdeptname (string name) {This-> _ deptname = Name;} string getname () {return this-> _ deptname ;}}; // idpedartment interface for client access, unbind the coupling class idepartment {public: Virtual void insert () {} virtual void getuser () {}}; // sqlserverdepartment class, departmentclass sqlserverdepartment: Public idepartment {public: void insert () {cout <"add a record to the Department table in SQL Server" <Endl ;} void getuser () {cout <"obtain a record in the department table by ID in SQL Server" <Endl ;}; // accessdepartment class, used to access the departmentclass accessdepartment of access: public idepartment {public: void insert () {cout <"add a record to the Department table in access" <Endl;} void getuser () {cout <"obtain a record in the department table by ID in access" <Endl ;}}; // ifacloud interface, define an abstract factory Interface Class ifacloud {public: Virtual iuser * createuser () = 0; virtual idepartment * createdepartment () = 0;} for creating an access Department table object ;}; // sqlserverfactory class, implements the ifactory interface, instantiates sqlserveruser and sqlserverdepartmentclass sqlserverfactory: Public ifactory {public: iuser * createuser () {return New sqlserveruser ();} idepartment * createdepartment () {return New sqlserverdepartment () ;}}; // accessfactory class, which implements the ifacloud interface and accessuser and accessdepartmentclass accessfactory: Public ifacloud {public: iuser * createuser () {return New accessuser () ;}idepartment * createdepartment () {return New accessdepartment () ;}}; int main () {user * user = new user (); department * dept = new Department (); ifacloud * factory = new accessfactory (); iuser * IU = factory-> createuser (); Iu-> insert (); iu-> getuser (); idepartment * id = factory-> createdepartment (); Id-> insert (); Id-> getuser (); Return 0 ;}

The advantage of the abstract factory model is that it is easy to exchange product series. Because of the specific factory classes, only one time during initialization is required for an application, this makes it very easy to change the specific factory of an application. It only needs to change the specific factory to use different product configurations. At the same time, it isolates the specific instance creation process from the client, the client operates instances through their abstract interfaces. The specific class names of the products are also separated by the implementations of specific factories and will not appear in the Customer Code.

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.