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

posted @ 2008-02-23 22:58  麦机长  阅读(850)  评论(0编辑  收藏  举报