The difference between state model and strategy

Looking at the GoF patterns, I find the similarities between the State and Stategy patterns quite striking. Both modify polymorphic classes to change behavior. Has anyone else found the same thing?

What are the exact differences?

+4
source share
2 answers

State templates and strategies are similar in the sense that they both encapsulate behavior in separate objects and use composition to delegate a linked object to implement the behavior, and both provide flexibility for changing behavior dynamically by changing the composed object at runtime. But there are some key differences:

  • In the state template, the client does not know anything about state objects. State changes occur transparently to the client. The client simply calls methods in the context, the context monitors its own state. Since the client does not know the state changes, it is displayed to the client as if the context was created from another class every time a behavior change due to a state change occurs. It seems that the object will change its class as the official definition of template states. The template is built around a well-defined sequence of states. State change is the key to the existence of the template.

  • Despite the fact that the strategy template provides flexibility for changing behavior by dynamically changing a structured strategic object, basically there is a corresponding strategy object that is already set for each context. those. even though the template provides a way to dynamically change the constructed strategic object, it will not be very necessary. Even if it needs to be done, the client makes changes. The client will call the setter method in context and pass the new strategy object. Therefore, behavior changes are NOT transparent to the client and are initiated and controlled by the client. A pattern does not encourage a series of well-defined behavior changes, such as a state pattern. The client knows about the strategy objects and usually creates the corresponding strategy object in the context when it is created. The client controls which strategy object the context uses, but the client does not know anything about the state object (s) that the context uses in the state template

    For more information, pls refer to the following link http://myrandomsparks.blogspot.in/2012/05/strategy-vs-state-pattern.html

+6
source

The strategy template decides " how " to perform certain actions, and the state template decides " when " to perform them.

Using the state template, the state (context) class is freed from knowing what state or type it is and what states or types that are available. This means that the class adheres to the principle of open closure (OCP): the class is closed to changes in which states / types exist, but states / types are open for extensions.

Using the strategy template, the algorithm-used (contextual) class is freed from knowledge of how to perform a specific task (- the "algorithm"). This case also creates an OCP commitment; the class is closed for changes regarding how to accomplish this task, but the design is very open for adding other algorithms to solve this problem.

0
source

Source: https://habr.com/ru/post/1495869/


All Articles