Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 540153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:12:38+00:00 2026-05-13T10:12:38+00:00

Feedback summary I will now close this thead (I think there will be no

  • 0

Feedback summary

I will now close this thead (I think there will be no more feedback) and try to summarize what I understood

  1. 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.
  2. 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.

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.

Let’s take the following classical implementation

//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();
  }
}

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.

It would give something like

//The strategy
interface IStrategy  
{  
  void Execute(Context arg);  
}  

and the invocation would give

//The context
class Context
{
  ....

  public void Execute()
  {
    strategy.Execute(this);
  }
}

Is this “coupling” to be avoided? Is it ok?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:12:38+00:00Added an answer on May 13, 2026 at 10:12 am

    One issue I see with your approach is that there would a tight coupling between the concrete Context class and the instances of the Strategy classes. This would imply that the Strategy classes can be used only with the Context class . One way to circumvent this is that to make your strategy classes dependent (or use) an interface that the ‘Context’ class would implement.

    EDIT
    Also when the Strategy classes have an instance of Context class, these classes have to get the explicitly data from the Context class. This would mean adding getters (as necessary) in the Context class for strategy classes to get the data they need. But adding getters is not necessarily a good OO practice as more getters pose the risk of breaking the encapsulation.

    An alternative you can think of is to not pass the reference (this) of Context class to the method(s) in strategy class but to pass only the needed data to the Strategy class.

    For example if the Context class is something like this: (the code is in Java)

    Context {
       IStrategy strategy;
       List<Integer> scores;
    
       public Context(IStrategy strategy)
       {
            this.strategy = strategy;
            scores = new ArrayList<Integer>
       }
    
       public print() {
           strategy.sort(scores);
       }
    }
    
    public interface IStrategy<Integer> {
        public void sort(List<Integer> l);
    }
    

    In the above code the Strategy class operates on a generic Integer list and is not particularly bound to be used with the Context class.
    Also taking further one can use a generic method while defining the Strategy class so that sort method is not just applicable to Integers but also to generic types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 279k
  • Answers 279k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It would be garbage NULL. Therefore you should always first… May 13, 2026 at 3:28 pm
  • Editorial Team
    Editorial Team added an answer while($row=mysql_fetch_array($result)) { $itemsByUser[$row['UserName']][] = $row; } foreach($itemsByUser as $user =>… May 13, 2026 at 3:28 pm
  • Editorial Team
    Editorial Team added an answer WADL (Web Application Description Language) WADL is a standard to… May 13, 2026 at 3:28 pm

Related Questions

Okay, I get it. Data in PostgreSQL is case-sensitive. And I know I can
I'm having reports and complaints from my user that they will be using a
I am quite new to Lean/Kanban, but have poured over online resources over the
Please note that my experience in Silverlight/.Net and WCF is about two weeks of
Can I get some constructive feedback about the following architecture? Simplified Architecture Summary: Return

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.