一、什么是状态模式
State模式也叫状态模式,是行为设计模式的 一种。State模式允许通过改变对象的内部状态 而改变对象的行为,这个对象表现得就好像修改
了它的类一样。
二、状态模式的应用场景
状态模式主要解决的是当控制一个对象状态转 换的条件表达式过于复杂时的情况。把状态的判 断逻辑转译到表现不同状态的一系列类当中,可
以把复杂的判断逻辑简化。
三、状态模式的结构
四、状态模式的角色和职责
Context:用户对象 拥有一个State类型的成员,以标识对象的当前 状态; State:接口或基类
封装与Context的特定状态相关的行为; ConcreteState:接口实现类或子类 实现了一个与Context某个状态相关的行为。
//示例1:
public class Person {
private int hour;
public void setHour(int hour) {
this.hour = hour;
}
public int getHour() {
return hour;
}
public void doSomething()
{
if(hour == 7)
{
System.out.println("吃早餐");
} else if(hour == 12) {
System.out.println("吃中饭");
} else if(hour == 18) {
System.out.println("吃晚饭");
}
}
}
public class MainClass {
public static void main(String[] args) {
Person person = new Person();
person.setHour(7);
person.doSomething();
person.setHour(12);
person.doSomething();
person.setHour(18);
person.doSomething();
}
}
//吃早餐
//吃中饭
//吃晚饭
//
//示例2:
public abstract class State {
public abstract void doSomething();
}
public class MState extends State {
@Override
public void doSomething() {
System.out.println("吃早餐");
}
}
public class LState extends State {
@Override
public void doSomething() {
System.out.println("吃中饭");
}
}
public class SState extends State {
@Override
public void doSomething() {
System.out.println("吃晚饭");
}
}
public class NoState extends State {
@Override
public void doSomething() {
System.out.println("未定义");
}
}
public class Person {
private int hour;
private State state;
public void setHour(int hour) {
this.hour = hour;
}
public int getHour() {
return hour;
}
public void doSomething()
{
if(hour == 7)
{
state = new MState();
state.doSomething();
} else if(hour == 12) {
state = new LState();
state.doSomething();
} else if(hour == 18) {
state = new SState();
state.doSomething();
} else {
state = new NoState();
state.doSomething();
}
}
}
public class MainClass {
public static void main(String[] args) {
Person person = new Person();
person.setHour(7);
person.doSomething();
person.setHour(12);
person.doSomething();
person.setHour(18);
person.doSomething();
person.setHour(8);
person.doSomething();
}
}
//吃早餐
//吃中饭
//吃晚饭
//未定义
//示例3:
public abstract class State {
public abstract void doSomething(Person person);
}
public class MState extends State {
@Override
public void doSomething(Person person) {
if(person.getHour() == 7)
System.out.println("吃早餐");
else {
person.setState(new LState());
person.doSomething();
}
}
}
public class LState extends State {
@Override
public void doSomething(Person person) {
if(person.getHour() == 12)
System.out.println("吃中饭");
else {
person.setState(new SState());
person.doSomething();
}
}
}
public class SState extends State {
@Override
public void doSomething(Person person) {
if(person.getHour() == 18)
System.out.println("吃晚饭");
else {
person.setState(new NoState());
person.doSomething();
}
}
}
public class NoState extends State {
@Override
public void doSomething(Person person) {
System.out.println("未定义");
}
}
public class Person {
private int hour;
private State state = new MState();
public void setState(State state) {
this.state = state;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getHour() {
return hour;
}
public void doSomething()
{
state.doSomething(this);
}
}
public class MainClass {
public static void main(String[] args) {
Person person = new Person();
person.setHour(7);
person.doSomething();
person.setHour(12);
person.doSomething();
person.setHour(18);
person.doSomething();
person.setHour(8);
person.doSomething();
}
}
//吃早餐
//吃中饭
//吃晚饭
//未定义