gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:32:49 25 4
gpt4 key购买 nike

我在阅读许多关于 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();
}
}

}

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

还有,它到底给我带来了什么好处。我可以使用接口(interface)以通常的非抽象工厂方式来完成它,并且整个事情仍然可以正常工作(设计明智)。

详细的解释和例子就是我要找的

最佳答案

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

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

关于c# - C# 中抽象工厂设计模式的真实世界示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51149700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com