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>
  • 相关阅读:
    JavaScript + Table 系列:排序
    asp.net 2.0中傻瓜式使用soap header
    Linq To Sql进阶系列(七)动态查询续及CLR与SQL在某些细节上的差别
    asp.net 2.0 导出DataTable到Excel中
    ASP.NET实用技巧(一)
    【ASP.NET】基础补习之FileUpload
    ASP.NET AJAX入门系列(8):自定义异常处理
    Linq To Sql进阶系列(五)Store Procedure篇
    Linq To Sql进阶系列(六)用object的动态查询与保存log篇
    深究“通过样式表实现固定表头和列”
  • 原文地址:https://www.cnblogs.com/jianfengyun/p/3735078.html
Copyright © 2011-2022 走看看