image

新增一個windows form application

image

同時加入一個新的類別檔案

image

   1: public interface iCommand
   2: {
   3:     void execute();
   4:     //void undo();
   5:     //void save();
   6:     //void load();
   7: }
   1: public class BusinessCheckCmd
   2:   {
   3:       // 身分查核
   4:       public void IdentityCheck()
   5:       {
   6:           MessageBox.Show("身分查核", "IdentityCheck()");
   7:       }
   8:       // 行李檢查+護照檢查
   9:       public void DoCheck()
  10:       {
  11:           MessageBox.Show("行李檢查", "DoCheck()");
  12:           MessageBox.Show("護照檢查", "DoCheck()");
  13:       }
  14:   }
   1: public class IdentityCheckCmd:iCommand
   2: {
   3:     private BusinessCheckCmd m_bcc;
   4:     public IdentityCheckCmd(BusinessCheckCmd bcc)
   5:     {
   6:         this.m_bcc = bcc;
   7:     }
   8:     public void execute()
   9:     {
  10:         m_bcc.IdentityCheck();
  11:     }
  12: }
   1: public class DoCheckCmd : iCommand
   2:  
   3:    private BusinessCheckCmd m_bcc;
   4:    public DoCheckCmd(BusinessCheckCmd bcc)
   5:    {
   6:        this.m_bcc = bcc;
   7:    }
   8:    public void execute()
   9:    {
  10:        m_bcc.DoCheck();
  11:    }
   1: public class NoCmd : iCommand
   2: {
   3:     public void execute()
   4:     {
   5:         MessageBox.Show("Do nothing", "NoCmd");
   6:     }
   7: }

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

1 public class InvokerCommand 2 { 3 private iCommand[] = commands; 4 // 建構子 5 public InvokerCommand() 6 { 7 commands = new iCommand[1]; 8 commands[0] = new NoCommand(); 9 } 10 // 具參數建構子 11 public InvokerCommand(int Num) 12 { 13 commands = new iCommand[num]; 14 for(int i=0; i<commands.Length; i++) 15 { 16 commands[i] = new NoCommand(); 17 } 18 } 19 public void SetCmd(iCommand cmd) 20 { 21 commands[0] = cmd; 22 } 23 public void SetCmd(int slot, iCommand cmd) 24 { 25 commands[slot] = cmd; 26 } 27 public void RunCmd() 28 { 29 commands[0].execute(); 30 } 31 public void RunCmd(int slot) 32 { 33 commands[slot].execute(); 34 } 35 public void RunAllCmd() 36 { 37 for(int i=0; i<commands.Length; i++) 38 { 39 commands[i].execute(); 40 } 41 } 42 } 43


client端應用程式

image

   1: private void button1_Click(object sender, EventArgs e)
   2: {
   3:     int num = 2;
   4:     InvokerCommand cmdSet = new InvokerCommand(num);
   5:     BusinessCheckCmd bcc = new BusinessCheckCmd();      // 業務
   6:  
   7:     IdentityCheckCmd icc = new IdentityCheckCmd(bcc);   // 身分查核
   8:     DoCheckCmd dcc = new DoCheckCmd(bcc);       // 行李檢查+護照檢查
   9:  
  10:     cmdSet.setCmd(0, icc);
  11:     cmdSet.setCmd(1, dcc);
  12:     cmdSet.runAllCmds();
  13: }

imageimageimage

   1: private void button2_Click(object sender, EventArgs e)
   2: {
   3:     InvokerCommand cmdSet = new InvokerCommand();
   4:     BusinessCheckCmd bcc = new BusinessCheckCmd();      // 業務
   5:     IdentityCheckCmd icc = new IdentityCheckCmd(bcc);   // 身分查核
   6:     cmdSet.setCmd(0, icc);
   7:     cmdSet.runCmd(0);
   8: }

image




參考資料: Visual C#程式設計實例演練與系統開發2013 許清榮

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

    天天向上

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