Note that there are some explanatory texts on larger screens.

plurals
  1. POIn the strategy pattern can the strategy take the Context as parameter
    primarykey
    data
    text
    <hr> <h2>Feedback summary</h2> <p>I will now close this thead (I think there will be no more feedback) and try to summarize what I understood</p> <ol> <li>using the "Context" as a parameter for my strategy introduces a tight coupling that should be avoided and also could force me to expose properties that should perhaps remain hidden in the class.</li> <li>To minimize coupling, it would be better to provide the needed values or at least to use an interface instead of a concrete type to the strategy.</li> </ol> <hr> <p>I'm trying to get a clear overview of the strategy pattern and I'm asking myself if it is a good or bad design to have the strategy depends on the context.</p> <p>Let's take the following classical implementation</p> <pre><code>//The strategy interface IStrategy { void Execute(); } class ConcreteStrategyA : IStrategy { public void Execute() { Console.WriteLine( "Called ConcreteStrategyA.Execute()" ); } } class ConcreteStrategyB : IStrategy { public void Execute() { Console.WriteLine( "Called ConcreteStrategyB.Execute()" ); } } //The context class Context { IStrategy strategy; // Constructor public Context(IStrategy strategy) { this.strategy = strategy; } public void UpdateContext(IStrategy strategy) { this.strategy = strategy; } public void Execute() { strategy.Execute(); } } </code></pre> <p>All examples I have seen have pretty simple strategies that take basic arguments (integers for example). What I would like to know is if there is something wrong if the strategy uses the Context to do the work.</p> <p>It would give something like </p> <pre><code>//The strategy interface IStrategy { void Execute(Context arg); } </code></pre> <p>and the invocation would give</p> <pre><code>//The context class Context { .... public void Execute() { strategy.Execute(this); } } </code></pre> <p>Is this "coupling" to be avoided? Is it ok?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload