简体   繁体   English

状态模式和策略模式的区别

[英]Difference between State pattern and Strategy pattern

Looking at the GoF patterns I find the similarities between State and Stategy pattern rather striking.查看 GoF 模式,我发现 State 和 Stategy 模式之间的相似性相当惊人。 Both swap out polymorphic classes to modify behavior.两者都交换了多态类来修改行为。 Anyone else found the same?有没有其他人发现同样的?

What are the exact differences?确切的区别是什么?

The state and strategy patterns are similar in the sense that both of them encapsulate behavior in separate objects and use composition to delegate to the composed object to implement the behavior and both of them provide the flexibility to change the behavior dynamically by changing the composed object at run-time.状态模式和策略模式的相似之处在于它们都将行为封装在单独的对象中,并使用组合委托组合对象来实现行为,并且它们都提供了通过在以下位置更改组合对象来动态更改行为的灵活性运行时。 But there are some key differences :但是有一些关键的区别:

  1. In the state pattern, the client knows nothing about the state objects.在状态模式中,客户端对状态对象一无所知。 State changes happen transparently to the client.状态更改对客户端是透明的。 The client just calls methods on the context, the context oversees its own state.客户端只是调用上下文上的方法,上下文监督自己的状态。 Because the client is not aware of the state changes, it appears to the client as though the context is instantiated from a different class each time there is a change in behavior due to a state change.由于客户端不知道状态更改,因此每次由于状态更改而导致行为发生更改时,客户端似乎都从不同的类中实例化了上下文。 The object will appear to change its class as the official definition of the pattern states.该对象将显示更改其类作为模式状态的官方定义。 The pattern is built around a well-defined series of state transitions.该模式是围绕一系列明确定义的状态转换构建的。 Changing state is key to the pattern's existence.改变状态是模式存在的关键。

  2. Even though the strategy pattern provides the flexibility to change behavior by changing the composed strategy object dynamically, mostly there is an appropriate strategy object already set for each context.尽管策略模式提供了通过动态更改组合策略对象来更改行为的灵活性,但大多数情况下已经为每个上下文设置了适当的策略对象。 ie even though the pattern provides a way to change the composed strategy object dynamically, there won't be much of a need for it.即,即使模式提供了一种动态更改组合策略对象的方法,也没有太多需要它。 Even if it has to be done, it is the client that does the change.即使必须完成,也由客户进行更改。 The client will call the setter method on the context and pass the new strategy object.客户端将调用上下文上的 setter 方法并传递新的策略对象。 Thus behavior changes are NOT transparent to the client and are initiated and controlled by the client.因此,行为改变对客户端来说是不透明的,而是由客户端发起和控制的。 The pattern does not encourage a series of well-defined behavior changes like the state pattern.该模式不鼓励像状态模式那样的一系列明确定义的行为变化。 The client knows about the strategy objects and will usually set the appropriate strategy object in the context while creating it.客户端知道策略对象,并且通常会在创建它时在上下文中设置适当的策略对象。 The client controls what strategy object the context uses, but in the state pattern, the client knows nothing about the state object(s) that the context uses客户端控制上下文使用的策略对象,但在状态模式中,客户端对上下文使用的状态对象一无所知

    For additional information pls refer the below link http://myrandomsparks.blogspot.in/2012/05/strategy-vs-state-pattern.html有关更多信息,请参阅以下链接http://myrandomsparks.blogspot.in/2012/05/strategy-vs-state-pattern.html

The strategy pattern decides on ' how ' to perform some action and state pattern decides on ' when ' to perform them.策略模式决定“如何”执行某些操作,状态模式决定“何时”执行它们。

By using the State pattern the state-holding (context) class is relieved from knowledge of what state or type it is and what states or types that are available.通过使用状态模式,状态保持(上下文)类无需了解它是什么状态或类型以及可用的状态或类型。 This means that the class adheres to the open-closed design principle (OCP): the class is closed for changes in what states/types there are, but the states/types are open to extensions.这意味着该类遵循开放-封闭设计原则 (OCP):该类对于状态/类型的变化是封闭的,但状态/类型对扩展是开放的。

By using the Strategy pattern the algorithm-using (context) class is relieved from knowledge of how to perform a certain task (-- the "algorithm").通过使用策略模式,算法使用(上下文)类从如何执行特定任务(——“算法”)的知识中解脱出来。 This case also creates an adherence to the OCP;这个案例也创造了对 OCP 的遵守; the class is closed for changes regarding how to perform this task, but the design is very open to additions of other algorithms for solving this task该类已关闭以更改有关如何执行此任务的更改,但该设计对添加其他用于解决此任务的算法非常开放

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM