工厂方法模式

工厂方法是创造性设计模式之一。它用于处理创建对象的问题,而无需指定确切的结果类型。本文档将教你如何正确使用 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 闪烁着灯说再见!