C#中的抽象工厂设计模式的真实示例

时间:2018-07-03 08:14:58

标签: c# design-patterns factory-pattern abstract-factory

在阅读了许多有关C#中的Abstract Factory Design Pattern的文章后,我正在写这个问题。我真的无法从所有这些文章提供的示例中了解现实中的用例。我所看到的只是汽车/计算机/电话等的一些基本示例。我知道它们对于提供简单的解释很重要。但是我真的无法将其映射到任何现实世界,因为如果我真的遵循这样的示例,那么当我想引入新对象时,我的代码将每隔一周更改一次。

下面是它的示例代码

namespace ClassLibrary1
{

    public interface AbstractProductA { }
    public class ProductA1 : AbstractProductA { }
    public class ProductA2 : AbstractProductA { }


    public interface AbstractProductB { }
    public class ProductB1 : AbstractProductB { }
    public class ProductB2 : AbstractProductB { }


    public interface AbstractFactory
    {
        AbstractProductA CreateProductA();
        AbstractProductB CreateProductB();
    }


    public class ConcreteFactoryA : AbstractFactory
    {
        public AbstractProductA CreateProductA()
        {
            return new ProductA1();
        }

        public AbstractProductB CreateProductB()
        {
            return new ProductB1();
        }
    }

    public class ConcreteFactoryB : AbstractFactory
    {
        public AbstractProductA CreateProductA()
        {
            return new ProductA2();
        }

        public AbstractProductB CreateProductB()
        {
            return new ProductB2();
        }
    }

    public class Client
    {
        private AbstractProductA _productA;
        private AbstractProductB _productB;

        public Client(AbstractFactory factory)
        {
            _productA = factory.CreateProductA();
            _productB = factory.CreateProductB();
        }
    }

}

现在,如何理解它。我能理解它是如何工作的,但是看起来太幼稚了。一件事是,教程站点上每个人选择的示例可能都不正确。

而且,它真正给我带来了什么好处。我可以使用接口以通常的非抽象工厂方式进行操作,整个过程仍然可以正常工作(在设计方面)。

示例的详细解释是我要寻找的

1 个答案:

答案 0 :(得分:2)

在这里,我根据很久以前在项目中实现的示例向您演示以下简单示例。

public interface IBluetoothCommFactory { }

public interface IWifiCommFactory { }

/// <summary>
/// This is our base factory
/// </summary>
public interface ICommunicationBaseFactory
{
    IBluetoothCommFactory InitializeBluetoothCommunication();
    IWifiCommFactory InitializeWiFiCommnucation();
}

public class BluetoothCommunication : IBluetoothCommFactory
{
    public BluetoothCommunication()
    {
        // Implement some init logic here...
        Console.WriteLine("Bluetooth Communication was initialized");
     }
}

public class WiFiCommunication : IWifiCommFactory
{
    public WiFiCommunication()
    {
        // Implement some init logic here...
        Console.WriteLine("WIFI (generic) Communication was initialized");
    }
}

以上是“通用” BluetoothCommunication 和“通用” WiFiCommunication 的基本实现。假设我们要制造一个使用这些通用通信类型的产品 ProductPrototypeONE

public class ProductPrototypeONE : ICommunicationBaseFactory
{
    public IBluetoothCommFactory InitializeBluetoothCommunication()
    {
        return new BluetoothCommunication();
    }

    public IWifiCommFactory InitializeWiFiCommnucation()
    {
        return new WiFiCommunication();
    }
}

然后在我们的客户代码中:

ICommunicationBaseFactory baseFactory = new ProductPrototypeONE();
baseFactory.InitializeBluetoothCommunication();
baseFactory.InitializeWiFiCommnucation();

/* CONSOLE OUTPUT */  
Bluetooth Communication was initialized
WIFI (generic) Communication was initialized

接下来,我们要添加另一个要求以创建具有低功耗蓝牙类型和某些特定WiFi类型的产品。然后,使用我们创建的同一工厂,我们可以继续实现这些目标:

public class BluetoothLowEnergyCommunication : IBluetoothCommFactory
{
    public BluetoothLowEnergyCommunication()
    {
        // Implement some init logic here...
        Console.WriteLine("Bluetooth Low Energy Communication was initialized");
    }
}

public class WiFiLowBandCommunication : IWifiCommFactory
{
    public WiFiLowBandCommunication()
    {
        // Implement some init logic here...
        Console.WriteLine("WIFI Low Band Communication was initialized");
    }
}

public class WifiHighBandCommunication : IWifiCommFactory
{
    public WifiHighBandCommunication()
    {
        // Implement some init logic here...
        Console.WriteLine("WIFI High Band Communication was initialized");
    }
}

然后我们创建了ProductPrototypeTWO作为具体的类:

public enum BluetoothType
{
    CLASSIC_TYPE,
    BLE_TYPE
}

public enum WiFiType
{
    LOW_BAND,
    HIGH_BAND
}

public class ProductPrototypeTWO : ICommunicationBaseFactory
{
    private BluetoothType _bluetoothType;
    private WiFiType _wifiType;

    public ProductPrototypeTWO(BluetoothType bluetoothType, WiFiType wifiType)
    {
        _bluetoothType = bluetoothType;
        _wifiType = wifiType;
    }

    public IBluetoothCommFactory InitializeBluetoothCommunication()
    {
        switch (_bluetoothType)
        {
            case BluetoothType.CLASSIC_TYPE:
                return new BluetoothCommunication();
            case BluetoothType.BLE_TYPE:
                return new BluetoothLowEnergyCommunication();
            default:
                throw new NotSupportedException("Unknown Bluetooth type");
        }
    }

    public IWifiCommFactory InitializeWiFiCommnucation()
    {
        switch (_wifiType)
        {
            case WiFiType.LOW_BAND:
                return new WiFiLowBandCommunication();
            case WiFiType.HIGH_BAND:
                return new WifiHighBandCommunication();
            default:
                throw new NotSupportedException("Unknown WIFI type");
        }
    }
}

最后,客户代码:

ICommunicationBaseFactory baseFactory = new ProductPrototypeONE();
baseFactory.InitializeBluetoothCommunication();
baseFactory.InitializeWiFiCommnucation();

baseFactory = new ProductPrototypeTWO(BluetoothType.BLE_TYPE, WiFiType.HIGH_BAND);
baseFactory.InitializeBluetoothCommunication();
baseFactory.InitializeWiFiCommnucation();

这就是输出中的内容:

Bluetooth Communication was initialized
WIFI (generic) Communication was initialized
Bluetooth Low Energy Communication was initialized
WIFI High Band Communication was initialized