Design mode -- state mode (JAVA)

Posted by Johnlbuk on Fri, 05 Jun 2020 06:22:04 +0200

Model introduction

/**
 * 
 */
/**
 *State mode
 *Motivation: allows an object to change its behavior when its internal state changes, and the object appears to modify its class.
 *Problem solved: allows an object to change its behavior when its internal state changes, and the object appears to modify its class.
 *Advantages: 
 *1. Encapsulate the transformation rules. 
 *2. Enumerate possible states. Before enumerating States, you need to determine the state type. 
 *3. Put all behaviors related to a state into a class, and easily add new states. Only changing the state of the object can change the behavior of the object. 
 *4. Allow state transformation logic and state object to be integrated, rather than a huge conditional statement block. 
 *5. Multiple environment objects can share a state object, thus reducing the number of objects in the system.
 *Disadvantages: 
 *1. The use of state mode will inevitably increase the number of system classes and objects. 
 *2. The structure and implementation of state mode are complex. If it is not used properly, it will lead to confusion of program structure and code. 
 *3. The state mode does not support the "open close principle". For the state mode that can be switched, adding a new state class requires modifying the source code responsible for the state transition, otherwise it cannot be switched to the new state, and modifying the behavior of a state class also requires modifying the source code of the corresponding class.
 *Usage scenario: 
 *1. The scene where the behavior changes with the change of state. 
 *2. Substitute of condition and branch statement.
 * 
 * @author 
 *
 */

example

/**
 * Problem introduction
 * A person's status is different at different times of the day
 * I am in good condition in the morning. I want to go to bed at noon. I gradually recover in the afternoon. I have to work overtime.
 */
public class Demo1 {
	public static void main(String[] args) {
		Work project = new Work();
		project.setHour(8);
		project.WriterProgram();
		project.setHour(10);
		project.WriterProgram();
		project.setHour(12);
		project.WriterProgram();
		project.setHour(14);
		project.WriterProgram();
		project.setFinish(false);
		project.setHour(17);
		project.WriterProgram();
		project.setHour(22);
		project.WriterProgram();
	}
}
class Work{
	private int hour;
	private Boolean finish =false;
	public int getHour() {
		return hour;
	}
	public void setHour(int hour) {
		this.hour = hour;
	}
	public Boolean getFinish() {
		return finish;
	}
	public void setFinish(Boolean finish) {
		this.finish = finish;
	}
	public void WriterProgram() {
		if(hour<12) {
			System.out.println("Current time:"+hour+"O'clock, morning, spirit");
		}else if(hour<13) {
			System.out.println("Current time:"+hour+"O'clock, noon, eating and sleeping");
		}else if(hour<17) {
			System.out.println("Current time:"+hour+"It's a good afternoon");
		}else {
			if(finish) {
				System.out.println("Current time:"+hour+"It's time to go home");
			}else {
				if(hour<21) {
					System.out.println("Current time:"+hour+"It's time to go to sleep");
				}else {
					System.out.println("Current time:"+hour+"Point. I can't hold it. I'm asleep");
				}
			}
		}
	}
}
/*
 * It's easy to find out what's wrong with the code
 * writerprogram Too much judgment in the method means too much responsibility
 * It violates the principle of single responsibility and makes it necessary to modify this method for any change of requirements
 * The risk of maintenance error is very high, which violates the open and closed principle.
 */

/*
 * State mode
 */
public class Demo2 {
	public static void main(String[] args) {
		Context c =new Context(new ConcreteStateA());
		c.Request();
		c.Request();
		c.Request();
		c.Request();
	}
}
//State class
abstract class State{
	public abstract void Handle(Context context);
}
//Specific status class
class ConcreteStateA extends State{

	@Override
	public void Handle(Context context) {
		// TODO Auto-generated method stub
		context.setState(new ConcreteStateB());
	}
	
}
class ConcreteStateB extends State{

	@Override
	public void Handle(Context context) {
		// TODO Auto-generated method stub
		context.setState(new ConcreteStateA());
	}
	
}
//Context class, maintaining an instance of a ConcreteState subclass
class Context{
	private State state;
	public Context(State state) {
		this.state = state;
	}
	public State getState() {
		return state;
	}
	public void setState(State state) {
		this.state = state;
		System.out.println("Current status:"+state.toString());
	}
	public void Request() {
		state.Handle(this);
	}
}

/*
 * State mode to realize working state in different periods
 */
public class Demo3 {
	public static void main(String[] args) {
		Context_Work project = new Context_Work();
		project.setHour(8);
		project.WriterProgram();
		project.setHour(10);
		project.WriterProgram();
		project.setHour(12);
		project.WriterProgram();
		project.setHour(14);
		project.WriterProgram();
		//project.setFinish(true);
		project.setFinish(false);
		project.setHour(17);
		project.WriterProgram();
		project.setHour(22);
		project.WriterProgram();
	}
}
interface State{
	void writeProgram(Context_Work w);
}

class shangwuState implements State{

	@Override
	public void writeProgram(Context_Work w) {
		// TODO Auto-generated method stub
		if(w.getHour()<12) {
			System.out.println("Current time:"+w.getHour()+"O'clock, morning, full of energy");
		}else {
			w.setState(new zhongwuState());
			w.WriterProgram();
		}
	}
	
}
class zhongwuState implements State{

	@Override
	public void writeProgram(Context_Work w) {
		// TODO Auto-generated method stub
		if(w.getHour()<13) {
			System.out.println("Current time:"+w.getHour()+"Point, noon, lunch break");
		}else {
			w.setState(new xiawuState());
			w.WriterProgram();
		}
	}
	
}

class xiawuState implements State{

	@Override
	public void writeProgram(Context_Work w) {
		// TODO Auto-generated method stub
		if(w.getHour()<17) {
			System.out.println("Current time:"+w.getHour()+"O'clock, afternoon, keep working");
		}else {
			w.setState(new bangwanState());
			w.WriterProgram();
		}
	}
	
}
class bangwanState implements State{

	@Override
	public void writeProgram(Context_Work w) {
		// TODO Auto-generated method stub
		if(w.isFinish()) {
			w.setState(new xiabanState());
			w.WriterProgram();
		}else {
			if(w.getHour()<21) {
				System.out.println("Current time:"+w.getHour()+"Work overtime");

			}else {
				w.setState(new shuijiaoState());
				w.WriterProgram();
			}
		}
	}
	
}
class shuijiaoState implements State{

	@Override
	public void writeProgram(Context_Work w) {
		// TODO Auto-generated method stub
		System.out.println("Current time:"+w.getHour()+"Go to sleep");
	}
	
}
class xiabanState implements State{

	@Override
	public void writeProgram(Context_Work w) {
		// TODO Auto-generated method stub
		System.out.println("Current time:"+w.getHour()+"Go home from work");
	}
	
}
class Context_Work{
	private State state;
	private int hour;
	private boolean finish =false;
	
	public Context_Work() {
		state = new shangwuState();
	}

	public State getState() {
		return state;
	}

	public void setState(State state) {
		this.state = state;
	}

	public int getHour() {
		return hour;
	}

	public void setHour(int hour) {
		this.hour = hour;
	}

	public boolean isFinish() {
		return finish;
	}

	public void setFinish(boolean finish) {
		this.finish = finish;
	}
	public void WriterProgram() {
		state.writeProgram(this);
	}
}