0

Note: I tagged design patterns just in (the unlikely) case it is actually one. If it's not I'll remove the tag.

consider this example:

template <class _base>
struct SplitPrint: public _base
{
    virtual void printPositive(int positive) = 0;
    virtual void printNegative(int negative) = 0;
    void print( int number) override
    { 
        _base::print(number);
        if (number >= 0)
            printPositive(number);
        else
            printNegative(number);
    }
};

struct Interface
{
    virtual void print( int number) = 0;
};

//implements the interface
struct NormalImplementer : public Interface
{
    void print( int number) override { std::cout << number << "\n"; }
};

//Still implements the interface
struct DifferentImplementer : public SplitPrint<NormalImplementer>
{
    void printPositive (int number) override { if (number > 10) std::cout << "big" << "\n"; }
    void printNegative (int number) override { if (number < -10) std::cout << "small" << "\n"; }
};

//Still implements the interface
struct TellSign : public SplitPrint<DifferentImplementer>
{
    void printPositive (int number) override { std::cout << "also, positive" << "\n"; }
    void printNegative (int number) override { std::cout << "also, negative" << "\n"; }
};


int main()
{
  NormalImplementer printer1;
  DifferentImplementer printer2;
  TellSign printer3;

  printer1.print(5); //5
  printer1.print(-42); //-42

  printer2.print(5); //5
  printer2.print(-42); //-42 small

  printer3.print(5); //5 also, positive
  printer3.print(-42); //-42 small also, negative
}

Here I split the pure virtual method "print" in two other methods, DifferentImplementer doesn't have to know how to print a number (silly example but hopefully you understand what I mean) as long as it knows how to print a positive number and how to print a negative one.

Do this OOP problem have a name? Is this example a good way to solve this problem in C++? is there a better/cleaner way to solve this problem in C++?

Nyashes
  • 652
  • 4
  • 22
  • This question probably belongs in http://softwareengineering.stackexchange.com – Daniel T. Mar 01 '17 at 17:08
  • @DanielT. Well it's based on a real problem I'm encountering so I posted on SO by default without really thinking about it, but now that I read my question again, you are probably right, maybe crosspost? – Nyashes Mar 01 '17 at 17:14
  • @DanielT. The question asks about a specific C++ programming problem, I don't think it needs to be migrated to SE Software Engineering. – πάντα ῥεῖ Mar 01 '17 at 17:16

1 Answers1

1

Do this OOP problem have a name?

Yes, it's called Mixin Pattern.

A related pattern you've been also using is the Template Method Pattern.

Is this example a good way to solve this problem in C++?

Depends on the situation and use cases. But that style isn't inherently bad and used in many API's. Microsoft's ATL is one example.

is there a better/cleaner way to solve this problem in C++?

Again depends on your use case. There are more than one way to provide multiple interface implementations.

Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Thanks, that was really helpful. I'll take some time to read the sources ou provided. – Nyashes Mar 01 '17 at 17:26
  • @AlexandreTHOUVENIN If you're able to spot the patterns used in some code, these are easy to find ;-) – πάντα ῥεῖ Mar 01 '17 at 17:30
  • True, but to be fair I don't know more than 3 or 4 design patterns, so I usually solve my problem and move on. This time since it looked like a general OOP problem I wondered if it already had a (better/similar) solution. and it has one. Call it wrong order ^^' – Nyashes Mar 01 '17 at 17:37