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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:33:50+00:00 2026-05-22T19:33:50+00:00

Let’s make it a simple example. Suppose I have a collection of Number objects,

  • 0

Let’s make it a simple example. Suppose I have a collection of Number objects, which can then be even or odd.

Suppose I want to traverse this collection and process each element. The process depends on the type of element: even or odd.

Example: Print each element, such that:

  • if Number is even, I have “This is an even number.”

  • if Number is odd, I have “This is an odd number.”

However, I may decide later that I want for only the even numbers to print “An even number.”

So, as we see, this could be solved with Inheritance and Polymorphism, through the use of virtual functions:

class Number {/*...*/};              // base class
class Even : public Number {/*...*/} // derived class
class Odd  : public Number {/*...*/} // derived class

However, how can I provide the flexibility in the process to execute different behavior later on, as shown in the example above? Also, is there an alternative way to inheritance and polymorphism? Because I did not wanted to create inheritance tree only because of a specific computation/process.

The original problem is to execute different mathematical model depending on the type of an object, and the mathematical model is not unique for a given type of object (as the way I can print the even number is not unique).


Edit: It is funny that this post was closed with the argument that it is difficult to tell what is being asked. The authors of the several answers below seem to not agree with this, since all of them could understand it and reply what I was looking for.

  • 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-22T19:33:51+00:00Added an answer on May 22, 2026 at 7:33 pm

    You could separate your Strategy hierarchy from the actual numbers. This way there is no need for an artificial class hierarchy, and you are free to change associations between number (value) types and actions even runtime. The association can be defined

    • if you stick to the Number class hierarchy, then between Number subclass (Odd, Even) and strategy class (OddNumberStrategy, EvenNumberStrategy), or
    • by a mapping between concrete values and strategy instances (of course this is suitable only if you have relatively few concrete values), or
    • by a selector function.

    As an alternative approach, you can redefine your Strategies as handlers, each of which can decide whether a given value is suitable for them to handle, and process them if so. Such handlers could even be chained or stored in a collection (and then iterated over for every value), such that some values can be processed by multiple “overlapping” handlers, if this suits you.

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

Sidebar

Related Questions

Let's suppose I have a Discussion model, and that a discussion can be tagged.
Let's say I have a dataset, which can be neatly classified using weka's J48
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a simple Login servlet that checks the passed name and
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's take a common example how a haskell function can be called from a
Let's suppose I have this piece of code. foreach(string value in myList) { string
Let give you the details of this rather odd issue. We have a very
Let's say I have a simple UserControl with no codebehind: <UserControl xmlns= .... x:Class=TrafficLight.LightControl>

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.