Unity常用的设计模式_状态模式

来源:互联网 发布:企业软件 编辑:程序博客网 时间:2024/06/01 12:03

1.状态模式定义

让一个对象的行为随着内部状态的变化而改变,而该对象也像换了类一样。

2.状态模式动机

在很多情况下,一个对象的行为取决于一个或多个动态变化的属性,这样的属性叫做状态,这样的对象叫做有状态的对象,这些对象状态是从事先定义好的一系列值中取出的。当一个这样的对象与外部事件产生时,其内部状态就会改变,从而使得系统行为也随之发生变化。

例如:人会根据心情的不同具有两种状态:开心和伤心,这两种状态可以相互转换。


这些状态的转换细节无须知道。在通常情况下,可以用复杂的条件判断(if......else......)(switch(state))来判断转换,但是这会导致代码的可维护性和灵活性下降,特别是出现新的状态。所以使用状态模式,可以降低状态转换之间的耦合度。

3.状态模式结构图



4.状态模式主要代码

例子:在某论坛中

(积分 < 100):新手-------------------发表留言,回复留言

(100 < 积分 < 1000):高手--------发表留言(双倍积分),回复留言,下载文件

(1000 < 积分):专家-----------------发表留言(双倍积分),回复留言,下载文件(扣一半积分)


论坛账号类

ForumAccount.cs

public class ForumAccount{private AbstractState state;/*用户状态*/private string name;/*用户姓名*/public ForumAccount(string name){this.name = name;this.state = new PrimaryState(this);Console.WriteLine(this.name + "创建成功!");}/*设置状态*/public void SetState(AbstractState state){this.state = state;}/*获取状态*/public AbstractState GetState(){return this.state;}/*获取姓名*/public string GetName(){return this.name;}/*下载文件*/public void DownLoadFile(int score){state.DownLoadFile(score);}/*发表留言*/public void WriteNote(int score){state.WriteNote(score);}/*回复留言*/public void ReplayNote(int score){state.ReplayNote(score);}}

抽象状态类(账号状态)

AbstractState.cs

public abstract class AbstractState{protected ForumAccount acc;/*账号*/protected int point;/*积分*/protected string stateName;/*状态名*//*检测下载所需分数是否足够下载文件||获得分数是否能够升级*/public abstract void checkState(int score);/*下载文件*/public void DownLoadFile(int score){Console.WriteLine(acc.GetName() + "下载文件" + "||" + "扣除" + score + "积分");this.point -= score;checkState(score);Console.WriteLine("剩余积分:" + this.point + "||" + "当前等级:" + acc.GetState().stateName);}/*发表留言*/public void WriteNote(int score){Console.WriteLine(acc.GetName() + "发表留言" + "||" + "增加积分" + score);this.point += score;checkState(score);Console.WriteLine("剩余积分:" + this.point + "||" + "当前等级:" + acc.GetState().stateName);}/*回复留言*/public void ReplayNote(int score){Console.WriteLine(acc.GetName() + "回复留言" + "||" + "增加积分" + score);this.point += score;checkState(score);Console.WriteLine("剩余积分:" + 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);}}

具体状态类

新手状态(PrimaryState.cs)

public class PrimaryState : 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){Console.WriteLine("对不起" + acc.GetName() + "你还没有下载文件的权限");}public void checkState(int score){if(point >= 1000){acc.SetState(new HighState(this));}else if(point >= 100){acc.SetState(new MiddleState(this));}}}

高手状态(MiddleState)

public class MiddleState : AbstractState{public MiddleState(AbstractState state){this.acc = state.acc;this.point = state.GetPoint();this.stateName = "高手";}/*发表留言*/public void WriteNote(int score){Console.WriteLine(acc.GetName() + "发表留言" + "||" + "增加积分" + score + "*2");this.point += score * 2;checkState(score);Console.WriteLine("剩余积分:" + this.point + "||" + "当前等级:" + acc.GetState().stateName);}public checkState(int score){if(point >= 1000){acc.SetState(new HighState(this));}else if(point < 0){Console.WriteLine("余额不足,请充值!");this.point += score;}else if(point <= 100){acc.SetState(new PrimaryState(this));}}}

专家状态类(HighState.cs)

public class HighState : AbstractState{public HighState(AbstractState state){this.acc = state.acc;this.point = state.GetPoint();this.stateName = "专家";}/*发表留言*/public void WriteNote(int score){Console.WriteLine(acc.GetName() + "发表留言" + "||" + "增加积分" + score + "*2");this.point += score * 2;checkState(score);Console.WriteLine("剩余积分:" + this.point + "||" + "当前等级:" + acc.GetState().stateName);}/*下载文件*/public void DownLoadFile(int score){Console.WriteLine(Console.WriteLine(acc.GetName() + "下载文件" + "||" + "扣除" + score + "/2" + "积分");)this.point -= score/2;checkState(score);Console.WriteLine("剩余积分:" + this.point + "||" + "当前等级:" + acc.GetState().stateName);}public void checkState(int score){if(point < 0){Console.WriteLine("余额不足,请充值!");this.point += score;}else if(point <= 100){acc.SetState(new PrimaryState(this));}else if(point <= 1000){acc.SetState(new PrimaryState(this));}}}

测试

ForumAccount accound = new ForumAccount("刘某");//输出:刘某注册成功account.WriteNote(20);/*输出:刘某发表留言||增加20积分剩余积分:20||当前等级:新手*/account.DownLoadFile(20);/*输出:对不起,刘某,你还没有下载文件的权限*/account.ReplayNote(100);/*输出:刘某回复留言||增加100积分剩余积分:120||当前等级:高手*/


5.参考

《设计模式》 主编:刘伟







原创粉丝点击