Shenzhen Ka Wah advanced road _14 _ architect abstract factory

I. Introduction

Abstract Factory (Abstract Factory Pattern) is to create a super other factories around the plant. The plant is also known as super other factories factory. This type of design pattern belongs create schema, which provides the best way to create objects.

In the abstract factory pattern, the interface is responsible for creating a related object factory, you do not need to explicitly specify their class. Each generated in accordance with the object factory can provide factory model.

1, Abstract Factory Details

Here is first example of a life in the abstract factory to implement an abstract factory, and then give the definition of UML diagrams and abstract factory to help us better understand the abstract factory pattern, while everyone in the understanding of the abstract factory can control life in an example of implementation and its definition to deepen understanding of the abstract factory UML FIG.

2, the specific implementation of the abstract factory

Here's an example of life "must taste" chain to implement an abstract factory pattern. For example, must taste duck neck in Nanchang, Jiangxi and Shanghai would like to open stores, but due to local tastes are not the same, all things must taste in Nanchang will do a little spicy, and Shanghai do not like spicy food, so all of Shanghai must taste of things are not done as spicy as Nanchang, however, this leads to different factories must taste Nanchang and Shanghai must taste the factory generates all products must taste different, which is responsible for a specific range of products factory (refers to all food must taste) to create work, the following specific look at how to use the abstract factory pattern to achieve this.

/// <summary>
/// 下面以绝味鸭脖连锁店为例子演示下抽象工厂模式
/// 因为每个地方的喜欢的口味不一样,有些地方喜欢辣点的,有些地方喜欢吃不辣点
/// 客户端调用
/// </summary>
class Client
{
    static void Main(string[] args)
    {
        // 南昌工厂制作南昌的鸭脖和鸭架
        AbstractFactory nanChangFactory = new NanChangFactory();
        YaBo nanChangYabo = nanChangFactory.CreateYaBo();
        nanChangYabo.Print();
        YaJia nanChangYajia= nanChangFactory.CreateYaJia();
        nanChangYajia.Print();
        // 上海工厂制作上海的鸭脖和鸭架
        AbstractFactory shangHaiFactory = new ShangHaiFactory();
        shangHaiFactory.CreateYaBo().Print();
        shangHaiFactory.CreateYaJia().Print();
        Console.Read();
    }

}

/// <summary>
/// 抽象工厂类,提供创建两个不同地方的鸭架和鸭脖的接口
/// </summary>
public abstract class AbstractFactory
{
    // 抽象工厂提供创建一系列产品的接口,这里作为例子,只给出了绝味中鸭脖和鸭架的创建接口
    public abstract YaBo CreateYaBo();
    public abstract YaJia CreateYaJia();
}

/// <summary>
/// 南昌绝味工厂负责制作南昌的鸭脖和鸭架
/// </summary>
public class NanChangFactory : AbstractFactory
{
    // 制作南昌鸭脖
    public override YaBo CreateYaBo()
    {
        return new NanChangYaBo();
    }

    // 制作南昌鸭架
    public override YaJia CreateYaJia()
    {
        return new NanChangYaJia();
    }
}

/// <summary>
/// 上海绝味工厂负责制作上海的鸭脖和鸭架
/// </summary>
public class ShangHaiFactory : AbstractFactory
{

    // 制作上海鸭脖
    public override YaBo CreateYaBo()
    {
        return new ShangHaiYaBo();
    }
    // 制作上海鸭架
    public override YaJia CreateYaJia()
    {
        return new ShangHaiYaJia();
    }
}

/// <summary>
/// 鸭脖抽象类,供每个地方的鸭脖类继承
/// </summary>
public abstract class YaBo
{
    /// <summary>
    /// 打印方法,用于输出信息
    /// </summary>
    public abstract void Print();
}

/// <summary>
/// 鸭架抽象类,供每个地方的鸭架类继承
/// </summary>
public abstract class YaJia
{
    /// <summary>
    /// 打印方法,用于输出信息
    /// </summary>
    public abstract void Print();
}

/// <summary>
/// 南昌的鸭脖类,因为江西人喜欢吃辣的,所以南昌的鸭脖稍微会比上海做的辣
/// </summary>
public class NanChangYaBo : YaBo
{
    public override void Print()
    {
        Console.WriteLine("南昌的鸭脖");
    }
}

/// <summary>
/// 上海的鸭脖没有南昌的鸭脖做的辣
/// </summary>
public class ShangHaiYaBo : YaBo
{
    public override void Print()
    
        Console.WriteLine("上海的鸭脖");
    }
}

/// <summary>
/// 南昌的鸭架
/// </summary>
public class NanChangYaJia : YaJia
{
    public override void Print()
    {
        Console.WriteLine("南昌的鸭架子");
    }
}
/// <summary>
/// 上海的鸭架
/// </summary>
public class ShangHaiYaJia : YaJia
{
    public override void Print()
    {
        Console.WriteLine("上海的鸭架子");
    }
}

The above code has detailed notes, is no longer here to explain the above code, the following specific look at the definition of the abstract factory pattern it (understand the definition above reference implementation can deepen understanding):
Here Insert Picture Description
Abstract Factory pattern: create a interface products responsible for creating objects related to or dependent, without specifying explicitly specify a specific class

Abstract Factory allows customers to use an abstract interface to create a group of related products, without the need to know or care what the actual production of specific products yes. So customers can be decoupled from specific products. Below to understand the relationship between the various classes by the class diagram of an abstract model work:

3, abstract factory respond to changing requirements

After reading the abstract factory to achieve the above, if the "must taste" the company wants to open a branch in Hunan how to do it? Because people like to eat spicy Hunan, the following specific look at how the system application abstract factory pattern is to address this need.

/// <summary>
/// 如果绝味又想开一家湖南的分店时,因为湖南喜欢吃麻的
/// 所以这是有需要有一家湖南的工厂专门制作
/// </summary>
public class HuNanFactory : AbstractFactory
{
    // 制作湖南鸭脖
    public override YaBo CreateYaBo()
    {
        return new HuNanYaBo();
    }

    // 制作湖南鸭架
    public override YaJia CreateYaJia()
    {
        return new HuNanYajia();
    }
}

/// <summary>
/// 湖南的鸭脖
/// </summary>
public class HuNanYaBo : YaBo
{
    public override void Print()
    {
        Console.WriteLine("湖南的鸭脖");
    }
}

/// <summary>
/// 湖南的鸭架
/// </summary>
public class HuNanYajia : YaJia
{
    public override void Print()
    {
        Console.WriteLine("湖南的鸭架子");
    }
}

At this time, only need to add three categories: one is in Hunan concrete factory class is responsible for creating and Hunan flavor duck neck duck frame, the other two classes is a taste of Hunan duck duck neck classes and Racks. Seen from the above code, change in support for the Abstract Factory series of "open - closed" principle (refer to the requirements of the system is open to extension, closed for modification), very easy to extend, however, to add a new abstract factory for this product case do not support the "open - closed" principle, which is the disadvantage of the abstract factory of lies, and this will be described in detail in part IV.

Analysis of the abstract factory
abstract factory mode creates a delay specific to the product specific subclass of plant, which will create an object encapsulated, can reduce the dependency between the client and the specific product type, so that the coupling system is low, so more conducive to the maintenance and expansion of late, this is really where the advantages of the abstract factory pattern, abstract patterns and then there are also inadequate. Following specific look disadvantage of the abstract factory (in fact disadvantage to in the introduction has been involved):

Abstract factory pattern is difficult to support the changes in the new product range. This is because the abstract factory interface has identified a set of products that can be created, if you need to add a new product, then you have to modify the interface of the abstract factory, thus involves changing the abstract factory class and all subclasses, such it violated the "development - closed" principle.

Know the advantages and disadvantages of the abstract factory after, you can also consider using a good grasp of the abstract factory pattern under what circumstances, the following specific look at the system to use the abstract factory pattern should be consistent with the premise that a few:

The system does not require a class instance depend on how the product is created, a combination of expression and expression, provided that this point is all the factory model applications.

This system has a number of products, but a system in which only consumer products

The system asks for a product class library, all products appeared in the same interface, the client does not depend on the specific implementation.

4, .NET abstract factory pattern achieved

Abstract factory pattern in the practical application is quite frequent, but there are also applications like abstract factory pattern in our .NET class library, this class is System.Data.Common.DbProviderFactory, this class is in System.Data.dll program focus, to play the role of such abstract factory pattern in abstract factory, we can see the class implemented reflector decompiler:


/// 扮演抽象工厂的角色
/// 创建连接数据库时所需要的对象集合,
/// 这个对象集合包括有 DbConnection对象(这个是抽象产品类,如绝味例子中的YaBo类)、DbCommand类、DbDataAdapter类,针对不同的具体工厂都需要实现该抽象类中方法,
public abstract class DbProviderFactory
{
    // 提供了创建具体产品的接口方法
    protected DbProviderFactory();
    public virtual DbCommand CreateCommand();
    public virtual DbCommandBuilder CreateCommandBuilder();
    public virtual DbConnection CreateConnection();
    public virtual DbConnectionStringBuilder CreateConnectionStringBuilder();
    public virtual DbDataAdapter CreateDataAdapter();
    public virtual DbDataSourceEnumerator CreateDataSourceEnumerator();
    public virtual DbParameter CreateParameter();
    public virtual CodeAccessPermission CreatePermission(PermissionState state);
}

DbProviderFactory class is an abstract factory class that provides an interface to create database objects needed to connect the set, the actual creation of the work carried out in its sub-class factory, using a Microsoft SQL Server database, SQL Server provides a connection achieve specific plant data, the specific code may be decompiler view, specific code as follows:

// 扮演着具体工厂的角色,用来创建连接SQL Server数据所需要的对象
public sealed class SqlClientFactory : DbProviderFactory, IServiceProvider
{
    // Fields
    public static readonly SqlClientFactory Instance = new SqlClientFactory();
    // 构造函数
    private SqlClientFactory()
    {
    }    
    // 重写抽象工厂中的方法
    public override DbCommand CreateCommand()
    {  // 创建具体产品
        return new SqlCommand();
    }
    public override DbCommandBuilder CreateCommandBuilder()
    {
        return new SqlCommandBuilder();
    }
    public override DbConnection CreateConnection()
    {
        return new SqlConnection();
    }
    public override DbConnectionStringBuilder CreateConnectionStringBuilder()
    {
        return new SqlConnectionStringBuilder();
    }
    public override DbDataAdapter CreateDataAdapter()
    {
        return new SqlDataAdapter();
    }
    public override DbDataSourceEnumerator CreateDataSourceEnumerator()
    {
        return SqlDataSourceEnumerator.Instance;
    }
    public override DbParameter CreateParameter()
    {
        return new SqlParameter();
    }
    public override CodeAccessPermission CreatePermission(PermissionState state)
    {
        return new SqlClientPermission(state);
    }
}

Since Microsoft only gives a connection to SQL Server to achieve concrete factory, we can also customize connect Oracle, MySql achieve concrete plant.

Published 37 original articles · won praise 3 · Views 6306

Guess you like

Origin blog.csdn.net/huan13479195089/article/details/105130966