State mode state
Scene: When having a plurality of state and require frequent change, with this pattern
- operation of the elevator: service, normal, self-closing, automatic door, running up, running down, the state of the fire
- traffic lights: red, yellow, green
- business or government system: the approval of the state documents
- When shopping online, the status of orders: orders, paid, delivery, a receipt has been
Core : packaging problems for solving the system state conversion and complex object behavior under different conditions
Structure: Context Environmental context (maintaining a State object defines the current state), the state of the abstract class State, state ConcreteState concrete class (each class encapsulates a state corresponding behavior)
Develop common scenarios:
- Management system in the state bank account
-OA document management system in state
- hotel systems, room status management
- switching between each thread object state
Examples: Different hotel room status
State diagram
public interface State {
void handler();
}
class FreeState implements State{
@Override
public void handler() {
System.out.println("空闲房间,可入住");
}
}
class Booked implements State{
@Override
public void handler() {
System.out.println("已预定");
}
}
class Checked implements State{
@Override
public void handler() {
System.out.println("已入住");
}
}
/ **
* context class
* Room
* @author sail entering code
*
* /
public class RoomContext {
Private State State;
public void the setState (State S) {
State = S;
System.out.println ( "modification status!") ;
state.handler ();
}
}
RoomContext rc=new RoomContext();
rc.setState(new FreeState());
rc.setState(new Booked());
}