zoukankan      html  css  js  c++  java
  • 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,当前级别为:专家。
    --------------------------------------
    

    看关系图

  • 相关阅读:
    ["Visual Studio快捷键" ,"Vs","IDEA快捷键"]
    文件夹
    x
    软考.第一章-信息化和信息系统
    软考.起航篇
    Global.asax.cs 为 /.aspx 执行子请求时出错。 Server.Transfer
    网关我选 Spring Cloud Gateway
    我面向 Google 编程,他面向薪资编程
    JDK 13 都已经发布了,Java 8 依然是最爱
    Spring Cloud 系列之 Spring Cloud Stream
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/6009834.html
Copyright © 2011-2022 走看看