1. Overview
Abstract Factory mode provides a unified creation interface for a product family. When a series of this product family is needed, the corresponding series can be selected from the abstract factory to create a specific factory category.
2. The role in the abstract factory model
2.1 Abstract Factory (Abstractfactory): This role is the core of the factory method model, which is unrelated to the business logic of the application system.
2.2 Specific Factory (Concretefactory): This role creates an instance of the product directly under the call of the client. This role contains the logic to choose the right product object, which is closely related to the business logic of the application system.
2.3 Abstract Product (ABSTRACTPRODUCT): The class that holds this role is the parent class of objects created by the factory method pattern, or the interfaces they share.
2.4 Specific product (CONCRETEPRODUCT): Any Product object created by the abstract factory pattern is an instance of a specific product class. This is what the client ultimately needs, and its interior must be full of the business logic of the application system.
3. Example: Replace the database. the existing system uses the SQL Server database, its licence is paid, and some customers want to use a free database service, such as access. Some customers have the licence of other database service providers, and they do not want to use SQL Server that has to be paid for otherwise.
3.1 The fundamental solution to this problem is to decouple the application from the database so that the application no longer relies on a specific database. The abstract factory provides us with solutions.
3.2 Implementation Class diagram
3.3 Implementation Code
The 3.3.1 Abstract factory class provides an interface to create a set of related or interdependent objects.
<summary>
///Abstract Factory class
///</summary> public
interface idatabasefactory
{
IEmployee Createemployee ();
Iuser CreateUser ();
}
3.3.2 a specific factory class that provides implementations for creating SQL Server, or access specific products
<summary>
///and SQL Server related products implementation
///</summary> public
class Sqlserverdatabasefactory : idatabasefactory
{public
iemployee Createemployee ()
{return
new Sqlemployee ();
}
Public Iuser CreateUser ()
{return
new SqlUser ();
}
}
<summary>
///Implementation of access-related products
///</summary> public
class Accessdatabasefactory: Idatabasefactory
{public
iemployee Createemployee ()
{return
new Accessemployee ();
}
Public Iuser CreateUser ()
{return
new Accessuser ();
}
}
3.3.32 abstract products, respectively, Iuser and IEmployee
<summary>
///Abstract Products
///</summary> public
interface Iuser
{
User getuser ();
BOOL Saveuser ();
}
<summary>
///Abstract Products
///</summary> public
interface IEmployee
{
Employee GetEmployee ();
BOOL Saveemployee ();
}
3.3.4 the realization of specific products
<summary>///specific products related to SQL Server user///</summary> public class Sqluser:iuser {public user Getu
Ser () {return null;
public bool Saveuser () {return false;
}///<summary>///specific products related to SQL Server employee///</summary> public class Sqlemployee:iemployee {
Public Sqlemployee () {} public Employee GetEmployee () {return null;
public bool Saveemployee () {return false; }///<summary>///specific products related to access user///</summary> public class Accessuser:iuser {public Ac
Cessuser () {} public User GetUser () {return null;
public bool Saveuser () {return false;
}///<summary>///specific products related to access employee///</summary> public class Accessemployee:iemployee {
Public Accessemployee () {} public Employee GetEmployee () {return null;
public bool Saveemployee () {return false;
}
}
4. Model Summary
4.1 Advantages
4.1.1 specific products are separated from the customer code
4.1.2 Easy to change product series (e.g. SQL Server product family, access product family)
4.1.3 unifies a family of product families together to create
4.2 Disadvantages
It is difficult to extend a new product in a product family, it requires modifying the interface of an abstract factory, such as adding a product customer becomes very difficult.
4.3 Practical range
4.3.1 a system to be independent of the creation, composition, and presentation of its products.
4.3.2 A system is to be configured by one of several product families.
4.3.3 When you want to emphasize the design of a series of related product objects for joint use.
4.3.4 when you provide a Product class library and just want to show their interfaces instead of implementing them.
The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.