Здесь я продемонстрирую вам следующий простой пример, основанный на том, что я использовал для реализации в своем проекте давным-давно.
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
Далее мы хотим добавить еще одно требование для создания продукта с типом Bluetooth Low Energy и некоторыми конкретными типами 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