第二十四讲:状态模式

Posted 绿茵好莱坞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第二十四讲:状态模式相关的知识,希望对你有一定的参考价值。

改变对象的内部状态hour,然后判断内部状态而改变对象的行为.但是这样的设计是非常不合理的.

 

Context:Person.用户对象类.

黑色菱形后面一条直线表示聚合,Context持有了State的一个引用.

    private State state;//持有了State类的一个引用.

整个判断还是放在了Person里面,


 

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();
    }
}
public class Person {
    private int hour;//时间

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }
    public void doSomething(){
        if(hour==7){
            System.out.println("吃早餐");
        }else if(hour==12){
            System.out.println("吃中饭");
        }else if(hour==18){
            System.out.println("吃晚饭");
        }else{
            System.out.println(hour+"未定义");
        }
    }
    
}

 

package com.ibeifeng.ex2;

public class LState extends State{

    @Override
    public void doSomething() {
        // TODO Auto-generated method stub
        System.out.println("吃中饭");
    }

}
package com.ibeifeng.ex2;

public class MState extends State{

    @Override
    public void doSomething() {
        // TODO Auto-generated method stub
        System.out.println("吃早餐");
    }

}
package com.ibeifeng.ex2;

public class SState extends State{

    @Override
    public void doSomething() {
        // TODO Auto-generated method stub
        System.out.println("吃晚饭");
    }

}
package com.ibeifeng.ex2;

public class NoState extends State{

    @Override
    public void doSomething() {
        // TODO Auto-generated method stub
        System.out.println("未定义");
    }

}
package com.ibeifeng.ex2;

public abstract class State {
    public abstract void  doSomething();
}
package com.ibeifeng.ex2;

public class Person {
    private int hour;//时间
    private State state;
    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }
    public void doSomething(){
        if(hour==7){
            //System.out.println("吃早餐");
            //State state = new MState();
            state = new MState();
            state.doSomething();
        }else if(hour==12){
            //System.out.println("吃中饭");
            //State state = new LState();
            state = new LState();
            state.doSomething();
        }else if(hour==18){
            //System.out.println("吃晚饭");
             State state = new SState();
             state.doSomething();
        }else{
            //System.out.println(hour+"未定义");
              //State state = new NoState();
            state = new NoState();  
            state.doSomething();
        }
    }
    
}

 

package com.ibeifeng.ex2;

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

 


 

再重新调用person.setHour(7)这时候State是NoState回不去了,永远都是未定义的.


 

request()相当于doSomething()方法,调用state的doSomething()方法.Person是通过状态Hour来改变行为的.需要根据不同的状态实现不同的行为就使用状态模式.把吃早餐那些代码放到状态类里面去,然后通过持有状态类的引用调用状态类的方法.每一个状态类都有一个状态的转移,因为如果不是这个状态的话就把他转移到下一个状态器.你可以把你的状态一个一个地列出来然后转移.添加一个状态是非常容易的,把这个类实现抽象状态类方法,然后把它列到未定义状态类后面也行.


 

package com.ibeifeng.ex3;

public class LState extends State{

    @Override
    //public void doSomething() {
    public void doSomething(Person person){    
    // TODO Auto-generated method stub
        if(person.getHour()==12){
        System.out.println("吃中饭");
        }else{
            person.setState(new SState());
            person.doSomething();
        }
    }

}
package com.ibeifeng.ex3;

public class MState extends State{

    @Override
    //public void doSomething() {
    public void doSomething(Person person){    
    // TODO Auto-generated method stub
        if(person.getHour()==7){
        System.out.println("吃早餐");
        }else{
            person.setState(new LState());
            person.doSomething();
        }
    }

}
package com.ibeifeng.ex3;

public class SState extends State{

    @Override
    //public void doSomething() {
    public void doSomething(Person person){    
    // TODO Auto-generated method stub
        if(person.getHour() == 18){
        System.out.println("吃晚饭");
        } else {
            person.setState(new NoState());
            person.doSomething();
            
        }
    }

}
package com.ibeifeng.ex3;

public class NoState extends State{

    @Override
    //public void doSomething() {
    public void doSomething(Person person){    
    // TODO Auto-generated method stub
        System.out.println(person.getHour()+"未定义");
    }

}
package com.ibeifeng.ex3;

public abstract class State {
    //public abstract void  doSomething();
    public abstract void  doSomething(Person person);
}
package com.ibeifeng.ex3;

public class Person {
    private int hour;//时间
    private State state;
    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }
    
    public State getState() {
        return state;
    }

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

    public Person() {
        super();
        this.state = new MState();
    }

    public void doSomething(){
        /*if(hour==7){
            //System.out.println("吃早餐");
            //State state = new MState();
            state = new MState();
            state.doSomething();
        }else if(hour==12){
            //System.out.println("吃中饭");
            //State state = new LState();
            state = new LState();
            state.doSomething();
        }else if(hour==18){
            //System.out.println("吃晚饭");
             State state = new SState();
             state.doSomething();
        }else{
            //System.out.println(hour+"未定义");
              //State state = new NoState();
            state = new NoState();  
            state.doSomething();
        }*/
    
            
            //this.state = new MState();//每次调用一次doSomething()就重新搜索一遍,因为MState
            //是开头.
        //错误,无限循环下去了.
        state.doSomething(this);//把Person传递进去
        //this.state = new MState();
        //复位,当所有方法完成以后再执行
        state = new MState();
    }
    
}
package com.ibeifeng.ex3;

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();
        
        person.setHour(7);
        person.doSomething();
/*        person.setHour(12);
        person.doSomething();*/
        
        person.setHour(18);
        person.doSomething();
        
/*        person.setHour(8);
        person.doSomething();*/
        
    }
}

 

以上是关于第二十四讲:状态模式的主要内容,如果未能解决你的问题,请参考以下文章

设计模式(二十四)---状态模式

设计模式十四—— 状态模式/策略模式

视觉SLAM十四讲笔记 -- 第二讲

视觉SLAM十四讲笔记 -- 第二讲

视觉SLAM十四讲笔记 -- 第二讲

视觉SLAM十四讲笔记 -- 第二讲