Detailed description of design mode-state pattern

Source: Internet
Author: User
State Pattern


Address: http://blog.csdn.net/caroline_wendy


State pattern: allows an object to change its behavior when its internal state changes. The object seems to have modified its class.


CreateContext class, Contains multipleConcrete state classAnd call specific methods according to different States,State. Handle (),IncludeSet \ get MethodChange status.

State Interface), IncludingAbstract method handle (),Concrete state inheritance (implement) state class (state)To implement the handle () method;


Specific Method:

1.Context class, Combining multipleConcrete state)And provideSet \ get Method,Handle)To change the status.

/*** @ Time July 11, 2014 */package state. pattern;/*** @ author C. l. wang **/public class gumballmachine {state soldoutstate; State noquarterstate; State hasquarterstate; State soldstate; State winnerstate; State state = soldoutstate; int COUNT = 0; /***/Public gumballmachine (INT numbergumbils) {// todo auto-generated constructor stubsoldoutstate = new soldoutstate (this); noquarterstate = new noquartersta Te (this); hasquarterstate = new hasquarterstate (this); soldstate = new soldstate (this); winnerstate = new winnerstate (this); this. count = numbergumbils; If (numbergumbils> 0) {state = noquarterstate;} public void insertquarter () {state. insertquarter ();} public void ejectquarter () {state. ejectquater ();} public void turncrank () {state. turncrank (); State. dispense ();} void setstate (State state) {This. state = State;} void releaseball () {system. Out. println ("A gumball comes rolling out the slot..."); If (count! = 0) count --;} int getcount () {return count;} void refill (INT count) {This. count = count; State = noquarterstate;} public State getstate () {return state;} public State getsoldoutstate () {return soldoutstate;} public State getnoquarterstate () {return noquarterstate ;} public state gethasquarterstate () {return hasquarterstate;} public State getsoldstate () {return soldstate;} public State getwinnerstate () {retu Rn winnerstate;} Public String tostring () {stringbuffer result = new stringbuffer (); result. append ("\ nmighty Gumball, Inc. "); result. append ("\ njava-enabled standing gumball model #2004 \ n"); result. append ("inventory:" + Count + "gumball"); If (count! = 1) {result. append ("S");} result. append ("\ nmachine is" + state + "\ n"); return result. tostring ();}}

2. State Interface), Including Handle).

/*** @ Time July 11, 2014 */package state. pattern;/*** @ author C. l. wang **/public interface state {void insertquarter (); void ejectquater (); void turncrank (); void dispense ();}

3. Concrete state), Implements the abstract method (handle) by using the implement state interface).

/*** @ Time July 11, 2014 */package state. pattern; import Java. util. random;/*** @ author C. l. wang **/public class hasquarterstate implements State {random randomwinner = new random (system. currenttimemillis (); gumballmachine;/*****/Public hasquarterstate (gumballmachine) {// todo auto-generated constructor stubthis. gumballmachine = gumballmachine;}/* (non-javadoc) * @ See S Tate. pattern. state # insertquarter () */@ overridepublic void insertquarter () {// todo auto-generated method stubsystem. out. println ("You can't insert another quarter. ");}/* (non-javadoc) * @ see state. pattern. state # ejectquater () */@ overridepublic void ejectquater () {// todo auto-generated method stubsystem. out. println ("quarter returned. "); gumballmachine. setstate (gumballmachine. getnoquarterstate ());}/* (Non-javadoc) * @ see state. pattern. state # turncrank () * // @ overridepublic void turncrank () {// todo auto-generated method stubsystem. out. println ("You turned... "); int Winner = randomwinner. nextint (4); If (Winner = 0) & (gumballmachine. getcount ()> 1) {// more than 1 candy gumballmachine is required. setstate (gumballmachine. getwinnerstate ();} else {gumballmachine. setstate (gumballmachine. getsoldstate ();}/* (non-javadoc )* @ See state. pattern. state # dispense () */@ overridepublic void dispense () {// todo auto-generated method stubsystem. out. println ("No gumball dispensed. ");} Public String tostring () {return" waiting for turn of Crank ";}/*** @ time July 11, 2014 */package state. pattern;/*** @ author C. l. wang **/public class noquarterstate implements State {gumballmachine;/*****/Public noquarterstate (Gumbal Lmachine gumballmachine) {// todo auto-generated constructor stubthis. gumballmachine = gumballmachine;}/* (non-javadoc) * @ see state. pattern. state # insertquarter () */@ overridepublic void insertquarter () {// todo auto-generated method stubsystem. out. println ("You inserted a quarter. "); gumballmachine. setstate (gumballmachine. gethasquarterstate ();}/* (non-javadoc) * @ see state. pattern. state # ejectquater () * // @ Overridepublic void ejectquater () {// todo auto-generated method stubsystem. out. println ("You haven't inserted a quarter. ");}/* (non-javadoc) * @ see state. pattern. state # turncrank () * // @ overridepublic void turncrank () {// todo auto-generated method stubsystem. out. println ("You turned, but there's no quarter. ");}/* (non-javadoc) * @ see state. pattern. state # dispense () */@ overridepublic void dispens E () {// todo auto-generated method stubsystem. out. println ("you need to pay first. ");} Public String tostring () {return" waiting for quater ";}/*** @ time July 11, 2014 */package state. pattern;/*** @ author C. l. wang **/public class soldoutstate implements State {gumballmachine;/*****/Public soldoutstate (gumballmachine) {// todo auto-generated constructor stubthis. gumball Machine = gumballmachine;}/* (non-javadoc) * @ see state. pattern. state # insertquarter () */@ overridepublic void insertquarter () {// todo auto-generated method stubsystem. out. println ("You can't insert a quarter, the machine is sold out. ");}/* (non-javadoc) * @ see state. pattern. state # ejectquater () */@ overridepublic void ejectquater () {// todo auto-generated method stubsystem. out. println ("You can't ejec T, you haven't inserted a quarter yet ");}/* (non-javadoc) * @ see state. pattern. state # turncrank () * // @ overridepublic void turncrank () {// todo auto-generated method stubsystem. out. println ("You turned, but there are no gambulls. ");}/* (non-javadoc) * @ see state. pattern. state # dispense () */@ overridepublic void dispense () {// todo auto-generated method stubsystem. out. println ("No gumball dispensed. ");} Pu BLIC string tostring () {return "sold out" ;}}/*** @ time July 11, 2014 */package state. pattern;/*** @ author C. l. wang **/public class soldstate implements State {gumballmachine;/*****/Public soldstate (gumballmachine) {// todo auto-generated constructor stubthis. gumballmachine = gumballmachine;}/* (non-javadoc) * @ see state. pattern. state # insertquarter () */@ overridepublic Void insertquarter () {// todo auto-generated method stubsystem. out. println ("Please wait, we're already giving you a gumball. ");}/* (non-javadoc) * @ see state. pattern. state # ejectquater () */@ overridepublic void ejectquater () {// todo auto-generated method stubsystem. out. println ("sorry, you already turned the crank. ");}/* (non-javadoc) * @ see state. pattern. state # turncrank () */@ overridepublic void TU Rncrank () {// todo auto-generated method stubsystem. out. println ("turning twice doesn't give you another gumball. ");}/* (non-javadoc) * @ see state. pattern. state # dispense () */@ overridepublic void dispense () {// todo auto-generated method stubgumballmachine. releaseball (); If (gumballmachine. getcount ()> 0) {gumballmachine. setstate (gumballmachine. getnoquarterstate ();} else {system. out. println ("Oops, O Ut of gumbils! "); Gumballmachine. setstate (gumballmachine. getsoldoutstate () ;}} Public String tostring () {return "dispensing a gumball" ;}}/*** @ time January 1, July 11, 2014 */package state. pattern;/*** @ author C. l. wang **/public class winnerstate implements State {gumballmachine;/*****/Public winnerstate (gumballmachine) {// todo auto-generated constructor stubthis. gumballmachine = gumballmachin E;}/* (non-javadoc) * @ see state. pattern. state # insertquarter () */@ overridepublic void insertquarter () {// todo auto-generated method stubsystem. out. println ("Please wait, we're already giving you a gumball");}/* (non-javadoc) * @ see state. pattern. state # ejectquater () */@ overridepublic void ejectquater () {// todo auto-generated method stubsystem. out. println ("Please wait, we're already giving you a gu Mball ");}/* (non-javadoc) * @ see state. pattern. state # turncrank () * // @ overridepublic void turncrank () {// todo auto-generated method stubsystem. out. println ("turning again doesn't give you another Gumball! ");}/* (Non-javadoc) * @ see state. pattern. state # dispense () */@ overridepublic void dispense () {// todo auto-generated method stubsystem. out. println ("you're a winner! You get two gumbils for your quarter. "); gumballmachine. releaseball (); If (gumballmachine. getcount () = 0) {gumballmachine. setstate (gumballmachine. getsoldoutstate ();} else {gumballmachine. releaseball (); If (gumballmachine. getcount ()> 0) {gumballmachine. setstate (gumballmachine. getnoquarterstate ();} else {system. out. println ("Oops, out of gumbils! "); Gumballmachine. setstate (gumballmachine. getsoldoutstate () ;}} Public String tostring () {return" demo-sing two gumbils for your quarter, because you're a winner! ";}}

4. Test class.

/*** @ Time July 11, 2014 */package state. pattern;/*** @ author C. l. wang **/public class gumballmachinetestdrive {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubgumballmachine gumballmachine = new gumballmachine (5); system. out. println (gumballmachine); gumballmachine. insertquarter (); gumballmachine. turncrank (); system. out. println (gumballmachine); gumballmachine. insertquarter (); gumballmachine. turncrank (); gumballmachine. insertquarter (); gumballmachine. turncrank (); system. out. println (gumballmachine );}}

5. Output. win twice.

Mighty, Gumball, Inc.Java-enabled Standing Gumball Model #2004Inventory: 5 gumballsMachine is waiting for quaterYou inserted a quarter.You turned...A gumball comes rolling out the slot...Mighty Gumball, Inc.Java-enabled Standing Gumball Model #2004Inventory: 4 gumballsMachine is waiting for quaterYou inserted a quarter.You turned...YOU‘RE A WINNER! you get two gumballs for your quarter.A gumball comes rolling out the slot...A gumball comes rolling out the slot...You inserted a quarter.You turned...YOU‘RE A WINNER! you get two gumballs for your quarter.A gumball comes rolling out the slot...A gumball comes rolling out the slot...Oops, out of gumballs!Mighty Gumball, Inc.Java-enabled Standing Gumball Model #2004Inventory: 0 gumballsMachine is sold out







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.