C + + design mode from 0 to abstract Factory mode

Source: Internet
Author: User

Abstract Factory (Abstract Factory mode): Provides an interface that creates a series of related or interdependent objects without specifying their specific classes.Abstract Factory mode (Factory)Chase mm, please eat, McDonald's chicken wings and KFC chicken wings are all mm love to eat things, although the taste is different, but whether you take mm to McDonald's or KFC, just to the waiter said "to four chicken wings" on the line. McDonald's and KFC are the factory to produce chicken wings.Factory mode: Separate the customer class from the factory class. Consumers need a product at any time, just request it from the factory. Consumers can accept new products without modification. The disadvantage is that when the product is modified,Factory classAlso make the corresponding changes. such as: How to create and how to provide to the client.
User abstract interface class iuser{public:virtual void GetUser () = 0;virtual void Insertuser () = 0;};/ /Department Abstract interface Class idepartment{public:virtual void getdepartment () = 0;virtual void insertdepartment () = 0;};/ /acess User class caccessuser:public iuser{public:virtual void GetUser () {cout << "Access GetUser" << Endl;} virtual void Insertuser () {cout << Access insertuser << endl;}};/ /access Department class caccessdepartment:public idepartment{public:virtual void Getdepartment () {cout << Access Getdepartment "<< Endl;} virtual void Insertdepartment () {cout << Access insertdepartment << endl;}};/ /sql User class csqluser:public iuser{public:virtual void GetUser () {cout << "Sql user" << Endl;} virtual void Insertuser () {cout << Sql Insert User << endl;}};/ /sql Department class csqldepartment:public idepartment{public:virtual void Getdepartment () {cout << "Sql Department" <& Lt Endl;} virtual void Insertdepartment () {cout << Sql Insert Department << endl;}};/ /abstract Factory class ifactory{public:virtual Iuser * CreateUser () = 0;virtual idepartment * createdepartment () = 0;};/ /access Factory class Accessfactory:public ifactory{public:virtual Iuser * CreateUser () {return new Caccessuser (); Virtual Idepartment * Createdepartment () {return new caccessdepartment ();}};/ /sql Factory class Sqlfactory:public ifactory{public:virtual Iuser * CreateUser () {return new Csqluser (); Virtual Idepartment * Createdepartment () {return new csqldepartment ();}}; int _tmain (int argc, _TCHAR * argv[]) {Ifactory * factory = new sqlfactory (); Iuser * user = Factory->createuser (); Idepar Tment * depart = Factory->createdepartment (); User->getuser ();d epart->getdepartment (); return 0;}
The key point is

C + + design mode from 0 to 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.