状态者模式
下面是设计的UML类图:
类总括:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Club 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 Account account = new Account("Legend", 270); 14 Console.WriteLine("Successed!"); 15 Console.WriteLine("Name:{0}", account.Owner); 16 Console.WriteLine("Initial:{0}", account.Balance); 17 Console.WriteLine("------------------"); 18 19 account.Cost(250); 20 Console.WriteLine("-----------------------------------------"); 21 22 account.Deposit(100); 23 Console.WriteLine("-------------------"); 24 25 account.Deposit(200); 26 Console.WriteLine("-----------------------------------------"); 27 28 account.Cost(300); 29 Console.WriteLine("--------------------"); 30 31 account.Deposit(999); 32 Console.WriteLine("-----------------------------------------"); 33 34 account.Cost(700); 35 Console.WriteLine("---------------------"); 36 37 account.Cost(123); 38 Console.WriteLine("-----------------------------------------"); 39 40 Console.ReadLine(); 41 } 42 } 43 } 44 class Account 45 { 46 public ClientState State{get;set;} 47 public string Owner{get;set;} 48 public double Balance{get;set;} 49 50 public Account(string owner,double initialAmount) 51 { 52 Owner=owner; 53 Balance=initialAmount; 54 State=new VistorState(this); 55 } 56 57 public void SetBalance(double amount) 58 { 59 Balance=amount; 60 } 61 62 public void Deposit(double amount) 63 { 64 Console.WriteLine("Now,deposit {0}.", amount); 65 State.Deposit(amount); 66 Console.WriteLine("ClientState turn to {0}", State); 67 } 68 69 public void Cost(double amount) 70 { 71 Console.WriteLine("Now,Cost {0}.", amount); 72 State.Cost(amount); 73 Console.WriteLine("ClientState turn to {0}", State); 74 } 75 } 76 abstract class ClientState 77 { 78 protected Account Account; 79 public abstract void Deposit(double amount); 80 public abstract void Cost(double amount); 81 public abstract void Check(); 82 } 83 class VistorState : ClientState 84 { 85 public VistorState(Account account) 86 { 87 Account=account; 88 } 89 90 public override void Deposit(double amount) 91 { 92 Account.Balance+=amount; 93 Console.WriteLine("Deposit {0} to {1},surplus of account have {2} affter deposit", amount, Account.Owner, Account.Balance); 94 Check(); 95 } 96 97 public override void Cost(double amount) 98 { 99 double newBalance=Account.Balance-amount; 100 Account.Balance-=amount; 101 Console.WriteLine("Cost {0} from {1},surplus of account have {2} affter cost", amount, Account.Owner, Account.Balance); 102 Check(); 103 } 104 public override void Check() 105 { 106 if(Account.Balance>100 && Account.Balance<1000) 107 { 108 Account.State=new MemberState(Account); 109 } 110 else if(Account.Balance>=1000) 111 { 112 Account.State=new VIPState(Account); 113 } 114 } 115 } 116 class MemberState : ClientState 117 { 118 public MemberState(Account account) 119 { 120 Account=account; 121 } 122 123 public override void Deposit(double amount) 124 { 125 Account.Balance+=amount; 126 Console.WriteLine("Deposit {0} to {1},surplus of account have {2} affter deposit", amount, Account.Owner, Account.Balance); 127 Check(); 128 } 129 130 public override void Cost(double amount) 131 { 132 double newBalance=Account.Balance-amount; 133 Account.Balance-=amount; 134 Console.WriteLine("Cost {0} from {1},surplus of account have {2} affter cost", amount, Account.Owner, Account.Balance); 135 Check(); 136 } 137 public override void Check() 138 { 139 if(Account.Balance>0 && Account.Balance<=100) 140 { 141 Account.State = new VistorState(Account); 142 } 143 else if(Account.Balance>=1000) 144 { 145 Account.State=new VIPState(Account); 146 } 147 } 148 } 149 class VIPState : ClientState 150 { 151 public VIPState(Account account) 152 { 153 Account=account; 154 } 155 156 public override void Deposit(double amount) 157 { 158 Account.Balance+=amount; 159 Console.WriteLine("Deposit {0} to {1},surplus of account have {2} affter deposit", amount, Account.Owner, Account.Balance); 160 Check(); 161 } 162 163 public override void Cost(double amount) 164 { 165 double newBalance=Account.Balance-amount; 166 Account.Balance-=amount; 167 Console.WriteLine("Cost {0} from {1},surplus of account have {2} affter cost", amount, Account.Owner, Account.Balance); 168 Check(); 169 } 170 public override void Check() 171 { 172 if(Account.Balance>100 && Account.Balance<1000) 173 { 174 Account.State=new MemberState(Account); 175 } 176 else if (Account.Balance > 0 && Account.Balance <=100) 177 { 178 Account.State = new VistorState(Account); 179 } 180 } 181 }
运行结果:
优点:
1.封装了转换规则;
2. 方便增加 新的状态;
3. 允许状态转换逻辑与状态对象合成一体;
4. 环境对象可共享状态对象。
缺点:
1.结构及实现复杂;
2.代码更显得累赘;
3.不太支持OCP。