Abstract Factory Model
Instance:
Implement a simple database access program
UML class diagram:
Implementation Code:
class User { private int _id; public int ID { get { return _id; } set { _id = value; } } private string _name; public string Name { get { return _name; } set { _name = value; } } } class Department { private int _id; public int ID { get { return _id; } set { _id = value; } } private string _deptName; public string DeptName { get { return _deptName; } set { _deptName = value; } } }
Interface IUser {void Insert (User user User); User GetUser (int id);} class SqlserverUser: IUser {public void Insert (user User) {Console. writeLine (add a record to the User table in Sqlserver);} public User GetUser (int id) {Console. writeLine (a User table record is obtained by ID in Sqlserver); return null ;}} class AccessUser: IUser {public void Insert (User user User) {Console. writeLine (add a record to the User table in Access);} public User GetUser (int id) {Console. writeLine (obtain a User table record by ID in Access); return null ;}}
Interface IDepartment {void Insert (Department department Department); Department GetDepartment (int id);} class SqlserverDepartment: IDepartment {public void Insert (department Department department) {Console. writeLine (add a record to the Department table in Sqlserver);} public Department GetDepartment (int id) {Console. writeLine (a record in the Department table obtained by ID in Sqlserver); return null ;}} class AccessDepartment: IDepartment {public void Insert (Department department Department) {Console. writeLine (add a record to the Department table in Access);} public Department GetDepartment (int id) {Console. writeLine (obtain a record in the Department table based on the ID in Access); return null ;}}
interface IFactory { IUser CreateUser(); IDepartment CreateDepartment(); } class SqlServerFactory : IFactory { public IUser CreateUser() { return new SqlserverUser(); } public IDepartment CreateDepartment() { return new SqlserverDepartment(); } } class AccessFactory : IFactory { public IUser CreateUser() { return new AccessUser(); } public IDepartment CreateDepartment() { return new AccessDepartment(); } }
Client implementation:
static void Main(string[] args) { User user = new User(); Department dept = new Department(); //AbstractFactory factory = new SqlServerFactory(); IFactory factory = new AccessFactory(); IUser iu = factory.CreateUser(); iu.Insert(user); iu.GetUser(1); IDepartment id = factory.CreateDepartment(); id.Insert(dept); id.GetDepartment(1); Console.Read(); }
Note:
Provides an interface for creating a set of related or interdependent objects without specifying their specific classes.
Differences between Abstract Factory mode and factory method mode:
Each mode is a solution to a certain problem. The factory method mode is for a product level structure, while the abstract factory mode is for multiple product level structures.
In the preceding example, if the Department section is removed, the factory method mode is used only when the User class is used. However, the database contains a lot of information, except for the User and Department tables, here, User, Department, and other information belong to a product family. Access and SqlServer have two different levels.
When there are multiple products with different hierarchical structures, if you use the factory method model, it is necessary to use multiple independent factory hierarchical structures to deal with the hierarchical structures of these products. If these product levels are parallel, multiple parallel factory levels may occur.
Abstract Factory mode uses the same factory level structure to create product objects of different product levels.
Each product family has a specific factory. Each specific factory creation belongs to the same product family, but is classified into products of different levels.
By introducing the abstract factory model, you can create product objects in multiple product families with the same (or similar) level structure.
Since each factory role is responsible for creating two product objects of different levels, each factory role must provide two factory methods for creating products of two levels. Since each specific factory role needs to implement these two factory methods, it is general and may be abstracted and declared in the abstract factory role.