Design Pattern learning ----- abstract factory pattern

Source: Internet
Author: User

GOOD: defines an interface for creating a series of related or interdependent interfaces without specifying their specific classes.

Used to exchange product series, such as ACCESS-> SQLSERVER;

The specific Class Name of the product is separated by the implementation of the specific factory

 

[Html]
# Include <string>
# Include <iostream>
# Include <vector>
Using namespace std;
 
// 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;
};
 
// ACCESS 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 User" <endl;
}
};
 
// SQL Department
Class CSqlDepartment: public IDepartment
{
Public:
Virtual void GetDepartment ()
{
Cout <"SQL getDepartment" <endl;
}
Virtual void InsertDepartment ()
{
Cout <"SQL insertdepartment" <endl;
}
};
 
// Abstract Factory
Class ifacloud
{
Public:
Virtual IUser * CreateUser () = 0;
Virtual IDepartment * CreateDepartment () = 0;
};
 
// ACCESS Factory
Class AccessFactory: public ifacloud
{
Public:
Virtual IUser * CreateUser ()
{
Return new CAccessUser ();
}
Virtual IDepartment * CreateDepartment ()
{
Return new CAccessDepartment ();
}
};
 
// SQL Factory
Class SqlFactory: public ifacloud
{
Public:
Virtual IUser * CreateUser ()
{
Return new CSqlUser ();
}
 
Virtual IDepartment * CreateDepartment ()
{
Return new CSqlDepartment ();
}
};
 
// Client:
Int main ()
{
IFactory * factory = new SqlFactory ();
IUser * user = factory-> CreateUser ();
IDepartment * depart = factory-> CreateDepartment ();
User-> GetUser ();
Depart-> GetDepartment ();
Return 0;
}
 

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.