I l@ve RuBoard Previous Section Next Section

Item 22. Class Relationships桺art 1

Difficulty: 5

How are your OO design skills? This Item illustrates a common class design mistake that still catches many programmers.

A networking application has two kinds of communications sessions, each with its own message protocol. The two protocols have similarities (some computations and even some messages are the same), so the programmer has come up with the following design to encapsulate the common computations and messages in a BasicProtocol class.



class BasicProtocol /* : possible base classes */ 


{


public:


  BasicProtocol();


  virtual ~BasicProtocol();


  bool BasicMsgA( /*...*/ );


  bool BasicMsgB( /*...*/ );


  bool BasicMsgC( /*...*/ );


};





class Protocol1 : public BasicProtocol


{


public:


  Protocol1();


  ~Protocol1();


  bool DoMsg1( /*...*/ );


  bool DoMsg2( /*...*/ );


  bool DoMsg3( /*...*/ );


  bool DoMsg4( /*...*/ );


};





class Protocol2 : public BasicProtocol


{


public:


  Protocol2();


  ~Protocol2();


  bool DoMsg1( /*...*/ );


  bool DoMsg2( /*...*/ );


  bool DoMsg3( /*...*/ );


  bool DoMsg4( /*...*/ );


  bool DoMsg5( /*...*/ );


};


Each DoMsg…() member function calls the BasicProtocol::Basic…() functions as needed to perform the common work, but the DoMsg…() function performs the actual transmissions itself. Each class may have additional members, but you can assume that all significant members are shown.

Comment on this design. Is there anything you would change? If so, why?

    I l@ve RuBoard Previous Section Next Section