Enum enumeration writes a simple state machine

Source: Internet
Author: User

Today is raining, the mood is a little depressed, therefore uses the enumeration to write a state machine to resolve the mood, by the way the enumeration uses the method.

 PackageStateMachine;Importjava.util.ArrayList;Importjava.util.List;/** *  * @authorZhenweilai **/ Public enumStateenum {Ready ("1", "Ready") {@OverridevoidSubmit (Entity entity) {Super. Submit (entity); //rewrite the update state method to add your own business logicSystem.out.println ("Ready to submit the finishing touches ..."); }}, START ("2", "Start") {@OverridevoidUndo (Entity entity) {Super. Undo (entity); System.out.println ("Start Undo Finishing work ..."); }}, RUN ("3", "Run"), STOP ("4", "Stop"); //Status Code    PrivateString Code; //Status Name    PrivateString name; //Construction MethodStateenum (string code, string name) { This. Code =Code;  This. Name =name; //Register code in the list when constructingStateList.stateList.add (code); }    //update the state of the method, if the update state needs to do something else, then override the method, then super call, and then add your own logic    voidSubmit (Entity entity) {if(entity.getstate () = =NULL&&! This. GetCode (). Equals (Ready.getcode ()))Throw NewRuntimeException ("Status not valid"); Else if(entity.getstate () = =NULL&& This. GetCode (). Equals (Ready.getcode ())) {Entity.setstate (StateList.stateList.get (0)); return; }                    if(! StateList.stateList.get ((StateList.stateList.indexOf (Entity.getstate ()) +1). Equals ( This. Code)) Throw NewRuntimeException ("Status not valid"); if(StateList.stateList.contains ( This. Code)) {Entity.setstate ( This. Code); }    }    //anti-operation method, same as submission method    voidUndo (Entity entity) {//throws an exception if there is currently no state and is not the current enumeration state        if(entity.getstate () = =NULL||! Entity.getstate (). Equals ( This. Code)) Throw NewRuntimeException ("Status not valid"); //determine if the legal state of the list has been registered            if(StateList.stateList.contains ( This. Code)) {Integer Codeindex= StateList.stateList.indexOf ( This. Code); //if it is not initialized, fallback to a state, otherwise set to null                if(codeindex>0) entity.setstate (StateList.stateList.get (--codeindex)); ElseEntity.setstate (NULL); }    }        //get the state name according to code     Public Staticstring Getnamebycode (String code) { for(Stateenum item:StateEnum.values ()) {if(Item.getcode (). Equals (code)) {returnItem.getname (); }        }         return""; }        /**     *      * @authorZhenweilai * Static inner class, very eco-friendly, in order to use this static list * Because the enumeration constructor method cannot call the static property, the reason is unknown, know the person please tell me*/    Static classstatelist{Private StaticList<string> statelist =NewArraylist<>(); }        //------------------------------------------------------Getter Setter-------------------------------------------------------------------------         PublicString GetCode () {returnCode; }     Public voidSetcode (String code) { This. Code =Code; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }}

 PackageStateMachine;/** *  * @authorZhenweilai * Entity*/ Public classEntity {//Status Code    PrivateString State; /*** Get Status code translation *@return     */     PublicString Getstatename () {returnStateenum.getnamebycode ( This. State); }         PublicString getState () {returnState ; }         Public voidsetState (String state) { This. State =State ; }}

 PackageStateMachine;/** *  * @authorZhenweilai **/ Public classTestClass {/*** Test method *@paramargs*/     Public Static voidMain (string[] args) {Entity entity=NewEntity ();        StateEnum.READY.submit (entity);        System.out.println (Entity.getstatename ());        StateEnum.START.submit (entity);        System.out.println (Entity.getstatename ());        StateEnum.RUN.submit (entity);        System.out.println (Entity.getstatename ());        StateEnum.STOP.submit (entity);                System.out.println (Entity.getstatename ());        StateEnum.STOP.undo (entity);        System.out.println (Entity.getstatename ());        StateEnum.RUN.undo (entity);        System.out.println (Entity.getstatename ());        StateEnum.START.undo (entity);        System.out.println (Entity.getstatename ());        StateEnum.READY.undo (entity);        System.out.println (Entity.getstatename ()); }}

Console output Results:

Ready to submit the finishing touches ... Ready to start running stop running start undo finishing work ... Get ready

Enum enumeration writes a simple state machine

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.