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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:34:08+00:00 2026-05-13T15:34:08+00:00

I have a class Parameter, the purpose of which is to represent the possible

  • 0

I have a class Parameter, the purpose of which is to represent the possible values a certain parameter could hold (implements two key methods, GetNumValues() and GetValue(int index)).

Often one logical parameter (parameter values are bit flags) is best represented by 2 or more instances of the Parameter class (i.e. a Parameter that can be 1 or 2, and a Parameter than can be 4 or 8, rather than one Parameter than can be 5, 6, 9, or 10). To handle that, I would like to create a CompositeParameter class which contains Parameters, and will implement the GetNumValues() and GetValue() functions based on the combinations of the Parameters it holds.

And since CompositeParameter is combining Parameters to have them act as one single Parameter, the “CompositeParameter is a Parameter” relationship makes sense. So I find myself in the situation where I have a class which composes objects of a class it inherits from, which just doesn’t seem right. But at the same time, I don’t see why the higher level code shouldn’t be able to treat CompositeParameters and Parameters exactly the same.

The only option I can think of is to have CompositeParameter simply compose Parameters, and the higher level code would only deal with CompositeParameters. However, that is somewhat wasteful b/c the general case would be CompositeParameters which contained just one Parameter.

Thoughts?

class Parameter
{
public:
    virtual unsigned int GetNumValues() const {...}
    virtual unsigned int GetValue(unsigned int index) const {...}
}

class CompositeParameter : public Parameter
{
public:
    // product of GetNumValues() of each item in mParamList
    virtual unsigned int GetNumValues() const {...} 

    // allow all the possible combinations of the items in mParamList to be
    // treated as one parameter. i.e. if mNumParams = 2, this would be analogous
    // to getting the row and col index of a matrix from index, and combining
    // the mParamList[0]->GetValue(row) and mParamList[1]->GetValue(col)
    virtual unsigned int GetValue(unsigned int index) const {...}

private:

    static const unsigned int MAX_PARAMS = 10;

    unsigned int mNumParams;
    const Parameter* mParamList[MAX_PARAMS];
}
  • 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-13T15:34:08+00:00Added an answer on May 13, 2026 at 3:34 pm
    I have a class which composes objects of a class it inherits from,
    which just doesn't seem right.
    

    Isn’t that the definition of a composite?

    (parameter values are bit flags)
    

    This is the part of the design that I would question. Perhaps a better name for Parameter would be FlagSet?

    It’s fine to hide the bitwise tests behind an interface, but it seems like inheritence might be overkill to solve a problem with well known solutions in basic computer science.


    However, that is somewhat wasteful b/c the general case would be
    CompositeParameters which contained just one Parameter.
    

    The point of the composite pattern is that a Leaf object represents the simple case, a Composite object represents the complex case, and client code can treat both cases the same. If your interface requires the client code to distinguish between the two, or iterate through the base class components, then you’re not really getting any value out of using the pattern.

    For example, if your main concern is testing, then the base class could have a method:

    bool Test() const;
    

    The leaf class implementation would look like:

    bool LeafTester::Test() { return _DoTest(); }
    

    The composite class implementation would look like:

    bool CompositeTester::Test() {
        bool success = true;
    
        for (int i = 0; i < m_count; i++)
            success &= m_components[i].Test();
    
        return success;
    }
    

    And clients would always use the code like this:

    // tester could be a Composite or a leaf, but we don't care:
    bool testResult = tester.Test();
    

    I’ve used a for loop to keep the example simple. In practice I would use STL instead.

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

Sidebar

Related Questions

I have a MustInherit .NET class which declares a constructor with an integer parameter.
I am trying to make general purpose image display class which receives two parameters,
I have a class with some method that depend by one parameter. What is
I have a static method that takes a parameter and returns a class. the
Is there a way to have the compile deduce the template parameter automatically? template<class
I have a class that currently has several methods that take integer parameters. These
I have a service class which has overloaded constructors. One constructor has 5 parameters
is it possibly in c# to have some sort of base class functionality which
I have a weird issue using polymorphism. I have a base class that implements
I have a validation class which I would like to use to check all

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.