usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleapplication2{classProgram {//each object has its corresponding state, and each state corresponds to some corresponding behavior, and if an object has more than one state,//Then there will be a lot of action. Then the judgment of these States and the behavior done according to the state will result in multiple conditional statements,//and if you add a new state, you need to change the previously existing code. Such a design clearly violates the principle of opening and shutting. //state patterns are used to solve such problems. The state pattern abstracts the behavior of each state into a separate new object,//changes in this state no longer depend on the behavior within the object. //Account class: Maintains an instance of the State class that identifies the status of the current object. //State Class: A class of abstract states that defines the behavior conventions that a specific State class needs to implement. //Silvestater, Goldstate, and RedState classes: specific state classes that implement each behavior of the abstract State class. Static voidMain (string[] args) { //open a new account.Account Account =NewAccount ("Learning Hard"); //to Trade//SaveAccount. Deposit (1000.0); Account. Deposit (200.0); Account. Deposit (600.0); //Pay InterestAccount . Payinterest (); //Withdraw MoneyAccount. Withdraw (2000.00); Account. Withdraw (500.00); //wait for user inputConsole.readkey (); } } /// <summary> ///Account/// </summary> Public classAccount { PublicState State {Get;Set; } Public stringOwner {Get;Set; } PublicAccount (stringowner) { This. Owner =owner; This. State =NewSilverstate (0.0, This); } Public DoubleBalance {Get{returnState.balance; } }//Balance//Save Public voidDeposit (Doubleamount) {state.deposit (amount); Console.WriteLine ("Deposit amount is {0:c}--", amount); Console.WriteLine ("account balance is =:{0:c}", This. Balance); Console.WriteLine ("The account status is: {0}", This. State.gettype (). Name); Console.WriteLine (); } //Withdraw Money Public voidWithdraw (Doubleamount) {State.withdraw (amount); Console.WriteLine ("withdrawal amount is {0:c}--", amount); Console.WriteLine ("account balance is =:{0:c}", This. Balance); Console.WriteLine ("The account status is: {0}", This. State.gettype (). Name); Console.WriteLine (); } //Earn Interest Public voidpayinterest () {state.payinterest (); Console.WriteLine ("Interest Paid---"); Console.WriteLine ("account balance is =:{0:c}", This. Balance); Console.WriteLine ("The account status is: {0}", This. State.gettype (). Name); Console.WriteLine (); } } //Abstract State Class Public Abstract classState {//Properties PublicAccount Account {Get;Set; } Public DoubleBalance {Get;Set; }//Balance Public DoubleInterest {Get;Set; }//Interest Rate Public DoubleLowerlimit {Get;Set; }//Lower Limit Public DoubleUpperlimit {Get;Set; }//Upper Limit Public Abstract voidDeposit (DoubleAmount);//Deposit Public Abstract voidWithdraw (DoubleAmount);//Withdraw Money Public Abstract voidPayinterest ();//the interest earned } //Red state means the account is overdrawn. Public classRedstate:state { PublicRedState (state state) {//Initialize This. Balance =State . Balance; This. Account =State . account; Interest=0.00; Lowerlimit= -100.00; Upperlimit=0.00; } //Deposit Public Override voidDeposit (Doubleamount) {Balance+=amount; Statechangecheck (); } //Withdraw Money Public Override voidWithdraw (Doubleamount) {Console.WriteLine ("There's no money to take! "); } Public Override voidpayinterest () {//No Interest } Private voidStatechangecheck () {if(Balance >upperlimit) {Account.state=NewSilverstate ( This); } } } //the Silver state means that there is no interest Public classSilverstate:state { Publicsilverstate (state): This(state. Balance, state. account) {} PublicSilverstate (Doublebalance, account account) { This. Balance =balance; This. Account =Account ; Interest=0.00; Lowerlimit=0.00; Upperlimit=1000.00; } Public Override voidDeposit (Doubleamount) {Balance+=amount; Statechangecheck (); } Public Override voidWithdraw (Doubleamount) {Balance-=amount; Statechangecheck (); } Public Override voidpayinterest () {Balance+ = Interest *Balance; Statechangecheck (); } Private voidStatechangecheck () {if(Balance <lowerlimit) {Account.state=NewRedState ( This); } Else if(Balance >upperlimit) {Account.state=NewGoldstate ( This); } } } //Gold state means interest status. Public classGoldstate:state { Publicgoldstate (state state) { This. Balance =State . Balance; This. Account =State . account; Interest=0.05; Lowerlimit=1000.00; Upperlimit=1000000.00; } //Save Public Override voidDeposit (Doubleamount) {Balance+=amount; Statechangecheck (); } //Withdraw Money Public Override voidWithdraw (Doubleamount) {Balance-=amount; Statechangecheck (); } Public Override voidpayinterest () {Balance+ = Interest *Balance; Statechangecheck (); } Private voidStatechangecheck () {if(Balance <0.0) {account.state=NewRedState ( This); } Else if(Balance <lowerlimit) {Account.state=NewSilverstate ( This); } } }}
19. Status Person mode (state pattern)