关于Strategy和State设计模式

之前,我在描述我所采用的设计模式时,一直在Strategy和State之间犹豫,略微有些拿捏不准,说哪种设计模式好。结果到最后,会根据自己所想,觉得是State就是State,觉得Strategy就是Strategy,不过,说的时候,还是会带有些犹豫。最近看Martin Fowler的《重构,改善既有代码的设计》一书,上面说到:

At this stage the choice of pattern (and name) reflects how you want to think about the structure. At the moment I'm thinking about this as a state of movie(当前模块是描述movie的一个模块). If I later decide a strategy communicates my intention better, I will refactor to do this by changing the names.

该书在Replace Type Code with State/Strategy上写道:

State and strategy are very similar, so the refactoring is the same whichever you use, and it doesn't really matter. Choose the pattern that better fits the specific circumstances. If you are trying to simplify a single algorithm with Replace Conditional with Polymorphism, strategy is the better term. If you are going to move state-specific data and you think of the object as changing state, use the state pattern. 

所以,在我看来,对于使用State或者Strategy来说,没有必要纠结。你可以认为你使用的State或者Strategy,只要你可以熟练使用。在交流的时候,知道State或者Strategy基本是等价的,所以不要太在意别人怎么说。对外,你可以说你使用了State设计模式,或者Strategy设计模式,或者State/Strategy设计模式就可以了。

转载于:https://www.cnblogs.com/albizzia/p/9130978.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值