状态模式
1.UML类图

2.核心代码
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace fd 7 { 8 public class Account 9 { 10 public AccountState State { set; get; } 11 public string Owner { set; get; } 12 public double Balance { set; get; } 13 14 public Account(string owner, double initialAmount) 15 { 16 Owner = owner; 17 Balance = initialAmount; 18 State = new VistorState(this); 19 } 20 21 public void SetBalance(double amount) 22 { 23 Balance = amount; 24 25 } 26 27 28 public void Deposit(double amount) 29 { 30 Console.WriteLine("存入" + amount); 31 State.Deposit(amount); 32 33 } 34 35 public void Shopping(double amount) 36 { 37 Console.WriteLine("消费" + amount); 38 State.Shopping(amount); 39 40 } 41 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace fd 7 { 8 9 public abstract class AccountState 10 { 11 protected Account _account; 12 13 public abstract void Deposit(double amount); 14 public abstract void Shopping(double amount); 15 public abstract void StateCheck(); 16 } 17 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace fd 7 { 8 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 Account account = new Account("白石麻衣", 666666); 14 Console.WriteLine("开户成功。姓名==>" + account.Owner + " 初始金额==>" + account.Balance ); 15 16 account.Deposit(56); 17 Console.WriteLine("==================================="); 18 19 } 20 } 21 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace fd 7 { 8 9 public class VistorState:AccountState 10 { 11 public VistorState(Account account) 12 { 13 _account = account; 14 StateCheck(); 15 } 16 17 18 public override void Deposit(double amount) 19 { 20 _account.Balance += amount; 21 Console.WriteLine( amount + "元,存款后的账户余额为:" + _account.Balance); 22 StateCheck(); 23 } 24 25 public override void Shopping(double amount) 26 { 27 double newBalance = _account.Balance - amount; 28 if (newBalance > 0) 29 { 30 _account.Balance -= amount; 31 Console.WriteLine( amount + "元,账户余额:" + _account.Balance); 32 } 33 34 StateCheck(); 35 } 36 37 38 public override void StateCheck() 39 { 40 if (_account.Balance > 100 && _account.Balance < 1000) 41 { 42 _account.State = new MemberState(_account); 43 } 44 else if (_account.Balance > 1000) 45 { 46 _account.State = new VIPState(_account); 47 } 48 } 49 } 50 51 public class VIPState : AccountState 52 { 53 public VIPState(Account account) 54 { 55 _account = account; 56 } 57 58 public override void Deposit(double amount) 59 { 60 _account.Balance += amount; 61 Console.WriteLine(amount + "元,存款后的账户余额为:" + _account.Balance); 62 StateCheck(); 63 } 64 65 public override void Shopping(double amount) 66 { 67 double newBalance = _account.Balance - amount; 68 if (newBalance > 0) 69 { 70 _account.Balance -= amount; 71 Console.WriteLine(amount + "元,账户余额:" + _account.Balance); 72 } 73 StateCheck(); 74 } 75 76 public override void StateCheck() 77 { 78 if (_account.Balance > 0 && _account.Balance < 100) 79 { 80 _account.State = new VistorState(_account); 81 } 82 else if (_account.Balance > 100 && _account.Balance < 1000) 83 { 84 _account.State = new MemberState(_account); 85 } 86 } 87 } 88 89 public class MemberState : AccountState 90 { 91 public MemberState(Account account) 92 { 93 _account = account; 94 } 95 96 97 public override void Deposit(double amount) 98 { 99 _account.Balance += amount; 100 Console.WriteLine(amount + "元,存款后的账户余额为:" + _account.Balance); 101 StateCheck(); 102 } 103 104 105 public override void Shopping(double amount) 106 { 107 double newBalance = _account.Balance - amount; 108 if (newBalance > 0) 109 { 110 _account.Balance -= amount; 111 Console.WriteLine(amount + "元,账户余额:" + _account.Balance); 112 } 113 114 StateCheck(); 115 } 116 117 118 public override void StateCheck() 119 { 120 if (_account.Balance > 0 && _account.Balance < 100) 121 { 122 _account.State = new VistorState(_account); 123 } 124 else if (_account.Balance > 1000) 125 { 126 _account.State = new VIPState(_account); 127 128 } 129 } 130 } 131 }
3.view on github
https://github.com/mengx8/test
浙公网安备 33010602011771号