zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    BZOJ 1854 [Scoi2010]游戏
    【模板】二分图匹配-匈牙利算法
    BZOJ 1432 [ZJOI2009]Function
    BZOJ 1192 [HNOI2006]鬼谷子的钱袋
    BZOJ 1088 [SCOI2005]扫雷Mine
    BZOJ 1047 [HAOI2007]理想的正方形
    BZOJ 1034 [ZJOI2008]泡泡堂BNB
    BZOJ 1022 [SHOI2008]小约翰的游戏John
    LOJ 6278 数列分块入门2
    【BZOJ 1003】[ZJOI2006]物流运输(Dijkstra+DP)
  • 原文地址:https://www.cnblogs.com/jianfengyun/p/3735078.html
Copyright © 2011-2022 走看看