close

 

image

image

image

GateState interface:

   1: interface GateState
   2: {
   3:    void enter();
   4:    void payOK();
   5:    void payFailed();
   6: }

Gate class:

   1: class Gate
   2: {
   3:    private GateState state;
   4:    public Gate(GateState _state)
   5:    {
   6:        this.state = _state;
   7:    }
   8:    public Gate()
   9:    {
  10:        state = new CloseGateState(this);
  11:    }
  12:    public void enter()
  13:    {
  14:        this.state.enter();
  15:    }
  16:    public void payOK()
  17:    {
  18:        this.state.payOK();
  19:    }
  20:    public void payFailed()
  21:    {
  22:        this.state.payFailed();
  23:    }
  24:    public void changeState(GateState _state)
  25:    {
  26:        this.state = _state;
  27:    }
  28:  
  29: }

CloseGateState class:

   1: class CloseGateState : GateState
   2: {
   3:    Gate gate;
   4:    public CloseGateState(Gate gate)
   5:    {
   6:        Console.WriteLine("Gate is closed.");
   7:        this.gate = gate;
   8:    }
   9:    public void enter()
  10:    {
  11:        Console.Write("enter: ");
  12:        this.gate.changeState(new CloseGateState(this.gate));
  13:    }
  14:  
  15:  
  16:    public void payFailed()
  17:    {
  18:        Console.Write("payFailed: ");
  19:        this.gate.changeState(new CloseGateState(this.gate));
  20:    }
  21:  
  22:    public void payOK()
  23:    {
  24:        Console.Write("payOK: ");
  25:        this.gate.changeState(new OpenGateState(this.gate));
  26:    }
  27: }

OpenGateState class:

   1: class OpenGateState : GateState
   2: {
   3:     Gate gate;
   4:     public OpenGateState(Gate _gate)
   5:     {
   6:         Console.WriteLine("Gate is open.");
   7:         this.gate = _gate;
   8:     }
   9:     public void enter()
  10:     {
  11:         Console.Write("enter: ");
  12:         this.gate.changeState(new CloseGateState(this.gate));
  13:     }
  14:  
  15:  
  16:     public void payFailed()
  17:     {
  18:         Console.Write("payFailed: ");
  19:         this.gate.changeState(new OpenGateState(this.gate));
  20:     }
  21:  
  22:     public void payOK()
  23:     {
  24:         Console.Write("payOK: ");
  25:         this.gate.changeState(new OpenGateState(this.gate));
  26:     }
  27: }

Console portion:

   1: class Program
   2: {
   3:     static void Main(string[] args)
   4:     {
   5:         Console.WriteLine("case 1.");
   6:         Gate gate = new Gate();
   7:         gate.enter();
   8:         gate.payOK();
   9:         Console.WriteLine("-----------------------");
  10:         Console.WriteLine("case 2.");
  11:         gate.changeState(new CloseGateState(gate));
  12:         gate.enter();
  13:         gate.payFailed();
  14:  
  15:         Console.WriteLine("-----------------------");
  16:         Console.WriteLine("case 3.");
  17:         gate.changeState(new OpenGateState(gate));
  18:         gate.enter();
  19:         gate.payOK();
  20:  
  21:         Console.WriteLine("-----------------------");
  22:         Console.WriteLine("case 4.");
  23:         gate.changeState(new OpenGateState(gate));
  24:         gate.payFailed();
  25:         gate.payOK();
  26:         Console.ReadKey();
  27:     }
  28: }

image

image

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

    天天向上

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