A
package com.maom.design.D_23_State.A;
/**
* @Title: D_23_State_A
* @Description: 23_状态模式
* @Author maohb
* @Version 1.0
*/
public class D_23_State_A {
public static void main(String[] args) {
Person person = new Person();
person.setHour(777);
person.doSomething();
person.setHour(7);
person.doSomething();
person.setHour(12);
person.doSomething();
}
}
class Person {
private int hour;
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public void doSomething(){
if (hour == 7)
System.out.println("A_吃早餐");
else if (hour == 12)
System.out.println("B_吃午餐");
else if (hour == 18)
System.out.println("C_吃晚饭");
else
System.out.println("No_未定义" + hour);
}
}
B
package com.maom.design.D_23_State.B;
/**
* @Title: D_23_State_B
* @Description: 23_状态模式
* @Author maohb
* @Version 1.0
*/
public class D_23_State_B {
public static void main(String[] args) {
Person person = new Person();
person.setHour(12);
person.doSomething();
person.setHour(7);
person.doSomething();
person.setHour(777);
person.doSomething();
person.setHour(12);
person.doSomething();
}
}
class Person {
private int hour;
private State state;
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public void doSomething(){
if(hour == 7) {
state = new State_A();
state.doSomething();
} else if(hour == 12) {
state = new State_B();
state.doSomething();
} else if(hour == 18) {
state = new State_C();
state.doSomething();
} else {
state = new NoState();
state.doSomething();
}
}
}
/** 状态行为抽象类 */
abstract class State {
public abstract void doSomething();
}
class State_A extends State {
public void doSomething() {
System.out.println("A_吃早餐");
}
}
class State_B extends State{
public void doSomething() {
System.out.println("B_吃午餐");
}
}
class State_C extends State {
public void doSomething() {
System.out.println("C_吃晚饭");
}
}
class NoState extends State {
public void doSomething() {
System.out.println("No_未定义");
}
}
C
package com.maom.design.D_23_State.C;
/**
* @Title: D_23_State_C
* @Description: 23_状态模式
* @Author maohb
* @Version 1.0
*/
public class D_23_State_C {
public static void main(String[] args) {
Person person = new Person();
person.setHour(7);
person.doSomething();
person.setHour(12);
person.doSomething();
person.setHour(777);
person.doSomething();
person.setHour(12);
person.doSomething();
}
}
class Person {
private int hour;
private State state;
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public Person() {
// 初始化 状态位
state = new State_A();
}
public void doSomething(){
state.doSomething(this);
// 恢复初始状态位
state = new State_A();
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
}
/** 状态行为抽象类 */
abstract class State {
public abstract void doSomething(Person person);
}
class State_A extends State {
public void doSomething(Person person) {
if(person.getHour() == 7) {
System.out.println("A_吃早餐");
} else {
person.setState(new State_B());
person.doSomething();
}
}
}
class State_B extends State{
public void doSomething(Person person) {
if(person.getHour() == 12) {
System.out.println("B_吃午餐");
} else {
person.setState(new State_C());
person.doSomething();
}
}
}
class State_C extends State {
public void doSomething(Person person) {
if(person.getHour() == 18) {
System.out.println("C_吃晚饭");
} else {
person.setState(new NoState());
person.doSomething();
}
}
}
class NoState extends State {
public void doSomething(Person person) {
System.out.println("No_未定义" + person.getHour() );
}
}