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

    简单工厂模式在种类较少的时候基本上可以满足了,但是缺点很明显,就是当你需要新增一种类型时,需要修改工厂的静态方法,以满足需求。如果需求一边多,这种设计带来的就是无尽的修改。比如,当前的工厂只能生产奔驰、宝马两种车,但是如果想新生产车的话,就不得不新增新种类车的生产线。

    工厂方法模式,可以规避掉这种问题,工厂方法的根本其实就是把生产线(用来产车的真正逻辑)交出去,他的办法就是我只给你提供可以支撑生产线的地方,你要来产奔驰车,那你就自己带上奔驰生产线,意味着所有想用我场地生产汽车的,你自己的生产线做的大小跟我一样,我的场地才能放的下你的生产线,这算是一种约束。

    通过上面的梳理,我们涉及到的对象其实就是四类:

    (1)抽象的工厂:只提供可以安放生产线的地方;

    (2)具体的工厂:可以实际生产具体产品的生产线(如:奔驰车生产线可以生产奔驰);

    (3)抽象的车:所有车的一个抽象模型,如本例中“车”的概念;

    (4)具体的车:是车的一种实现,是一个具体的被具体的生产线生产下来。

     理解了上述本质,我们知道,你要什么样的车,就得对应这么一个对应的生产线,这个生产线还要能放到我的工厂里(满足接口约束),不然没法生产。

    具体实现可看下面代码(C#):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace codesniped
    {
        /// <summary>
        /// 车工厂接口
        /// </summary>
        public interface ICarFactory
        {
            ICar CreateCar();
        }
        /// <summary>
        /// 车接口
        /// </summary>
        public interface ICar
        {
            void run();
        }
        /// <summary>
        /// 奔驰具体车,继承che接口
        /// </summary>
        public class BenzCar:ICar
        {
            public void run()
            {
                Console.WriteLine("Benz Car is Running...");
            }
        }
        /// <summary>
        /// 宝马具体车,继承车接口
        /// </summary>
        public class BmwCar:ICar
        {
            public void run()
            {
                Console.WriteLine("Bmw Car is Running...");
            }
        }
        /// <summary>
        /// 奔驰车工厂
        /// </summary>
        public class BenzCarFactory : ICarFactory
        {
            public ICar CreateCar()
            {
                return new BenzCar();
            }
        }
        /// <summary>
        /// 宝马车工厂
        /// </summary>
        public class BmwCarFactory:ICarFactory
        {
            public ICar CreateCar()
            {
                return new BmwCar();
            }
        }
    }
     
    测试代码如下:
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace codesniped
    {
        class Program
        {
            static void Main(string[] args)
            {
                ICarFactory carFactory = new BmwCarFactory(); //要什么车,先安装对应的生产线
                ICar myCar = carFactory.CreateCar();//利用生产线,产车
                myCar.run();
                Console.ReadKey();
            }
        }
    }
    通过上述我们可分析出,如果我想要台大众车,那么我就必须需要有条大众的生产线才行。因为大众车的生产在大众车的生产线上产出来。
     
  • 相关阅读:
    'Undefined symbols for architecture i386,clang: error: linker command failed with exit code 1
    The codesign tool requires there only be one 解决办法
    XCode iOS project only shows “My Mac 64bit” but not simulator or device
    Provisioning profile XXXX can't be found 的解决办法
    UIView 中的控件事件穿透 Passthrough 的实现
    Xcode4.5出现时的OC新语法
    xcode 快捷键(持续更新)
    打越狱包
    php缓存与加速分析与汇总
    浏览器的判断
  • 原文地址:https://www.cnblogs.com/chyshx/p/10498840.html
Copyright © 2011-2022 走看看