状态模式实例与解析--实例一:论坛用户等级

在某论坛系统中,用户可以发表留言,发表留言将增加积分;用户也可以回复留言,回复留言也将增加积分;用户还可以下载文件,下载文件将扣除积分。该系统用户分为三个等级,分别是新手、高手和专家,这三个等级对应三种不同的状态,这三种状态分别定义如下: (1) 如果积分小于100分,则为新手状态,用户可以发表留言、回复留言,但是不能下载文件。如果积分大于等于1000分,则转换为专家状态;如果积分大于等于100分,则转换为高手状态。 (2) 如果积分大于等于100分但小于1000分,则为高手状态,用户可以发表留言、回复留言,还可以下载文件,而且用户在发表留言时可以获取双倍积分。如果积分小于100分,则转换为新手状态;如果积分大于等于1000分,则转换为专家状态;如果下载文件后积分小于0,则不能下载该文件。 (3) 如果积分大于等于1000分,则为专家状态,用户可以发表留言、回复留言和下载文件,用户除了在发表留言时可以获取双倍积分外,下载文件只扣除所需积分的一半。如果积分小于100分,则转换为新手状态;如果积分小于1000分,但大于等于100,则转换为高手状态;如果下载文件后积分小于0,则不能下载该文件。

 

 

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

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

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

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

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

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值