随笔 - 113  文章 - 0  评论 - 0  阅读 - 66063

设计模式——状态模式

一、定义与简单实现

1、定义

状态模式,是一种行为型模式,允许对象在内部状态改变时改变它的行为

 

2、UML图

 

状态模式的UML类图与策略模式的UML类图一模一样,维基百科上也称状态模式可解释成策略模式两者都定义算法族,分别封装起来,让它们之间可以互相替换,使得程序更具有弹性

但两种模式区别就在于它们的“意图”。

策略模式:定义多个算法实现,通常由客户主动指定某一个具体的算法实现。

状态模式:定义多个状态实现,随着时间流逝,会自动转换状态实现,改变对象的行为,而不需要客户主动指定算法的实现对象。

状态模式经常被用来实现内部状态自动装换的逻辑(状态机)。

3、简单实现

自动状态转换,一次小写,两次大写循环。不需要客户设置。

复制代码
public interface State {
    void writeName(StateContext context, String name);
}

public class LowerCaseState implements State {

    private State state;

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

    @Override
    public void writeName(StateContext context, String name) {
        System.out.println(name.toLowerCase());
        //转换两次大写
        context.setState(state == null ? new MultipleUpperCaseState(): state);
    }
}

public class MultipleUpperCaseState implements State {
    private int count = 0;
    private State state;

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

    @Override
    public void writeName(StateContext context, String name) {
        System.out.println(name.toUpperCase());
        if(++count > 1){
count = 0;
//两次大写后转换到一次小写 context.setState(state == null ? new LowerCaseState() : state); } } } public class StateContext { private State state; public StateContext(){ this.state = new LowerCaseState(); } public void setState(State state){ this.state = state; } public void writeName(String name){ state.writeName(this,name); } } public class Main { public static void main(String[] args) { StateContext context = new StateContext(); context.writeName("Monday"); context.writeName("Tuesday"); context.writeName("Wednesday"); context.writeName("Thursday"); context.writeName("Friday"); context.writeName("Saturday"); context.writeName("Sunday"); } }
复制代码

算法的灵活性,可动态增加算法实现,例如新增一个不转化大小写实现,然后改变状态,一次小写,一次不变,两次大写 循环

不需要修改原状态代码。

复制代码
public class NoCaseState implements State {

    private State state;

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

    @Override
    public void writeName(StateContext context, String name) {
        System.out.println(name);
        context.setState(state == null ? new LowerCaseState() : state);
    }
}

public class Main {
    public static void main(String[] args) {
        //一次小写,一次不变,两次大写 循环
        StateContext context = new StateContext();
        LowerCaseState lowerCaseState = new LowerCaseState();
        MultipleUpperCaseState multipleUpperCaseState = new MultipleUpperCaseState();
        multipleUpperCaseState.setState(lowerCaseState);
        NoCaseState noCaseState = new NoCaseState();
        lowerCaseState.setState(noCaseState);
        noCaseState.setState(multipleUpperCaseState);
        context.setState(lowerCaseState);
        context.writeName("Monday");
        context.writeName("Tuesday");
        context.writeName("Wednesday");
        context.writeName("Thursday");
        context.writeName("Friday");
        context.writeName("Saturday");
        context.writeName("Sunday");
        context.writeName("Monday");
    }
}
复制代码

二、框架中的状态模式

 暂时没有发现

posted on   FFStayF  阅读(462)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示