zoukankan      html  css  js  c++  java
  • 4.10 Template Method(模板方法)

    【返回目录】

    大家一定可以想想制造行业里的生产流水线的样子,例如宝马公司的汽车生产流水线,大体上从原料加工到零件组装到喷漆再到上线测试,一辆汽车就算生产完了。这样的流水线作业大大提高了汽车的生产速度和效率,并且质量也更加可靠。

    那么波音公司生产飞机又是怎样一个流程呢?其实大体上也是从原料加工(假设把给波音提供部分零件的公司也看作是这条生产线上的一部分)到零件组装到喷漆再到上线测试,一架飞机也就算是生产完成了。但是,我们就能说生产飞机和生产汽车是一样的吗?显然不能。

    但是虽然生产出的产品不一样,不过生产的流程却有共同之处。例如大体流程都是原料→零件→喷漆→测试,只是在具体到各个环节的细节上有差别,例如飞机不需要生产转弯灯、汽车大多是纯色而飞机在喷漆的时候就会喷上相应航空公司的纹理和标志、而且飞机所要经过的测试环节比汽车要多得多等等。

    这样就给我们一些启发,作为一名程序员,一旦发现有任何共同的地方,首先应该想到的就是抽象。对,把这些共同的方法抽取出来,放到Produce这个抽象类里面,而继承自这个类的CarProducer和AirplaneProduce类则分别根据自己的情况实现具体方法中的细节,只要保持流水线作业的方式不变就行了。

       1: using System;
       2:  
       3: namespace Autumoon.DesignPatterns.Template
       4: {
       5:     public class Product
       6:     {
       7:         public string ColorName { get; set; }
       8:     }
       9:  
      10:     public class Bus : Product
      11:     {
      12:         public int WheelAmount { get; set; }
      13:     }
      14:  
      15:     public class Airplane : Product
      16:     {
      17:         public int ParachuteAmount { get; set; }
      18:     }
      19:  
      20:     abstract public class Produce
      21:     {
      22:         protected Product CurrentProduct { get; set; }
      23:  
      24:         abstract protected void Setup();
      25:         abstract protected void Paint();
      26:         abstract protected void Test();
      27:  
      28:         public Product CreateProduct()
      29:         {
      30:             this.Setup();
      31:             this.Paint();
      32:             this.Test();
      33:  
      34:             return this.CurrentProduct;
      35:         }
      36:     }
      37:  
      38:     public class BusProducer : Produce
      39:     {
      40:         protected override void Setup()
      41:         {
      42:             this.CurrentProduct = new Bus();
      43:             ((Bus)this.CurrentProduct).WheelAmount = 4;
      44:         }
      45:  
      46:         protected override void Paint()
      47:         {
      48:             this.CurrentProduct.ColorName = "Gray";
      49:         }
      50:  
      51:         protected override void Test()
      52:         {
      53:             Console.WriteLine("Test running.");
      54:         }
      55:     }
      56:  
      57:     public class AirplaneProducer : Produce
      58:     {
      59:         protected override void Setup()
      60:         {
      61:             this.CurrentProduct = new Airplane();
      62:             ((Airplane)this.CurrentProduct).ParachuteAmount = 2;
      63:         }
      64:  
      65:         protected override void Paint()
      66:         {
      67:             this.CurrentProduct.ColorName = "Boeing";
      68:         }
      69:  
      70:         protected override void Test()
      71:         {
      72:             Console.WriteLine("Test flight.");
      73:         }
      74:     }
      75:  
      76: }

    有了这样先进的流水线,咱就使劲儿地造吧!

       1: static void Main(string[] args)
       2: {
       3:     #region Template
       4:     Bus benz = new BusProducer().CreateProduct() as Bus;
       5:     Airplane boeing = new AirplaneProducer().CreateProduct() as Airplane;
       6:     #endregion
       7:  
       8:     Console.ReadLine();
       9: }

    Boeing Manufacture

  • 相关阅读:
    博科SAN交换机基本配置(华为SNS系列交换机为例OEM博科)
    华为SNS交换机(OEM博科FC交换机)Fabric OS: v 8版本后通过https方式浏览器访问交换机Webtools显示没有匹配的加密算法套件的解决办法
    SNS光纤交换机怎样禁用 Virtual Fabric模式
    光纤网络的相关知识
    元音老人:怎样了生死
    戒淫偈【每天念十遍】。。。
    戒淫偈汇集
    净空老法师:你能这样念上一个星期试试看,肯定比你过去念十年还有效!
    修华严奥旨妄尽还原观 第6集
    贤公和尚,佛门榜样。-海贤老和尚往生纪实
  • 原文地址:https://www.cnblogs.com/Autumoon/p/1079088.html
Copyright © 2011-2022 走看看