OO system design question

B

Bart Simpson

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?.
 
B

Bob Hairgrove

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?.

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.
 
B

Bart Simpson

Bob said:
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.

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)
 
B

Ben Pope

Bart said:
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?

Encapsulation (as you have it) is preferable to inheritance, unless you
have an "is substitutable for".
Is one approach better than the other? - if yes, what are the pros and
cons on either method?.

This is a FAQ, I believe:
http://www.parashift.com/c++-faq-lite/private-inheritance.html#faq-24.2

Ben Pope
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top