工廠方法模式

工廠方法是創造性設計模式之一。它用於處理建立物件的問題,而無需指定確切的結果型別。本文件將教你如何正確使用 Factory Method DP。

讓我通過一個簡單的例子向你解釋它的想法。想象一下,你在一家生產三種裝置的工廠工作 - 電流表,電壓表和電阻表。你正在為將要建立所選裝置的中央計算機編寫程式,但你不知道你的老闆最終決定要生成什麼。

讓我們建立一個介面 IDevice,其中包含所有裝置具有的一些常用功能:

public interface IDevice
{
    int Measure();
    void TurnOff();
    void TurnOn();
}

現在,我們可以建立代表我們裝置的類。這些類必須實現 IDevice 介面:

public class AmMeter : IDevice
{
    private Random r = null;
    public AmMeter()
    {
        r = new Random();
    }
    public int Measure() { return r.Next(-25, 60); }
    public void TurnOff() { Console.WriteLine("AmMeter flashes lights saying good bye!"); }
    public void TurnOn() { Console.WriteLine("AmMeter turns on..."); }
}
public class OhmMeter : IDevice
{
    private Random r = null;
    public OhmMeter()
    {
        r = new Random();
    }
    public int Measure() { return r.Next(0, 1000000); }
    public void TurnOff() { Console.WriteLine("OhmMeter flashes lights saying good bye!"); }
    public void TurnOn() { Console.WriteLine("OhmMeter turns on..."); }
}
public class VoltMeter : IDevice
{
    private Random r = null;
    public VoltMeter()
    {
        r = new Random();
    }
    public int Measure() { return r.Next(-230, 230); }
    public void TurnOff() { Console.WriteLine("VoltMeter flashes lights saying good bye!"); }
    public void TurnOn() { Console.WriteLine("VoltMeter turns on..."); }
}

現在我們必須定義工廠方法。讓我們用靜態方法建立 DeviceFactory 類:

public enum Device
{
    AM,
    VOLT,
    OHM
}
public class DeviceFactory
{
    public static IDevice CreateDevice(Device d)
    {
        switch(d)
        {
            case Device.AM: return new AmMeter();
            case Device.VOLT: return new VoltMeter();
            case Device.OHM: return new OhmMeter();
            default: return new AmMeter();
        }
    }
}

大! 讓我們測試一下我們的程式碼:

public class Program
{
    static void Main(string[] args)
    {
        IDevice device = DeviceFactory.CreateDevice(Device.AM);
        device.TurnOn();
        Console.WriteLine(device.Measure());
        Console.WriteLine(device.Measure());
        Console.WriteLine(device.Measure());
        Console.WriteLine(device.Measure());
        Console.WriteLine(device.Measure());
        device.TurnOff();
        Console.WriteLine();

        device = DeviceFactory.CreateDevice(Device.VOLT);
        device.TurnOn();
        Console.WriteLine(device.Measure());
        Console.WriteLine(device.Measure());
        Console.WriteLine(device.Measure());
        Console.WriteLine(device.Measure());
        Console.WriteLine(device.Measure());
        device.TurnOff();
        Console.WriteLine();

        device = DeviceFactory.CreateDevice(Device.OHM);
        device.TurnOn();
        Console.WriteLine(device.Measure());
        Console.WriteLine(device.Measure());
        Console.WriteLine(device.Measure());
        Console.WriteLine(device.Measure());
        Console.WriteLine(device.Measure());
        device.TurnOff();
        Console.WriteLine();
    }
}

這是執行此程式碼後可能看到的示例輸出:

AmMeter 開啟……

36

6

33

43

24

AmMeter 閃爍的燈光說再見!

VoltMeter 開啟……

102

-61

85

138

36

VoltMeter 閃爍燈光說再見!

OhmMeter 開啟……

723828

368536

685412

800266

578595

OhmMeter 閃爍著燈說再見!