17-State pattern for 23 design Patterns in Java

Source: Internet
Author: User

Interface State
{
public void handle (StateMachine statemachine);
}

Class Eat implements State
{
StateMachine Statemachine=null;
public void handle (StateMachine statemachine)
{
System.out.println ("eat");
This.statemachine=statemachine;
This.statemachine.setState (new work ());
This.statemachine.run ();
}
}
Class work implements State
{
StateMachine Statemachine=null;
public void handle (StateMachine statemachine)
{
System.out.println ("work");
This.statemachine=statemachine;
This.statemachine.setState (New Sleep ());
This.statemachine.run ();
}
}

Class Sleep implements State
{
StateMachine Statemachine=null;
public void handle (StateMachine statemachine)
{
System.out.println ("Sleep");
}
}

Class StateMachine
{
Private State state=null;
public void SetState (state State)
{
This.state=state;
}
public void Run ()
{
State.handle (this);
}
}

public class Statepattern
{
public static void Main (string[] args)
{
State eatstate=new Eat ();
StateMachine astatemachine=new statemachine ();
Astatemachine.setstate (eatstate);
Astatemachine.run ();
}
}

17-State pattern for 23 design Patterns in Java

Related Article

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.