首页 > 代码库 > State状态方法模式

State状态方法模式

State.java

package edu.state;

public abstract class State {
    public abstract void doSomething(Person person);
}

LState.java

package edu.state;

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();
        }
    }

}

MState.java

package edu.state;

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();
        }
    }

}

NoState.java

package edu.state;

public class NoState extends State {

    @Override
    public void doSomething(Person person) {
        System.out.println(person.getHour() + "未定义");
    }

}

SState.java

package edu.state;

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();
        }
    }
}

Person.java(内部state可以转换)

package edu.state;

public class Person {
    private State state;
    private int hour;

    public int getHour() {
        return hour;
    }

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

    public Person() {
        state = new MState();
    }

    public void doSomething() {
        state.doSomething(this);
        state=new MState();
    }

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

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
    <bean id="mState" class="edu.state.MState" />
    <bean id="lState" class="edu.state.LState" />
    <bean id="sState" class="edu.state.SState" />
    <bean id="noState" class="edu.state.NoState" />
    <bean id="person" class="edu.state.Person">
        <property name="state" ref="mState" />
        <property name="hour" value="18" />
    </bean>
</beans>