• 设计模式 Vs实践-2 抽象工厂模式


    参考:https://www.runoob.com/design-pattern/abstract-factory-pattern.html

    前言:抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

     在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。

    假如现在项目中已经有了4个类,要改成抽象工厂模式

    (一)现有类如下

    1、家具-furniture

    1.1、凳子(chair)

    1.2、桌子(desk)

    2、电气-electric

    2.1、电脑(computer)

    2.2、空调(air)

    使用vs2019实现,2010重构功能相对比较弱。

    抽象工厂直观理解就是工厂中又有工厂

     

    (二)从凳子桌子抽象furniture接口,电脑空调抽象electric接口

    (三)定义抽象类

    abstract class AbstractOfficeFactory
    {
        public abstract IElectric GetElectric(string type);
        public abstract IFurniture GetFurniture(string type);
    }

    (四)创建一个ElectricFactory类

    class ElectricFactory: AbstractOfficeFactory
    {
    
    }

    (五)在类上点击左键,点实现抽象类

     再进行修改,最终代码如下

    class ElectricFactory : AbstractOfficeFactory
        {
            public override IElectric GetElectric(string type)
            {
                if (type == null)
                {
                    return null;
                }
                if (type.Equals("Air"))
                {
                    return new Air();
                }
                else if (type.Equals("Computer"))
                {
                    return new Computer();
                }
                return null;
            }
    
            public override IFurniture GetFurniture(string type)
            {
                return null;
            }
        }

    同样创建FurnitureFactory

    class FurnitureFactory : AbstractOfficeFactory
        {
            public override IElectric GetElectric(string type)
            {
                return null;
            }
    
            public override IFurniture GetFurniture(string type)
            {
                if (type == null)
                {
                    return null;
                }
                if (type.Equals("Desk"))
                {
                    return new Desk();
                }
                else if (type.Equals("Chair"))
                {
                    return new Chair();
                }
                return null;
            }
        }

    (六)创建FactoryProducer(用于创建工厂的工厂类)

    class FactoryProducer
    {
        public static AbstractOfficeFactory GetFactory(string factoryType)
        {
            if (factoryType.Equals("Furniture"))
            {
                return new FurnitureFactory();
            }
            else if (factoryType.Equals("Electric"))
            {
                return new ElectricFactory();
            }
            return null;
        }
    }

    (七)测试代码如下

    //获取形状工厂
    AbstractOfficeFactory officeFactory = FactoryProducer.GetFactory("Electric");
    officeFactory.GetElectric("Air").ShowInfo();
    //officeFactory.GetFurniture("Chair").ShowInfo();
    Console.ReadLine();

    程序可以运行,但是有个问题,就是获取的Electric工厂也暴露有GetFurniture方法,但这个方法又没什么实质作用,又无法隐藏,也就是这种分类、这种场景不是很典型的抽象工厂场景。

    下面这个例子更合适,假如有公司G开头的和S开头的两个公司,获取他们的家具和电气对象

    S公司

    G公司

    1、家具-furniture

    1.1、凳子(chair)

    1.2、桌子(desk)

    2、电气-electric

    2.1、电脑(computer)

    2.2、空调(air)

    abstract class AbstractOfficeFactory
    {
        //type:G-公司,S-公司
        public abstract IElectric GetElectric(string type);
        public abstract IFurniture GetFurniture(string type);
    }
    class GAir : IElectric
    {
        public void ShowInfo()
        {
            Console.WriteLine("GAir");
        }
    }
    class SAir : IElectric
    {
        public void ShowInfo()
        {
            Console.WriteLine("SAir");
        }
    }
    class GChair : IFurniture
    {
        public void ShowInfo()
        {
            Console.WriteLine("GChair");
        }
    }
    class SChair : IFurniture
    {
        public void ShowInfo()
        {
            Console.WriteLine("SChair");
        }
    }
    class GComputer:IElectric
    {
        public void ShowInfo()
        {
            Console.WriteLine("GComputer");
        }
    }
    class SComputer : IElectric
    {
        public void ShowInfo()
        {
            Console.WriteLine("SComputer");
        }
    }
    public class GDesk : IFurniture
    {
        public void ShowInfo()
        {
            Console.WriteLine("GDesk");
        }
    }
    public class SDesk : IFurniture
    {
        public void ShowInfo()
        {
            Console.WriteLine("SDesk");
        }
    }
    class GFactory : AbstractOfficeFactory
    {
        public override IElectric GetElectric(string type)
        {
            if (type == null)
            {
                return null;
            }
            if (type.Equals("Air"))
            {
                return new GAir();
            }
            else if (type.Equals("Computer"))
            {
                return new GComputer();
            }
            return null;
        }
    
        public override IFurniture GetFurniture(string type)
        {
            if (type == null)
            {
                return null;
            }
            if (type.Equals("Desk"))
            {
                return new GDesk();
            }
            else if (type.Equals("Chair"))
            {
                return new GChair();
            }
            return null;
        }
    }
    class SFactory : AbstractOfficeFactory
    {
        public override IElectric GetElectric(string type)
        {
            if (type == null)
            {
                return null;
            }
            if (type.Equals("Air"))
            {
                return new SAir();
            }
            else if (type.Equals("Computer"))
            {
                return new SComputer();
            }
            return null;
        }
    
        public override IFurniture GetFurniture(string type)
        {
            if (type == null)
            {
                return null;
            }
            if (type.Equals("Desk"))
            {
                return new SDesk();
            }
            else if (type.Equals("Chair"))
            {
                return new SChair();
            }
            return null;
        }
    }
    class FactoryProducer
    {
        public static AbstractOfficeFactory GetFactory(string factoryType)
        {
            if (factoryType.Equals("G"))
            {
                return new GFactory();
            }
            else if (factoryType.Equals("S"))
            {
                return new SFactory();
            }
            return null;
        }
    }
    static void Main(string[] args)
    {
        
        AbstractOfficeFactory officeFactory = FactoryProducer.GetFactory("G");
        officeFactory.GetElectric("Air").ShowInfo();
        officeFactory.GetFurniture("Chair").ShowInfo();
        Console.ReadLine();
    }

    输出

    GAir
    GChair

  • 相关阅读:
    简单的Servlet结合Jsp实现请求和响应以及对doGet和doPost的浅析
    My1stServlet
    Myeclipse发布第一个jsp页面及web project部署到tomcat上的几种方法
    java中循环的不同终止方式
    Mybatis学习笔记
    python 进程间的数据交互
    python 进程
    python 队列
    python 多线程
    python paramiko 向linux执行命令和发送接收文件
  • 原文地址:https://www.cnblogs.com/zhaogaojian/p/12497080.html
Copyright © 2020-2023  润新知