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 7438513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:33:58+00:00 2026-05-29T10:33:58+00:00

In my understanding the strategy pattern is used to make behaviour interchangable. This involves

  • 0

In my understanding the strategy pattern is used to make behaviour interchangable. This involves that the responsibility of the strategy is defined in an interface, to which the client may then delegate calls. E.g. suppose a value can be obtained in different ways, the interface would have a method “getValue()”.

My question concerns the case where the flow of control is opposite. For example if the concrete strategy initiates the request “onValueChanged()” on the client (suppose it has a reference to the client or a callback interface).

Is this still considered a strategy pattern?

Update – added the following source code example:

interface DataSupplierCb
{
  void onValueChanged(int a);
}

interface DataSupplier
{
  void check();
}

// NOTE 1: Data supplier knows how the get the value
class ConcreteDataSupplier : public DataSupplier
{
  void check()
  {        
    myDataSupplierCb.onValueChanged(47);
  }
}

class Client : public DataSupplierCb
{
  void onValueChanged(int a)
  {
    // NOTE 2: Client knows what to do with the value
  }

  void changeDataSupplier(int i)
  {
    if (i == 1)
    {
      myCurrentDataSupplier = new ConcreteDataSupplier(this);
    }
  }
}
  • 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-29T10:33:58+00:00Added an answer on May 29, 2026 at 10:33 am

    If the intent of the DataSupplier interface to allow your Client to swap in, and delegate to, different concrete data-fetching implementations then yes it can be considered a strategy. Your Client is shielded from the details (aka strategy) used to fetch the value as expected in the use of the Strategy pattern. And the fact that the Client reference is passed to the Strategy is fine and common:

    (From the GoF)

    “Strategy and Context interact to implement the chosen algorithm. A
    context may pass all data required by the algorithm to the strategy
    when the algorithm is called. Alternatively, the context can pass
    itself as an argument to Strategy operations. That lets the strategy
    call back on the context as required
    .”

    The Context for you is Client.

    Now that all being said, rare is a solution that uses only one pattern. Your notification does seem to use the Observer pattern as another poster commented, and that is fine.

    What I don’t like about what you have implemented though is that your Strategy is a pure interface. Not always a bad thing, but in this case, with that notification callback, an interface does not provide a guarantee that the notifictaion callback is going to happen. Interfaces only guarantee the method signatures. I would recommend using the Template pattern in a base class to derrive the strategies from.

    abstract class DataSupplier
    {
    
       protected ClientInterface _client;
    
       // ctor takes in context
       public DataSupplier(ClientInterface client)
       {
          _client - client;
       }
    
       public void check()
       {
          int priorValue = 46;
    
          int newValue = OnGetValue(); 
    
          if (priorValue != newValue)
             _client.onValueChanged(newValue)
    
       }
    
       protected abstract int OnCheck();
    
    }
    

    And then:

    class ConcreteDataSupplier : DataSupplier
    {
    
       // Check, and notification, are handled by the base.  We only need
       // to implement the actually data fetching
    
       int OnGetValue()
       {        
          return someValue;
       }
    }
    

    With this approach, I know the notification will be handled. I don’t need to worry about an implementor forgetting it in a new strategy later.

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

Sidebar

Related Questions

My understanding of col is that it may be used to indicate a class
My understanding of the Javascript so far has been that it is a client-side
Understanding that I should probably just dig into the source to come up with
My understanding of Hibernate is that as objects are loaded from the DB they
My understanding is that wxWidgets is for a number of programming languages (C++, Python,
My understanding is that C/C++ produces native code to run on a particular machine
My understanding is that in unix, when memory is freed, the memory doesn't get
My understanding was that Python strings are immutable. I tried the following code: a
My understanding is that find only takes the primary key as the parameter. That
My understanding about CSS is that, generally if you set <div style=color: Red> ,

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.