设计模式(二十四)------23种设计模式(16):状态模式


使用频率:★★★☆☆

一、什么是状态模式

一个对象的行为根据其内部状态的改变自动变化;

二、补充说明

结构与策略模式基本一致;

与策略模式区别:使用策略模式时,客户端手动选择策略,使用状态模式时,其行为是根据状态是自动切换的。

其内部状态改变时,它的行为(方法)也跟着改变,看起来就像修改了类的方法;

三、角色

抽象状态

具体状态

环境:定义客户端感兴趣的方法,其内部有一个状态接口,运行时可以改变;

客户端

四、例子,JAVA实现

说明:还是策略模式的例子,只不过这个人比较特殊,他喜欢一天走路上班,另一天开车上班...

两种状态:走路上班状态和开车上班状态,运行时可以改变

抽象状态

package com.pichen.dp.behavioralpattern.state;

public interface IState {

    public void execute(StateContext context);
}

具体状态

复制代码
package com.pichen.dp.behavioralpattern.state;

public class DriveState implements IState{

    @Override
    public void execute(StateContext context) {
        System.out.println("driving。。。");
        context.setState(new WalkState());
    }

}
复制代码
复制代码
package com.pichen.dp.behavioralpattern.state;

public class WalkState implements IState{

    @Override
    public void execute(StateContext context) {
        System.out.println("walking。。。");
        context.setState(new DriveState());
    }

}
复制代码

环境:

复制代码
package com.pichen.dp.behavioralpattern.state;

public class StateContext {

    IState state;
    
    public StateContext(IState state) {
        this.state = state;
    }

    public void execute(){
        this.state.execute(this);
    }
    /**
     * @return the state
     */
    public IState getState() {
        return state;
    }

    /**
     * @param state the state to set
     */
    public void setState(IState state) {
        this.state = state;
    }
    



}
复制代码

客户端

复制代码
package com.pichen.dp.behavioralpattern.state;

public class Main {
    public static void main(String[] args) {
        StateContext stateContext = new StateContext(new WalkState());
        stateContext.execute();
        stateContext.execute();
        stateContext.execute();
        stateContext.execute();
        stateContext.execute();
    }
}
复制代码

执行结果:用户的上班行为状态在运行时自动切换

walking。。。
driving。。。
walking。。。
driving。。。
walking。。。

 


@Author       风一样的码农
@HomePageUrl  http://www.cnblogs.com/chenpi/ 
@Copyright       转载请注明出处,谢谢~ 

使用频率:★★★☆☆

一、什么是状态模式

一个对象的行为根据其内部状态的改变自动变化;

二、补充说明

结构与策略模式基本一致;

与策略模式区别:使用策略模式时,客户端手动选择策略,使用状态模式时,其行为是根据状态是自动切换的。

其内部状态改变时,它的行为(方法)也跟着改变,看起来就像修改了类的方法;

三、角色

抽象状态

具体状态

环境:定义客户端感兴趣的方法,其内部有一个状态接口,运行时可以改变;

客户端

四、例子,JAVA实现

说明:还是策略模式的例子,只不过这个人比较特殊,他喜欢一天走路上班,另一天开车上班...

两种状态:走路上班状态和开车上班状态,运行时可以改变

抽象状态

package com.pichen.dp.behavioralpattern.state;

public interface IState {

    public void execute(StateContext context);
}

具体状态

复制代码
package com.pichen.dp.behavioralpattern.state;

public class DriveState implements IState{

    @Override
    public void execute(StateContext context) {
        System.out.println("driving。。。");
        context.setState(new WalkState());
    }

}
复制代码
复制代码
package com.pichen.dp.behavioralpattern.state;

public class WalkState implements IState{

    @Override
    public void execute(StateContext context) {
        System.out.println("walking。。。");
        context.setState(new DriveState());
    }

}
复制代码

环境:

复制代码
package com.pichen.dp.behavioralpattern.state;

public class StateContext {

    IState state;
    
    public StateContext(IState state) {
        this.state = state;
    }

    public void execute(){
        this.state.execute(this);
    }
    /**
     * @return the state
     */
    public IState getState() {
        return state;
    }

    /**
     * @param state the state to set
     */
    public void setState(IState state) {
        this.state = state;
    }
    



}
复制代码

客户端

复制代码
package com.pichen.dp.behavioralpattern.state;

public class Main {
    public static void main(String[] args) {
        StateContext stateContext = new StateContext(new WalkState());
        stateContext.execute();
        stateContext.execute();
        stateContext.execute();
        stateContext.execute();
        stateContext.execute();
    }
}
复制代码

执行结果:用户的上班行为状态在运行时自动切换

walking。。。
driving。。。
walking。。。
driving。。。
walking。。。

 


@Author       风一样的码农
@HomePageUrl  http://www.cnblogs.com/chenpi/ 
@Copyright       转载请注明出处,谢谢~ 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值