1

Assume I have a sort of Observer-Pattern, where message objects are sent to observers. So I have say class Message as the top of a hierarchy and observers working on them. Concrete messages are derived from that and identified by an ID, by which it is possible to cast the message to its concrete subtype and work with it. (This is existing code i need to work on) Now there is other functionality, let us call it A, B, ... Currently, there's a huge class hierarchy which is like

class Message
class MessageWithA : public Message
class MessageWithAandB : public MessageWithA

etc.

Now the functionalities A, B etc are (mostly) completely independent of the messaging system and also among then. Wouldn't it be better to derive the concrete classes like

class MessageWithA : public Message, public A
class MessageWithAandB : public Message, public A, public B ?

(where A and B are classes that concentrate on the strict necessary to implement the appropriate functionality)

Does this pattern have a name? And is it used in C++? I was thinking about "Mix-ins", but the definition of that explicitly states that no inheritance is used. Does what I want to do make any sense?

  • As to your pattern identification question, it looks like ordinary inheritance to me. – Robert Harvey Nov 15 '16 at 15:08
  • I like the second one better - especially if A and B only incidentally have something to do with Message. – Jerry Jeremiah Nov 15 '16 at 20:56
  • 2
    Given that A, B, and Message are basically unrelated, it sounds an awful lot like using inheritance (at all) may well be misplaced here. – Jerry Coffin Nov 15 '16 at 23:36
  • @Jerry Coffin: True, but the Message-base is imposed by the mechanism and even if i put A and B as aggregated members somewhere, i'd still have to cast to a particular Message-descendant containing A and/or B. Some Visitor-like pattern may help but i'd consider it overkill in this scenario. – Scrontch Nov 16 '16 at 8:08
  • @Scrontch: Based on your descriptions, I would expect that A receives Messages and processes the ones with the needed information needed for A (i.e. MessageWithA and possibly MessageWithAandB). – Bart van Ingen Schenau Nov 16 '16 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.