zoukankan      html  css  js  c++  java
  • 工厂方法模式 和 抽象工厂模式

    1.简介

      相比于简单工厂,工厂方法是使用一个工厂类去创建一个对象

      IRace接口和Human类、NE类都和上文简单工厂一样

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace FactoryMethod
    {
        public interface IRace
        {
            void ShowKing();
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace FactoryMethod
    {
        class Human : IRace
        {
            public void ShowKing()
            {
                Console.WriteLine("这里是人类的国王");
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace FactoryMethod
    {
        class NE : IRace
        {
            public void ShowKing()
            {
                Console.WriteLine("这里是精灵族的国王");
            }
        }
    }

      然后我们添加一个Human工厂HumanFactory,用这个类来实例化Human

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace FactoryMethod
    {
        public class HumanFactory
        {
            public IRace CreateInstance()
            {
                return new Human();
            }
        }
    }

      同理添加一个NE工厂NEFactory

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace FactoryMethod
    {
        public class NEFactory:IFactory
        {
            public IRace CreateInstance()
            {
                Console.WriteLine("实例化之前NE执行一些操作");
                return new NE();
            }
        }
    }

      Program:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace FactoryMethod
    {
        class Program
        {
            static void Main(string[] args)
            {
                HumanFactory humanFactory = new HumanFactory();
                IRace race1 = humanFactory.CreateInstance();
                race1.ShowKing();
           //NE同理 Console.Read(); } } }

      从这里看,我们可能会觉得工厂方法只会增加代码量,没有什么实际意义,不过在某些场合还是非常适合的

      比如:我们需要在实例化之前执行一些操作

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace FactoryMethod
    {
        public class HumanFactory
        {
            public IRace CreateInstance()
            {
                Console.WriteLine("实例化之前Human执行一些操作");
                return new Human();
            }
        }
    }

    2.用简单工厂方法管理工厂方法的工厂类

      我们也可以结合简单工厂和工厂方法

      使用简单工厂方法对工厂类HumanFactory进行管理,对Program解耦

      添加一个接口

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace FactoryMethod
    {
        public interface IFactory
        {
            IRace CreateInstance();
        }
    }

      使HumanFactory和NEFactory继承这个接口

      然后添加一个简单工厂类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace FactoryMethod
    {
        public class SimpleFactory
        {
            public static IFactory GetFactory(FactoryType factoryType)
            {
                switch (factoryType)
                {
                    case FactoryType.HumanFactory: return new HumanFactory();
                    case FactoryType.NEFactory: return new NEFactory();
                    default: throw new Exception("wrong");
                }
            }
        }
        public enum FactoryType
        {
            HumanFactory, NEFactory
        }
    }

      Program:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace FactoryMethod
    {
        class Program
        {
            static void Main(string[] args)
            {
                IFactory factory1 = SimpleFactory.GetFactory(FactoryType.NEFactory);
                IRace race1= factory1.CreateInstance();
                race1.ShowKing();
    
                IFactory factory2 = SimpleFactory.GetFactory(FactoryType.HumanFactory);
                IRace race2 = factory2.CreateInstance();
                race2.ShowKing();
    
                Console.Read();
            }
        }
    }

      像这种把对多个工厂从细节代码变成抽象代码的方法,也叫做抽象工厂模式

      要进一步解耦,可以使用反射,请参考上一章

  • 相关阅读:
    AWS IoT Greengrass:配置安装 AWS CLI
    AWS IoT Greengrass:如何使用 AWS 管理控制台配置本地资源访问
    AWS IoT Greengrass 入门-模块6: 访问其他 AWS 服务
    AWS IoT Greengrass 入门-模块5:与设备影子交互
    AWS IoT Greengrass 入门-模块4:在 AWS IoT Greengrass 组中与设备交互
    AWS IoT Greengrass 入门-模块3(第 2 部分):AWS IoT Greengrass 上的 Lambda 函数
    Nginx 启动报错 (nginx: error while loading shared libraries: XXX: cannot open shared object file: No such file or directory ) 的解决办法
    自己制作一个简单的操作系统二[CherryOS]
    Linux-误删apt-get以及把aptitude换回
    完美解决phpstudy安装后mysql无法启动(无需删除原数据库,无需更改任何配置,无需更改端口)直接共存
  • 原文地址:https://www.cnblogs.com/wskxy/p/9234878.html
Copyright © 2011-2022 走看看