狀態模式

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace ConsoleApplication32
  8 {
  9     public interface States
 10     {
 11         void PreProduction(Context context); //上機
 12         void PostProduction(Context context);//下機
 13         void MaterialImput(Context context);//物料錄入
 14     }
 15 
 16     public  class Context
 17     {
 18         public Context(States state)
 19         {
 20             this.currentState = state;
 21         }
 22         private States currentState;
 23         public States CurrentState
 24         {
 25             //set
 26             //{
 27             //    currentState = value;
 28             //}
 29 
 30             get { return currentState; }
 31             set { currentState = value; }
 32         }
 33 
 34         /// <summary>
 35         /// 执行当前状态
 36         /// </summary>
 37         public virtual void PreProduction() { this.currentState.PreProduction(this); }
 38         public virtual void PostProduction() { this.currentState.PostProduction(this); }
 39         public virtual void MaterialImput() { this.currentState.MaterialImput(this); }
 40     }
 41 
 42     /// <summary>
 43     /// 空閒
 44     /// </summary>
 45     public class OpenState : States
 46     {
 47         public void PreProduction(Context context)
 48         {
 49             Console.WriteLine("FLOW卡上機成功");
 50             context.CurrentState = new Opening();
 51         }
 52 
 53         public void PostProduction(Context context)
 54         {
 55         
 56             Console.WriteLine("FLOW卡還未物料錄入,無法下機");
 57 
 58         }
 59 
 60         public void MaterialImput(Context context)
 61         {
 62             Console.WriteLine("FLOW卡未上機不能進行物料錄入");
 63 
 64         }
 65     }
 66  
 67     /// <summary>
 68     /// 完成
 69     /// </summary>
 70     public class CloseState : States
 71     {
 72         public void PreProduction(Context context)
 73         {
 74             Console.WriteLine("FLOW卡正在上機中,無法重複上機");
 75            
 76         }
 77 
 78         public void PostProduction(Context context)
 79         {
 80             Console.WriteLine("物料已錄入,成功下機");
 81             context.CurrentState = new OpenState();
 82         }
 83 
 84         public void MaterialImput(Context context)
 85         {
 86             Console.WriteLine("物料已錄入,無法重複錄入");
 87           
 88         }
 89     }
 90 
 91     /// <summary>
 92     /// 生產中
 93     /// </summary>
 94     public class Opening : States
 95     {
 96         public void PreProduction(Context context)
 97         {
 98             Console.WriteLine("FLOW卡正在上機中,無法重複上機");
 99         
100         }
101 
102         public void PostProduction(Context context)
103         {
104             Console.WriteLine("物料還未錄入,無法下機");
105         }
106 
107         public void MaterialImput(Context context)
108         {
109             Console.WriteLine("FLOW已上機,物料成功錄入");
110             context.CurrentState = new CloseState();
111         }
112     }
113 }
using ConsoleApplication32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {

            Context contexts = new Context(new OpenState());
            contexts.PreProduction();//上機
            contexts.PostProduction();//下機
            contexts.MaterialImput();//物料錄入

            contexts.PreProduction();
            contexts.PostProduction();
            contexts.MaterialImput();
            Console.ReadKey();
        }
    }
}

原文地址:https://www.cnblogs.com/YzpJason/p/7268933.html