• 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>
  • 相关阅读:
    Class 、NSObject、id
    xcode8.2 打包问题
    JS WEB 交互问题
    C语言 关于内存动态分配问题
    Objective-C( Category 分类,非正式协议,分类延展)
    NSComparisonResul、NSNotFound、NSEnumerationOptions......的用处
    Objective-C( Foundation框架 一 常见的结构体)
    Objective-C( Foundation框架 一 数组(NSMutableArray))
    Objective-C( Foundation框架 一 数组(NSArray))
    Objective-C( Foundation框架 一 字符串)
  • 原文地址:https://www.cnblogs.com/jianfengyun/p/3735078.html
Copyright © 2020-2023  润新知