Question

What´s the difference between a coupled strategy and an uncoupled strategy?

Thanks for answering.

Was it helpful?

Solution

A strategy is coupled when the strategy classes has knowledge of the context, or when the client is aware of the strategy concrete classes.

we can read here (with some minor edits):

Usually each strategy need data from the context or have to return some processed data to the context. This can be achieved in 2 ways.

  1. creating some additional classes to encapsulate the specific data.
  2. passing the context object itself to the strategy objects. The strategy object can set returning data directly in the context.

When data should be passed, the drawbacks of each method should be analyzed. For example, if some classes are created to encapsulate additional data, a special care should be paid to what fields are included in the classes. Maybe in the current implementation all required fields are added, but maybe in the future some new strategy concrete classes require data from context which are not included in those additional classes. Another fact should be specified at this point: it's very likely that some of the strategy concrete classes will not use fields passed to it in the additional classes.

If the context object is passed to the strategy then we have a coupling between strategy and context.

About the coupling between strategies and client, in the same page we read:

In the classic implementation of the pattern the client should be aware of the strategy concrete classes. In order to decouple the client class from strategy classes is possible to use a factory class inside the context object to create the strategy object to be used. By doing so the client has only to send a parameter (like a string) to the context asking to use a specific algorithm, being totally decoupled of strategy classes.

Whether the context is who decide the strategy, the decoupling is made injecting the concrete strategy object to the client, so the client don't know which specific implementation it is, or what other implementations there are.

So, a decoupled implementation is where nobody has knowledge of the other concrete participants.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top