Patterns in SOME –State

 
Code in C#:
 
namespace State_DesignPattern
{
     using System;
 
     abstract class State
     {
         protected string strStatename;      
 
         abstract public void Pour();
         // do something state-specific here
     }
 
     class OpenedState : State
     {       
         public OpenedState ()
         {
              strStatename = "Opened";
         }
         override public void Pour()
         {
              Console.WriteLine("...pouring...");
              Console.WriteLine("...pouring...");
              Console.WriteLine("...pouring...");
         }
     }
    
     class ClosedState : State
     {       
         public ClosedState()
         {
              strStatename = "Closed";
         }
         override public void Pour()
         {
              Console.WriteLine("ERROR - bottle is closed - cannot pour");
         }
     }
 
     class ContextColaBottle
     {
         public enum BottleStateSetting
         {
              Closed,
              Opened
         };
 
         // If teh state classes had large amounts of instance data,
         // we could dynamically create them as needed - if this demo
         // they are tiny, so we just create them as data members
         OpenedState openedState = new OpenedState();
         ClosedState closedState = new ClosedState();
 
         public ContextColaBottle ()
         {
              // Initialize to closed
              CurrentState = closedState;
         }
 
         private State CurrentState;
        
         public void SetState(BottleStateSetting newState)
         {
              if (newState == BottleStateSetting.Closed)
              {
                   CurrentState = closedState;
              }
              else
              {
                   CurrentState = openedState;
              }
         }
 
         public void Pour()
         {
              CurrentState.Pour();
         }   
     }
 
     ///<summary>
     ///    Summary description for Client.
     ///</summary>
     public class Client
     {
         public static int Main(string[] args)
         {
              ContextColaBottle contextColaBottle = new ContextColaBottle();
 
              Console.WriteLine("initial state is closed");
 
              Console.WriteLine("Now trying to pour");
              contextColaBottle.Pour();
 
              Console.WriteLine("Open bottle");
              contextColaBottle.SetState(ContextColaBottle.BottleStateSetting.Opened);
 
              Console.WriteLine("Try to pour again");
              contextColaBottle.Pour();
 
              return 0;
         }
     }
}
 
 
Code in SOME:
  
AState
       string m_strStatename
       a_Pour()
 
COpenedState : AState
       (string m_strStatename)                      /* member m_strStatename should not be defined again becuase it has already been defined by its father*/
       o_Pour()
 
CClosedState : AState
       ()
       o_Pour()
 
CContextColaBottle ->AState[_CurrentState] ->CCOpenedState[c_openedState.()] ->CClosedState[c_closedState.()]           
       ()                         
       SetState(string)                                        
       Pour()
 
CClient
       main
 
      
CClient.main
{
       CContextColaBottle contextColaBottle.()
       {
              _CurrentState = _closedState;
       };
 
       <% Console.WriteLine("initial state is closed"); %>
 
       <% Console.WriteLine("Now trying to pour"); %>
       contextColaBottle.Pour()
       {
              _CurrentState.Pour();                                //override function should not be unfolded in this situation
       };
 
       <% Console.WriteLine("Open bottle"); %>
       contextColaBottle.SetState(newState["CContextColaBottle.BottleStateSetting.Opened"])                 //alias for parameter, all the content between [] will be reserved
       {
                     <%
                     if (newState == BottleStateSetting.Closed)
                     {
                            _CurrentState = _closedState;
                     }
                     else
                     {
                            _CurrentState = _openedState;
                     }
                     %> 
       };
 
       <% Console.WriteLine("Try to pour again");%>
       contextColaBottle.Pour();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值