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

ank*_*kur 2 c# design-patterns factory-pattern abstract-factory

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

下面是它的示例代码

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();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

现在,如何理解它。我可以理解它是如何工作的,但它看起来很天真。一件事是,也许教程网站上每个人都选择的示例并不正确。

另外,它真正给我带来了什么好处。我可以使用接口以通常的非抽象工厂方式来完成它,整个事情仍然可以正常工作(设计方面)。

我正在寻找带有示例的详细解释

小智 7

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

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

    public IWifiCommFactory InitializeWiFiCommnucation()
    {
        return new WiFiCommunication();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

/* CONSOLE OUTPUT */  
Bluetooth Communication was initialized
WIFI (generic) Communication was initialized
Run Code Online (Sandbox Code Playgroud)

接下来,我们要添加另一个要求来创建具有蓝牙低功耗类型和一些特定 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");
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我们创建了 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");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

最后是客户端代码:

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

baseFactory = new ProductPrototypeTWO(BluetoothType.BLE_TYPE, WiFiType.HIGH_BAND);
baseFactory.InitializeBluetoothCommunication();
baseFactory.InitializeWiFiCommnucation();
Run Code Online (Sandbox Code Playgroud)

这就是输出中的内容:

Bluetooth Communication was initialized
WIFI (generic) Communication was initialized
Bluetooth Low Energy Communication was initialized
WIFI High Band Communication was initialized
Run Code Online (Sandbox Code Playgroud)