Abstract Factory Model

Source: Internet
Author: User

The most basic data access program
"You should first show me your original database access practices ."

"Let's use 'add user' and 'Get user' as examples ."

# Include <stdio. h>

 

Class User

{

Public:

Int id;

Char * name;

};

 

Class SqlserverUser

{

Public:

Void Insert (User * user)

{

Printf ("adding a record to the User table in SQL Server ");

}

 

User * GetUser (int id)

{

Printf ("obtain a User table record by ID in SQL Server ");

Return 0;

}

};

 

Int main ()

{

User * user = new User ();

SqlserverUser * su = new SqlserverUser ();

Su-> Insert (user );

Su-> GetUser (1 );

Delete user;

Delete su;

Return 0;

}

"This is what I wrote at the beginning, which is very simple ."

"The reason why the database cannot be changed is that SqlserverUser * su = new SqlserverUser (); this causes the su object to be framed on SQL Server. If it is flexible here, the professional points are said to be polymorphism, then execute 'su-> Insert (user); 'and 'Su-> GetUser (1 ); ', you don't have to consider whether you are using SQL Server or Access."

Data Access Program in factory Mode
# Include <stdio. h>

 

Class User

{

Public:

Int id;

Char * name;

};

 

Class IUser

{

Public:

Virtual void Insert (User * user) = 0;

Virtual User * GetUser (int id) = 0;

};

 

Class SqlserverUser: public IUser

{

Public:

Virtual void Insert (User * user)

{

Printf ("adding a record to the User table in SQL Server ");

}

 

Virtual User * GetUser (int id)

{

Printf ("obtain a User table record by ID in SQL Server ");

Return 0;

}

};

 

Class AccessUser: public IUser

{

Public:

Virtual void Insert (User * user)

{

Printf ("adding a record to the User table in Access ");

}

 

Virtual User * GetUser (int id)

{

Printf ("obtain a User table record by ID in Access ");

Return 0;

}

};

 

Class ifacloud

{

Public:

Virtual IUser * CreateUser () = 0;

};

 

Class SqlServerFactory: public ifacloud

{

Public:

Virtual IUser * CreateUser ()

{

Return new SqlserverUser ();

}

};

 

Class AccessFactory: public ifacloud

{

Public:

Virtual IUser * CreateUser ()

{

Return new AccessUser ();

}

};

 

Int main ()

{

User * user = new User ();

IFactory * factory = new SqlServerFactory ();

IUser * iu = factory-> CreateUser ();

Iu-> Insert (user );

Iu-> GetUser (1 );

Delete user;

Delete factory;

Delete iu;

Return 0;

}

"Very good. To change the database, you only need to change new SqlServerFactory () to new AccessFactory, the IUser interface object iu implementation does not know which database to access, but can complete the work well at runtime. This is the so-called decoupling of business logic and data access."

"However, if you write it like this, the Code still specifies 'new SqlServerFactory () '. There are still a lot to change ."

"This is not urgent. Later, the problem is not completely solved. It is impossible to have only one User table in your database. It is very likely that there are other tables, such as adding Department tables (Department tables ), how can this problem be solved?"

Abstract Factory Model
The customer class and the factory class are separated. When a consumer needs a set of products at any time, they only need to request the abstract factory. Abstract factories will then produce products that meet the product set specifications to specific factories.

Implementation Method (UML class diagram)


Implementation Code
# Include <stdio. h>

 

Class User

{

Public:

Int id;

Char * name;

};

 

Class Department

{

Public:

Int id;

Char * deptname;

};

 

// User Table Interface

Class IUser

{

Public:

Virtual void Insert (User * user) = 0;

Virtual User * GetUser (int id) = 0;

};

 

// Department Table Interface

Class IDepartment

{

Public:

Virtual void Insert (Department * department) = 0;

Virtual Department * GetDepartment (int id) = 0;

};

 

Class SqlserverUser: public IUser

{

Public:

Virtual void Insert (User * user)

{

Printf ("adding a record to the User table in SQL Server ");

}

 

Virtual User * GetUser (int id)

&

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.