Design Pattern Collation _ state mode

Source: Internet
Author: User

State mode allows an object to change its behavior when the internal state changes, and the object looks as if it has modified its class. Because this pattern encapsulates the state as a separate class and delegates the action to an object representing the current state, the behavior changes with the internal state.

In the state mode, the context holds the state, and it defines a common interface for all States, and any State implements the same interface, so that each state can be replaced by each other. State interfaces are implemented, so when the context changes state, Behavior changes as well. At any time, whenever someone calls the specific method of the context, it will be used to delegate state processing, which is illustrated by specific examples, the person class represents the context.

  

 Public classPerson {//the three states givenState happystate=NewHappystate ( This); State Angrystate=NewAngrystate ( This); State Sadstate=NewSadstate ( This); State State=happystate;//Initial State//returns the status to facilitate the setting of the State     PublicState Gethappystate () {returnhappystate; }     PublicState Getangrystate () {returnangrystate; }     PublicState Getsadstate () {returnsadstate; }     Public voidsetState (state state) { This. state = State;//When conditions change, set the state    }    //The following three methods will call the state's own method     Public voidListen () {State.listen (); }     Public voiddohomework () {state.dohomework (); }     Public voidStudy () {state.study (); }}
ImportJava.util.Scanner;//state interface, all state classes need to implement this interface Public InterfaceState { Public voidlisten ();  Public voiddohomework ();  Public voidstudy ();}//Three implementation ClassesclassHappystateImplementsstate{PrivatePerson p;//The person object needs to be passed in in order to change the state         Publichappystate (person p) { This. P =p; } @Override Public voidListen () {System.out.println ("I don ' t know the lesson"); System.out.println ("Do you want to listen again"); Scanner SC=NewScanner (system.in); String Str=Sc.nextline (); if(Sc.equals ("No") ) P.setstate (P.getsadstate ()); //dynamic change of state through customer's choice} @Override Public voiddohomework () {System.out.println ("I don ' t know how does it."); System.out.println ("Review?"); Scanner SC=NewScanner (system.in); String Str=Sc.nextline (); if(Str.equals ("No") {p.setstate (P.getangrystate ()); }} @Override Public voidStudy () {SYSTEM.OUT.PRINTLN ("Studying Happy"); }    }classAngrystateImplementsstate{PrivatePerson p;  Publicangrystate (person p) { This. P =p; } @Override Public voidListen () {System.out.println ("I can ' t listen more!"); } @Override Public voiddohomework () {System.out.println ("It's Too Hard!!!"); } @Override Public voidStudy () {SYSTEM.OUT.PRINTLN ("Just want to sleep!!"); }}classSadstateImplementsstate{PrivatePerson p;  Publicsadstate (person p) { This, pb=p; } @Override Public voidListen () {System.out.println ("Why I can ' t understand it!"); } @Override Public voiddohomework () {System.out.println ("I can ' t do the homework,"); System.out.println ("Just Listen again?"); Scanner SC=NewScanner (system.in); String Str=Sc.nextline (); if(Str.equals ("Yes") {p.setstate (P.gethappystate ());        P.listen (); }        Elsep.setstate (P.getangrystate ()); } @Override Public voidStudy () {SYSTEM.OUT.PRINTLN ("I can ' t study!"); }}
 Public class Test {    publicstaticvoid  main (string[] args) {person        p=New person ();         /* The client calls the context method without knowing its internal specific state implementation */         P.listen ();        P.dohomework ();        P.study ();    }}

The UML class diagram is as follows:

Design Pattern Collation _ state mode

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.