Handle-Body Example
Example 59 demonstrates the scenario given in
Defining Handle Classes that Automatically Create Bodies. For the complete source code, see
buildspace\examples\pointer\HandleBodyEx1.cpp. (For comparison, the same example is also implemented according to the second guideline in
HandleBodyEx2.cpp.)
The UML diagram in
Figure 38 illustrates the model.
Example 59 – Implementing a derived handle class that automatically creates a body
First implement BicycleImp, the bicycle body class.
class BicycleImp : public RWBodyBase {
public:
friend class Bicycle; //1
void showInformation(void) const;
private:
BicycleImp(void) {;} //2
BicycleImp(const RWCString& model, const RWDateTime& buildDate,
const RWCString& manufacturer) ;
RWCString model_;
RWDateTime buildDate_;
RWCString manufacturer_;
};
Now implement the matching handle class, Bicycle, which duplicates the body’s interface.
class Bicycle : public RWHandleBase {
public:
Bicycle(void) {;} //1
Bicycle(const RWCString& model, const RWDateTime& buildDate,
const RWCString& manufacturer);
void showInformation(void) const;
protected:
BicycleImp & body(void) const;
};
Bicycle::Bicycle(const RWCString& model, const RWDateTime& buildDate, const RWCString& manufacturer) : RWHandleBase(new
BicycleImp(model, buildDate, manufacturer)) {;} //2
BicycleImp & Bicycle::body(void) const {
return (BicycleImp &) RWHandleBase::body(); } //3
void Bicycle::showInformation(void) const {
body().showInformation(); } //4