An example of a design pattern State

xiaoxiao2021-03-06  93

An example of a design pattern State

(From http:/blog.9cbs.net/airhand/archive/2004/08/27/86405.ASPX? Some chapters in?

This instance is relatively long, I briefly explain that this is an example of an automatic vending machine, and the customer can throw a coin of 5, 10, and 25, the value of 25. Whenever the customer is investigating the amount of money, if it is 25, it will be sold. State is an abstract class, which depends 5, 10, 15, 20, 25 yuan and classes (that is, all possible money and), because they are inherited from State, they all have a state. Type static member state as a status of the status (you can imagine it into a global variable), each class receives a coin invested 5, 10, 25-dimensional coins, the corresponding method is public Virtual Void Add Nickel (VendingMachine VM) {} public Virtual Void Adddime (VendingMachine VM) {} Public Virtual Void Addquarter (VendingMachine VM) {虽, although the method of each class is different, such as 5 yuan of class, after receiving 10 yuan, STATE is transition It is 15 yuan to push it in this class. Take a closer look, this is a very interesting example. But tell the truth, so that the implementation is really too tired, maybe you can reduce the burden on programmers in other applications, but I haven't found it (some words tell me). In addition, if there is 100 states, there are 10 kinds of paths, is it inherited by each state (100 × 10)? What Coding is not too tired, and the code should not be too long, hey, when the programmer is not easy ...

Using system;

Abstract Class State {? public virtual void address {}

? Public Virtual Void Adddime (VendingMachine VM) {}

Public Virtual Void Addquarter (VendingMachine VM) {}

? protected virtual void changeteate (VendingMachine VM, State S) {??? vm.changestate (s) ;?}}

Class VENDINGMACHINE {? private vendingmachine () {??? console.writeline ("The Vending Machine Is Now Online: Product Costs 25c"); ??? state = start.instance () ;?}

Public Void ChangeState (state to) {??? state = to ;?}

PUBLIC VOID VEND () {// Remnied ??? Console.WriteLine ("Dispensing Product ... Thank you!") ;?}

Public void add nickel () {??? state.addnickel (this) ;?}

Public void adddime () {??? state.adddime (this) ;?}

Public void addquarter () {??? state.addquarter (this) ;?}}

Class start: state {? private static state state = new start (); public static state instance () {// singleton logic ??? console.writeline ("CRedit: 0c"); ??? Return State;?}

PUBLIC OVERRIDE VOID ADDNICKEL (VendingMachine VM) {??? ChangeState (VM, Five.instance ());?}

? public override void adddime (vendingmachine vm) {??? changestate (VM, Ten.Instance ());?}

PUBLIC OVERRIDE VOID Addquarter (VendingMachine VM) {??? vm.vend () ;?}}

Class Five: State {? private static state state = new file () ;? private file () {?}

PUBLIC Static State Instance () {// Singleton Logic ??? Console.WriteLine ("CREDIT: 5C"); ??? Return State ;?}

PUBLIC OVERRIDE VOID ADDNICKEL (VendingMachine VM) {??? ChangeState (VM, Ten.Instance ());?}

? public override void adddime (vendingmachine vm) {??? channelate (vm, fifteen.instance ());?}

PUBLIC OVERRIDE VOID Addquarter (VendingMachine VM) {??? vm.vend (); ??? ChangeState (VM, Start.instance ()); // no change Returned :-)?}}

Class Ten: State {? private static state state = new ten () ;? private ten () {?}

PUBLIC Static State Instance () {// Singleton logic ??? console.writeline ("CREDIT: 10C"); ??? Return State ;?}

PUBLIC OVERRIDE VOID ADDNICKEL (VendingMachine VM) {??? ChangeState (VM, Fifteen.instance ());?}

? public override void adddime (VendingMachine VM) {??? changestate (VM, TWENTY.INSTANCE ());?}

PUBLIC OVERRIDE VOID Addquarter (VendingMachine VM) {??? vm.vend (); ??? ChangeState (VM, Start.instance ()); // no change Returned :-)?}}

Class Fifteen: State {? private static state state = new fifteen () ;? private fifteen () {?}

PUBLIC Static State Instance () {// Singleton logic ??? console.writeline ("CREDIT: 15C"); ??? Return State ;?}

PUBLIC OVERRIDE VOID ADDNICKEL (VENDINGMACHINE VM) {??? ChangeState (VM, TWENTANSTANCE ());?}? public override void adddime (vendingmachine vm) {??? vm.vend ();? ??? ChangeState VM, Start.instance ());?}

PUBLIC OVERRIDE VOID Addquarter (VendingMachine VM) {??? vm.vend (); ??? ChangeState (VM, Start.instance ()); // no change Returned :-)?}}

Class twenty: State {? private static state state = new twenty () ;? private twenty () {?}

Public static state instance () {// singleton logic ??? console.writeline ("CREDIT: 20C"); ??? Return State ;?}

Public override void add nickel (VendingMachine VM) {??? vm.vend (); ??? ChangeState (VM, Start.instance ());?}

PUBLIC OVERRIDE VOID ADDDIME (VENDINGMACHINE VM) {??? vm.vend (); ??? ChangeState (VM, Start.instance ()) ;?}

PUBLIC OVERRIDE VOID Addquarter (VendingMachine VM) {??? vm.vend (); ??? ChangeState (VM, Start.instance ()); // no change Returned :-)?}}

Class application {? public static void main () {??? int coin = 0; ??? string infut = null; ??? vendingmachine vm = new vendingmachine (); ??? while (TRUE) {???? ? Console.write ("INSERT A COIN (5, 10, 25):"); ????? INPUT = Console.Readline (); ????? coin = int32.parse (input);??? ?? Switch (COIN) {??????? case 5: ????????? vm.addnickel (); ????????? Break; ??????? Case 10: ????????? vm.adddime (); ??????????.?????????.?????. Addquarter (); ????????? brefault: ????????? Break; ?????} ???}?}}

转载请注明原文地址:https://www.9cbs.com/read-122726.html

New Post(0)