close

新增一個抽象類別(範本方法),裡面定義驗算法的骨架TemplateMethod(),包含3個步驟proc1()、proc2()、proc3();這些步驟實作延遲至繼承的子類別去實作,增加了使用上的彈性。

   1: abstract class AbstractClass
   2: {
   3:     protected abstract void proc1();
   4:     protected abstract void proc2();
   5:     protected abstract void proc3();
   6:     public void TemplateMethod()
   7:     {
   8:         proc1();
   9:         proc2();
  10:         proc3();
  11:         MessageBox.Show("TemplateMethod");            
  12:     }
  13: }

image

ConcreteClassA和ConcreteClassB分別繼承AbstractClass,因此需要覆寫(override)剛剛父類別的抽象方法:包含3個步驟proc1()、proc2()、proc3();

ConcreteClassA: 1. 讀入影像 2.彩色轉灰階 3. 灰階轉黑白

   1: class ConcreteClassA : AbstractClass
   2:    {
   3:        protected override void proc1()
   4:        {
   5:            MessageBox.Show("具體A proc1=imread");
   6:            //throw new NotImplementedException();
   7:        }
   8:        protected override void proc2()
   9:        {
  10:            MessageBox.Show("具體A proc2=rgb2gray");
  11:            //throw new NotImplementedException();
  12:        }
  13:        protected override void proc3()
  14:        {
  15:            MessageBox.Show("具體A proc3=gray2bw");
  16:            //throw new NotImplementedException();
  17:        }
  18:    }

ConcreteClassB: 1.讀入影像 2.金字塔縮小 3.金字塔放大

   1: class ConcreteClassB : AbstractClass
   2:     {
   3:         protected override void proc1()
   4:         {
   5:             MessageBox.Show("具體B proc1=imread");
   6:             //throw new NotImplementedException();
   7:         }
   8:         protected override void proc2()
  10:             MessageBox.Show("具體B proc2=pyrmidDown");
  11:             //throw new NotImplementedExcepti
   9:         {
on();
  12:         }
  13:         protected override void proc3()
  14:         {
  15:             MessageBox.Show("具體B proc3=pyrmidUp");
  16:             //throw new NotImplementedException();
  17:         }
  18:     }

客戶端程式

AbstractClass imgProc;

影像處理1. 讀入影像 2.彩色轉灰階 3. 灰階轉黑白

imgProc = new ConcreteClassA();            <---------多型

或是

影像處理1.讀入影像 2.金字塔縮小 3.金字塔放大

imgProc = new ConcreteClassB();

image

   1: AbstractClass imgProc;
   2:        private void button1_Click(object sender, EventArgs e)
   3:        {
   4:            imgProc = new ConcreteClassA();
   5:            imgProc.TemplateMethod();
   6:        }
   7:  
   8:        private void button2_Click(object sender, EventArgs e)
   9:        {
  10:            imgProc = new ConcreteClassB();
  11:            imgProc.TemplateMethod();
  12:        }

------------------------------------

如果把父類別中proc1()、proc2()、proc3()從abstract變成virtual,則繼承的子類別可以不需要實作(覆寫)全部函式,儘管TemplateMethod中有定義proc3()

image

   1: namespace TemplateCMethodDemo
   2: {
   3:     abstract class AbstractClass
   4:     {
   5:         protected virtual void proc1(){}
   6:         protected virtual void proc2(){}
   7:         protected virtual void proc3(){}
   8:         public void TemplateMethod()
   9:         {
  10:             proc1();
  11:             proc2();
  12:             proc3();
  13:             MessageBox.Show("TemplateMethod");            
  14:         }
  15:     }
  16:     class ConcreteClassA : AbstractClass
  17:     {
  18:         protected override void proc1()
  19:         {
  20:             MessageBox.Show("具體A proc1=imread");
  21:             //throw new NotImplementedException();
  22:         }
  23:         protected override void proc2()
  24:         {
  25:             MessageBox.Show("具體A proc2=rgb2gray");
  26:             //throw new NotImplementedException();
  27:         }
  28:         //protected override void proc3()
  29:         //{
  30:         //    MessageBox.Show("具體A proc3=gray2bw");
  31:         //    //throw new NotImplementedException();
  32:         //}
  33:     }
  34:     class ConcreteClassB : AbstractClass
  35:     {
  36:         protected override void proc1()
  37:         {
  38:             MessageBox.Show("具體B proc1=imread");
  39:             //throw new NotImplementedException();
  40:         }
  41:         protected override void proc2()
  42:         {
  43:             MessageBox.Show("具體B proc2=pyrmidDown");
  44:             //throw new NotImplementedException();
  45:         }
  46:         protected override void proc3()
  47:         {
  48:             MessageBox.Show("具體B proc3=pyrmidUp");
  49:             //throw new NotImplementedException();
  50:         }
  51:     }
  52: }


參考資料:

1. 大話設計模式, 作者: 程杰

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()