战略设计模式与国家设计模式有何区别? [英] What is the difference between Strategy design pattern and State design pattern?

查看:122
本文介绍了战略设计模式与国家设计模式有何区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c c c c c

我正在网路上浏览不少文章,但无法明确区分。



有人可以用外行人的说法解释吗?

解决方案

老实说,这两种模式在实践中非常相似,而且它们之间的定义差异往往会随着你的问题而变化。一些流行的选择是:




  • 状态存储对包含它们的上下文对象的引用。策略没有。

  • 允许国家替换自己(IE:将上下文对象的状态更改为其他内容),而策略不是。

  • 策略作为参数传递给上下文对象,而国家由上下文对象本身创建。

  • 策略只处理一个单一的特定任务,而国家提供基本的实现



经典实现将匹配状态或策略中的每个项目,列表中,但是您可以跨越具有两者混合的混合动力。一个特定的一个是更多的状态或战略y最终是一个主观问题。


What are the differences between Strategy design pattern and State design pattern?

I was going through quite a few articles on the web but could not make out the difference clearly.

Can someone please explain in layman's terms?

解决方案

Honestly, the two patterns are pretty similar in practice, and the defining difference between them tends to vary depending on who you ask. Some popular choices are:

  • States store a reference to the context object that contains them. Strategies do not.
  • States are allowed to replace themselves (IE: to change the state of the context object to something else), while Strategies are not.
  • Strategies are passed to the context object as parameters, while States are created by the context object itself.
  • Strategies only handle a single, specific task, while States provide the underlying implementation for everything (or most everything) the context object does.

A "classic" implementation would match either State or Strategy for every item on the list, but you do run across hybrids that have mixes of both. Whether a particular one is more State-y or Strategy-y is ultimately a subjective question.

这篇关于战略设计模式与国家设计模式有何区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆