OO system design question

Discussion in 'C++' started by Bart Simpson, Mar 12, 2006.

  1. Bart Simpson

    Bart Simpson Guest

    I am writing a communications library, and have several classes in my
    project. The "main" class (BaseEngine) is responsible for setting up the
    low level comms machinery (queues, sockets, ports etc). I have chosen a
    singleton pattern design for this class, because only one such object is
    equired, to provide shared access to the underlying comms plumbing.

    The other remaining classes all do little 'speacialized' jobs - e.g
    sending heartbeat mesages, maintaining topics and subscribers,
    forwarding messages etc, etc. They all require BaseEngine, to get their
    work done.

    Currently, I have designed the system so that the other classes have a
    "uses-a" relationship with BaseEngine. The implementation is that I have
    a private static variable of type BaseEngine in each of these classes.
    My question is that is a "good" design?. Could I simply have allowed the
    other classes to subclass (i.e. inherit from) BaseEngine?

    Is one approach better than the other? - if yes, what are the pros and
    cons on either method?.
     
    Bart Simpson, Mar 12, 2006
    #1
    1. Advertisements

  2. If each class has a private static BaseEngine member as an object,
    then you don't have a singleton because each class would have its own
    instance of a different BaseEngine. Inheriting BaseEngine wouldn't
    solve the problem for the same reason.

    If BaseEngine is to be a true singleton, then you should be sharing a
    pointer or reference to it, and not an object.
     
    Bob Hairgrove, Mar 12, 2006
    #2
    1. Advertisements

  3. Bart Simpson

    Bart Simpson Guest

    thanks for the correction Bob (my Bad), the other classes hold a static
    reference to BaseEngine (not the actual object as I had typed earlier)
     
    Bart Simpson, Mar 12, 2006
    #3
  4. Bart Simpson

    Ben Pope Guest

    Encapsulation (as you have it) is preferable to inheritance, unless you
    have an "is substitutable for".
    This is a FAQ, I believe:
    http://www.parashift.com/c++-faq-lite/private-inheritance.html#faq-24.2

    Ben Pope
     
    Ben Pope, Mar 12, 2006
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.