学习随笔(设计模式:工厂方法模式)

发布时间 2023-11-16 18:02:16作者: 大耳朵tutwo

内容

今天学习了工厂方法模式,之前也学习了简单工厂模式。工厂方法模式是对简单工厂模式的抽象和升级。

收获

1.工厂方法模式延续了简单工厂模式对复杂创建过程的封装的优点,同时克服了简单工厂违背开放-封闭原则的缺点。
2.采用了多态的思想,将具体需要创建的对象交给了上层来决定,从而不需要修改自己内部的代码逻辑。
3.简单工厂:是一个工厂产所有类型
工厂方法模式:是一个工厂接口,生成很多工厂,每个工厂生产自己的类型

感悟

1.当我们需要创建工厂类创建对象时,先抽象出接口设计工厂创建接口
2.然后根据这个接口生成各种工厂类,这些工厂类重新实现工厂创建接口
3.用户需要构造新对象时,先构建一个工厂基类,然后指向具体想创建的类,然后创建该类对象
4.当需要扩展时,只需要创建新的类就行了,但是也额外增加了开发量(说是后续可以采用反射技术),虽然这样做也麻烦,但是比简单工厂好在,无需对类内代码再做修改了

示例代码

//Device.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryDesignMode
{
    public class BasicDevice
    {
        public string? DevId { get; set; }                   
        public string? DevName { get; set; }                 
        public string? DevType { get; set; }                 
        public string? DevModel { get; set; }
        public string? IpAddr { get; set; }
        public string? Manufacturer { get; set; }            
        public string? FactoryContactPerson { get; set; }    
        public string? FactoryContactPhone { get; set; }     
               

        public virtual bool PrintDeviceInfo()
        {
            Console.WriteLine("============== 设备基础信息 ===============");
            Console.WriteLine("设备编号:" + DevId);
            Console.WriteLine("设备名称:" + DevName);
            Console.WriteLine("设备类型:" + DevType);
            Console.WriteLine("设备型号:" + DevModel);
            Console.WriteLine("IP地址:" + IpAddr);
            Console.WriteLine("生产厂商:" + Manufacturer);
            Console.WriteLine("厂商联系人:" + FactoryContactPerson);
            Console.WriteLine("厂商联系方式:" + FactoryContactPhone);
            Console.WriteLine("===========================================");

            return true;
        }

        public bool LoadDeviceInfo(string TmpDevId, string TmpDevName, string TmpDevType, string TmpDevModel,
                                   string TmpIpAddr, string TmpManufacturer, string TmpFactoryContactPerson,
                                   string TmpFactoryContactPhone)
        {
            this.DevId = TmpDevId;
            this.DevName = TmpDevName;  
            this.DevType = TmpDevType;
            this.IpAddr = IpAddr;
            this.Manufacturer = Manufacturer;
            this.FactoryContactPerson = FactoryContactPerson;
            this.FactoryContactPhone = FactoryContactPhone;

            Console.WriteLine("BasicDevice:设备加载基础信息成功");

            return true;
        }
    }

    public class MachineDevice : BasicDevice
    { 
        public string? MajorAxleType { get; set; }
        public string? MajorAxlePower { get; set; }
        public string? MajorAxleTorqu { get; set; }
        public string? MajorAxleSpeed { get; set; }

        public override bool PrintDeviceInfo()
        {
            Console.WriteLine("============== 机床设备信息 ===============");
            Console.WriteLine("主轴类型:" + MajorAxleType);
            Console.WriteLine("主轴功率:" + MajorAxlePower);
            Console.WriteLine("主轴扭矩:" + MajorAxleTorqu);
            Console.WriteLine("主轴转速:" + MajorAxleSpeed);
            Console.WriteLine("===========================================");

            return base.PrintDeviceInfo();
        }

        public bool LoadDeviceInfo(string TmpDevId, string TmpDevName, string TmpDevType, string TmpDevModel,
                                   string TmpIpAddr, string TmpManufacturer, string TmpFactoryContactPerson,
                                   string TmpFactoryContactPhone, string TmpMajorAxleType, string TmpMajorAxlePower,
                                   string TmpMajorAxleTorqu, string TmpMajorAxleSpeed)
        {
            this.DevId = TmpDevId;
            this.DevName = TmpDevName;
            this.DevType = TmpDevType;
            this.IpAddr = IpAddr;
            this.Manufacturer = Manufacturer;
            this.FactoryContactPerson = FactoryContactPerson;
            this.FactoryContactPhone = FactoryContactPhone;
            this.MajorAxleType = TmpMajorAxleType;
            this.MajorAxlePower = TmpMajorAxlePower;
            this.MajorAxleTorqu = TmpMajorAxleTorqu;
            this.MajorAxleSpeed = TmpMajorAxleSpeed;

            Console.WriteLine("MachineDevice:机床设备加载信息成功");

            return true;
        }
    }

    public class DetectDevice : BasicDevice
    {
        public string? CheckFilePath { get; set; }

        public override bool PrintDeviceInfo()
        {
            Console.WriteLine("============== 检测设备信息 ===============");
            Console.WriteLine("检测文件路径:" + CheckFilePath);
            Console.WriteLine("===========================================");

            return base.PrintDeviceInfo();
        }
    }

    public class RackDevice : BasicDevice
    {
        public string? Row { get; set; }
        public string? Col { get; set; }

        public override bool PrintDeviceInfo()
        {
            Console.WriteLine("============== 料库设备信息 ===============");
            Console.WriteLine("行:" + Row);
            Console.WriteLine("列:" + Col);
            Console.WriteLine("===========================================");

            return base.PrintDeviceInfo();
        }
    }

    public class TransportDevice : BasicDevice
    {
        public string? GripCount { get; set; }

        public override bool PrintDeviceInfo()
        {
            Console.WriteLine("============== 运输设备信息 ===============");
            Console.WriteLine("夹爪个数:" + GripCount);

            return base.PrintDeviceInfo();
        }
    }

    //工厂接口
    interface IFactory
    {
        BasicDevice CreateDevice();
    }

    //机床设备工厂
    class MachineDeviceFactory : IFactory
    {
        public BasicDevice CreateDevice()
        {
            return new MachineDevice();
        }
    }

    //检测设备工厂
    class DetectDeviceFactory : IFactory
    {
        public BasicDevice CreateDevice()
        {
            return new DetectDevice();
        }
    }

    //料库设备工厂
    class RackDeviceFactory : IFactory
    {
        public BasicDevice CreateDevice()
        {
            return new RackDevice();
        }
    }

    //运输设备工厂
    class TransportDeviceFactory : IFactory
    {
        public BasicDevice CreateDevice()
        {
            return new TransportDevice();
        }
    }

    //其余设备工厂
    class BasicDeviceFactory : IFactory
    {
        public BasicDevice CreateDevice()
        {
            return new BasicDevice();
        }
    }
}
// Program.cs
using FactoryDesignMode;

List<BasicDevice> DeviceList = new List<BasicDevice>();

IFactory Machinefactory = new MachineDeviceFactory();
DeviceList.Add(Machinefactory.CreateDevice());             //添加机床设备

IFactory Detectfactory = new DetectDeviceFactory();
DeviceList.Add(Detectfactory.CreateDevice());              //添加检测设备

IFactory Rackfactory = new RackDeviceFactory();
DeviceList.Add(Rackfactory.CreateDevice());                //添加料库设备

IFactory Transportfactory = new RackDeviceFactory();
DeviceList.Add(Transportfactory.CreateDevice());           //添加运输设备

foreach (BasicDevice TmpDev in DeviceList)
{
    TmpDev.PrintDeviceInfo();
}