学习随笔(设计模式:简单工厂模式)

内容:

今天学习了设计模式中的简单工厂模式。

收获:

1.将所有相同类似的对象抽象,然后获得一个基类
2.再根据该基类封装成不同的派生类
3.当创建对象时,该对象可能是基类的任意一个派生类对象,那么到底是哪一个派生类呢?此时就可以通过工厂模式,用一个单独的类来做这个创造实例的过程,这就是工厂

感悟:

就拿熟悉的自动化产线项目来说,我们熟悉的产线中有很多不同类型的设备,包括:机床设备、检测设备、清洗设备、定位设备、料库设备等等,那么我们就可以套用今天学习的工厂模式。
1.先封装所有设备类型的基类,也就是一个基础设备类。
2.再根据基础设备类,派生出不同的类,包括:机床设备类、检测设备类、清洗设备类、定位设备类、料库设备类等等。
3.当在软件中添加一个新的设备对象时,有可能是任意设备类中的一种,所以设计一个单独的工厂类,这个类就是用来实例化这个新增的设备对象,根据传参来决定创建哪种设备类型。
4.基础设备类中应该包括:设备的基础信息(设备编号、设备型号、设备厂商等等),派生类中应该包含每个各种设备类型独有的一些数据,机床设备类中一些加工的参数,主轴信息等,料库设备有库位信息等。当然除了一些属性外,应该还有一些方法的抽象。

示例代码:

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();
        }
    }

    public class DeviceFactory
    { 
        public BasicDevice CreateNewDevice(string DeviceType)
        {
            BasicDevice NewDevice = null;

            switch (DeviceType)
            {
                case "机床设备": NewDevice = new MachineDevice();break;
                case "检测设备": NewDevice = new DetectDevice();break;
                case "料库设备": NewDevice = new RackDevice();break;
                case "运输设备": NewDevice = new TransportDevice();break;
                case "其它设备": NewDevice = new BasicDevice();break;
            }

            return NewDevice;
        }
    }
}

1.代码中设计了BasicDevice基类,又根据基类生成了MachineDevice、DetectDevice、RackDevice、TransportDevice等几个派生类,通过构造简单工厂类DeviceFactory,并封装了CreateNewDevice来创建不同类型的设备。
2.降低使用该设备类的客户编写代码的复杂性,使其只需调用CreateNewDevice方法,就可以脱离内部复杂的设备创建流程。

posted @ 2023-11-10 21:57  大耳朵tutwo  阅读(19)  评论(0)    收藏  举报