JAVA设计模式之【状态模式】

简介:
状态模式    水、固态、气态、液态    账户、正常状态、透支状态、受限状态    状态模式中,用一个状态类来分散冗长的条件语句,让系统有灵活性和可扩展性    状态模式用于解决系统中复杂对象的状态转换以及不同状态下行为的封装问题    角色        环境类Context            拥有多种状态的对象        抽象状态类State            定义一个接口来封装与环境类的一个特定状态相关的行为,相同的方法写在抽象状态类中        具体状态类ConcreteState            实现与环境类的一个状态相关的行为    状态转换方式        由环境类来充当状态管理器角色        由具体状态类来负责状态之间的转换

看例子

1.环境类,注册用户

package State;

/**
 * Created by Jiqing on 2016/10/29.
 */
public class ForumAccount {
    private AbstractState state;
    private String name;
    public ForumAccount(String name)
    {
        this.name=name;
        this.state=new PrimaryState(this);
        System.out.println(this.name + "注册成功!");
        System.out.println("---------------------------------------------");
    }

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

    public AbstractState getState()
    {
        return this.state;
    }

    public void setName(String name)
    {
        this.name=name;
    }

    public String getName()
    {
        return this.name;
    }

    public void downloadFile(int score)
    {
        state.downloadFile(score);
    }

    public void writeNote(int score)
    {
        state.writeNote(score);
    }

    public void replyNote(int score)
    {
        state.replyNote(score);
    }
}

2.抽象状态类

package State;

/**
 * Created by Jiqing on 2016/10/29.
 */
public abstract class AbstractState
{
    protected ForumAccount acc;
    protected int point;
    protected String stateName;
    public abstract void checkState(int score);

    public void downloadFile(int score)
    {
        System.out.println(acc.getName() + "下载文件,扣除" + score + "积分。");
        this.point-=score;
        checkState(score);
        System.out.println("剩余积分为:" + this.point + ",当前级别为:" + acc.getState().stateName + "。");
    }

    public void writeNote(int score)
    {
        System.out.println(acc.getName() + "发布留言" + ",增加" + score + "积分。");
        this.point+=score;
        checkState(score);
        System.out.println("剩余积分为:" + this.point + ",当前级别为:" + acc.getState().stateName + "。");
    }

    public void replyNote(int score)
    {
        System.out.println(acc.getName() + "回复留言,增加" + score + "积分。");
        this.point+=score;
        checkState(score);
        System.out.println("剩余积分为:" + this.point + ",当前级别为:" + acc.getState().stateName + "。");
    }

    public void setPoint(int point) {
        this.point = point;
    }

    public int getPoint() {
        return (this.point);
    }

    public void setStateName(String stateName) {
        this.stateName = stateName;
    }

    public String getStateName() {
        return (this.stateName);
    }
}

3.具体状态类

package State;

/**
 * Created by Jiqing on 2016/10/29.
 */
public class HighState extends AbstractState
{
    public HighState(AbstractState state)
    {
        this.acc=state.acc;
        this.point=state.getPoint();
        this.stateName="专家";
    }

    public void writeNote(int score)
    {
        System.out.println(acc.getName() + "发布留言" + ",增加" + score + "*2个积分。");
        this.point+=score*2;
        checkState(score);
        System.out.println("剩余积分为:" + this.point + ",当前级别为:" + acc.getState().stateName + "。");
    }

    public void downloadFile(int score)
    {
        System.out.println(acc.getName() + "下载文件,扣除" + score + "/2积分。");
        this.point-=score/2;
        checkState(score);
        System.out.println("剩余积分为:" + this.point + ",当前级别为:" + acc.getState().stateName + "。");    }

    public void checkState(int score)
    {
        if(point<0)
        {
            System.out.println("余额不足,文件下载失败!");
            this.point+=score;
        }
        else if(point<=100)
        {
            acc.setState(new PrimaryState(this));
        }
        else if(point<=1000)
        {
            acc.setState(new MiddleState(this));
        }
    }
}
package State;

/**
 * Created by Jiqing on 2016/10/29.
 */
public class MiddleState extends AbstractState
{
    public MiddleState(AbstractState state)
    {
        this.acc=state.acc;
        this.point=state.getPoint();
        this.stateName="高手";
    }

    public void writeNote(int score)
    {
        System.out.println(acc.getName() + "发布留言" + ",增加" + score + "*2个积分。");
        this.point+=score*2;
        checkState(score);
        System.out.println("剩余积分为:" + this.point + ",当前级别为:" + acc.getState().stateName + "。");
    }

    public void checkState(int score)
    {
        if(point>=1000)
        {
            acc.setState(new HighState(this));
        }
        else if(point<0)
        {
            System.out.println("余额不足,文件下载失败!");
            this.point+=score;
        }
        else if(point<=100)
        {
            acc.setState(new PrimaryState(this));
        }
    }
}
package State;

/**
 * Created by Jiqing on 2016/10/29.
 */
public class PrimaryState extends AbstractState{
    public PrimaryState(AbstractState state)
    {
        this.acc=state.acc;
        this.point=state.getPoint();
        this.stateName="新手";
    }

    public PrimaryState(ForumAccount acc)
    {
        this.point=0;
        this.acc=acc;
        this.stateName="新手";
    }

    public void downloadFile(int score)
    {
        System.out.println("对不起," + acc.getName() + ",您没有下载文件的权限!");
    }

    public void checkState(int score)
    {
        if(point>=1000)
        {
            acc.setState(new HighState(this));
        }
        else if(point>=100)
        {
            acc.setState(new MiddleState(this));
        }
    }
}

4.客户端执行

package State;

/**
 * Created by Jiqing on 2016/10/29.
 */
public class Client
{
    public static void main(String args[])
    {
        ForumAccount account=new ForumAccount("张三");
        account.writeNote(20);
        System.out.println("--------------------------------------");
        account.downloadFile(20);
        System.out.println("--------------------------------------");
        account.replyNote(100);
        System.out.println("--------------------------------------");
        account.writeNote(40);
        System.out.println("--------------------------------------");
        account.downloadFile(80);
        System.out.println("--------------------------------------");
        account.downloadFile(150);
        System.out.println("--------------------------------------");
        account.writeNote(1000);
        System.out.println("--------------------------------------");
        account.downloadFile(80);
        System.out.println("--------------------------------------");
    }
}

执行结果:

张三注册成功!
---------------------------------------------
张三发布留言,增加20积分。
剩余积分为:20,当前级别为:新手。
--------------------------------------
对不起,张三,您没有下载文件的权限!
--------------------------------------
张三回复留言,增加100积分。
剩余积分为:120,当前级别为:高手。
--------------------------------------
张三发布留言,增加40*2个积分。
剩余积分为:200,当前级别为:高手。
--------------------------------------
张三下载文件,扣除80积分。
剩余积分为:120,当前级别为:高手。
--------------------------------------
张三下载文件,扣除150积分。
余额不足,文件下载失败!
剩余积分为:120,当前级别为:高手。
--------------------------------------
张三发布留言,增加1000*2个积分。
剩余积分为:2120,当前级别为:专家。
--------------------------------------
张三下载文件,扣除80/2积分。
剩余积分为:2080,当前级别为:专家。
--------------------------------------

看关系图


本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6009834.html,如需转载请自行联系原作者

相关文章
|
Java Spring
【注解】Spring AOP 面向切面编程之@Around的详细用法
【注解】Spring AOP 面向切面编程之@Around的详细用法
3441 0
|
监控 Java 双11
重试利器之Guava Retrying
重试利器之Guava Retrying
751 0
重试利器之Guava Retrying
|
2天前
|
数据采集 人工智能 安全
|
11天前
|
云安全 监控 安全
|
3天前
|
自然语言处理 API
万相 Wan2.6 全新升级发布!人人都能当导演的时代来了
通义万相2.6全新升级,支持文生图、图生视频、文生视频,打造电影级创作体验。智能分镜、角色扮演、音画同步,让创意一键成片,大众也能轻松制作高质量短视频。
1019 151
|
3天前
|
编解码 人工智能 机器人
通义万相2.6,模型使用指南
智能分镜 | 多镜头叙事 | 支持15秒视频生成 | 高品质声音生成 | 多人稳定对话
|
16天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1713 9
|
8天前
|
人工智能 自然语言处理 API
一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸
一句话生成拓扑图!next-ai-draw-io 结合 AI 与 Draw.io,通过自然语言秒出架构图,支持私有部署、免费大模型接口,彻底解放生产力,绘图效率直接爆炸。
654 152
|
10天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
620 12