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

  • SEARCH
  • Home
  • 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 839935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:32:35+00:00 2026-05-15T05:32:35+00:00

I want to create a class that can use one of four algorithms (and

  • 0

I want to create a class that can use one of four algorithms (and the algorithm to use is only known at run-time). I was thinking that the Strategy design pattern sounds appropriate, but my problem is that each algorithm requires slightly different parameters. Would it be a bad design to use strategy, but pass in the relevant parameters into the constructor?.

Here is an example (for simplicity, let’s say there are only two possible algorithms) …

class Foo
{
private:
   // At run-time the correct algorithm is used, e.g. a = new Algorithm1(1);
   AlgorithmInterface* a; 

};

class AlgorithmInterface
{
public:
   virtual void DoSomething() = 0;
};

class Algorithm1 : public AlgorithmInterface
{
public:
   Algorithm1( int i ) : value(i) {}
   virtual void DoSomething(){ // Does something with int value };
   int value;   
};

class Algorithm2 : public AlgorithmInterface
{
public:
   Algorithm2( bool b ) : value(b) {}
   virtual void DoSomething(){ // Do something with bool value };
   bool value;   
};
  • 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-15T05:32:36+00:00Added an answer on May 15, 2026 at 5:32 am

    It would be a valid design because the Strategy pattern asks for an interface to be defined and any class that implements it is a valid candidate to run the strategy code, regardless how it is constructed.

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

Sidebar

Related Questions

I want to create a template class (let's call it Foo ) that only
I want to create a class that has one method that calls all other
I want to create a class that initializes a timer which will be used
I want to create whois class like that public class DomainInfo { public string
I want to create a wrapper class so that all queries should not be
I want to create a Javascript class/object that allow me to have various method:
If I create a class that extends UserControl and want to set a default
I want to create a substr method in C++ in a string class that
I have a signed class library that I want to create assemblies for via
I have create my own NSOpenGLView class, right now the data that i want

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.