state mode of JAVA design pattern

State mode: Action based on status When the action is ce...

State mode:

  • Action based on status
  • When the action is certain but the state can be extended, it is suitable for using the state mode
  • When movement changes don't fit
  • There is no need to use traditional switch when the state does not extend.

Let's first look at an example of not using state mode:

package com.srr.dp.state; /** * My girlfriend has many states * When adding a new state to my girlfriend, a new branch judgment needs to be added to the switch statement of all the following methods * In short, this implementation is inconvenient when adding new states */ public class GirFriend { private String name; private enum State private State state; private void smile(){ switch (state){ case HAPPY: System.out.println("Ha ha ha ha"); break; case NORMAL: System.out.println("A smile"); default: System.out.println(); } } private void cry(){ switch (state){ case HAPPY: System.out.println("Smile with tears, moved to cry"); break; case NORMAL: System.out.println("Whoa, whoa, whoa, whoa"); break; default: System.out.println(); } } private void say(){ switch (state){ case HAPPY: System.out.println("Talk with fun and humor"); break; case NORMAL: System.out.println("Be quiet and seldom talk"); default: System.out.println(); } } }

After using the status mode, the code is as follows:

package com.srr.dp.state; /** * State abstract class */ abstract public class State { abstract void smile(); abstract void cry(); abstract void say(); } package com.srr.dp.state; /** * Girlfriend's normal state */ public class NormalState extends State { @Override void smile() { System.out.println("A smile"); } @Override void cry() { System.out.println("Whoa, whoa, whoa, whoa"); } @Override void say() { System.out.println("Be quiet and seldom talk"); } } package com.srr.dp.state; /** * Girlfriend happy state */ public class HappyState extends State { @Override void smile() { System.out.println("Ha ha ha ha"); } @Override void cry() { System.out.println("Smile with tears, moved to cry"); } @Override void say() { System.out.println("Talk with fun and humor"); } } package com.srr.dp.state; /** * When adding a new state, you only need to add a new state class to inherit the abstract state class */ public class GirFriend2 { private String name; private State state; public GirFriend2(String name,State state){ this.name = name; this.state = state; } public void smile(){ state.smile(); } public void cry(){ state.cry(); } public void say(){ state.say(); } } package com.srr.dp.state; /** * Test class */ public class T { public static void main(String[] args) { State state = new HappyState(); GirFriend2 girFriend = new GirFriend2("Cheng Xiao",state); girFriend.smile(); girFriend.cry(); girFriend.say(); } }

When my girlfriend adds a new state here, she just needs to add a new state class to inherit the abstract state class.

Operation result:

23 May 2020, 11:42 | Views: 3384

Add new comment

For adding a comment, please log in
or create account

0 comments