Proxy Classes
Recall that two of the fundamental principles of good software engineering are separating interface from implementation and hiding implementation details. We strive to achieve these goals by defining a class in a header file and implementing its member functions in a separate implementation file. However, as we pointed out in Chapter 9, header files do contain some portion of a class's implementation and hints about others. For example, a class's private members are listed in the class definition in a header file, so these members are visible to clients, even though the clients may not access the private members. Revealing a class's private data in this manner potentially exposes proprietary information to clients of the class. We now introduce the notion of a proxy class that allows you to hide even the private data of a class from clients of the class. Providing clients of your class with a proxy class that knows only the public interface to your class enables the clients to use your class's services without giving the client access to your class's implementation details.
Implementing a proxy class requires several steps, which we demonstrate in Figs. 10.2410.27. First, we create the class definition for the class that contains the proprietary implementation we would like to hide. Our example class, called Implementation, is shown in Fig. 10.24. The proxy class Interface is shown in Figs. 10.2510.26. The test program and sample output are shown in Fig. 10.27.
Figure 10.24. Implementation class definition.
(This item is displayed on pages 562 - 563 in the print version)
1 // Fig. 10.24: Implementation.h 2 // Header file for class Implementation 3 4 class Implementation 5 { 6 public: 7 // constructor 8 Implementation( int v ) 9 : value( v ) // initialize value with v 10 { 11 // empty body 12 } // end constructor Implementation 13 14 // set value to v 15 void setValue( int v ) 16 { 17 value = v; // should validate v 18 } // end function setValue 19 20 // return value 21 int getValue() const 22 { 23 return value; 24 } // end function getValue 25 private: 26 int value; // data that we would like to hide from the client 27 }; // end class Implementation |
Figure 10.25. Interface class definition.
(This item is displayed on page 563 in the print version)
1 // Fig. 10.25: Interface.h 2 // Header file for class Interface 3 // Client sees this source code, but the source code does not reveal 4 // the data layout of class Implementation. 5 6 class Implementation; // forward class declaration required by line 17 7 8 class Interface 9 { 10 public: 11 Interface( int ); // constructor 12 void setValue( int ); // same public interface as 13 int getValue() const; // class Implementation has 14 ~Interface(); // destructor 15 private: 16 // requires previous forward declaration (line 6) 17 Implementation *ptr; 18 }; // end class Interface |
Figure 10.26. Interface class member-function definitions.
(This item is displayed on page 564 in the print version)
1 // Fig. 10.26: Interface.cpp 2 // Implementation of class Interface--client receives this file only 3 // as precompiled object code, keeping the implementation hidden. 4 #include "Interface.h" // Interface class definition 5 #include "Implementation.h" // Implementation class definition 6 7 // constructor 8 Interface::Interface( int v ) 9 : ptr ( new Implementation( v ) ) // initialize ptr to point to 10 { // a new Implementation object 11 // empty body 12 } // end Interface constructor 13 14 // call Implementation's setValue function 15 void Interface::setValue( int v ) 16 { 17 ptr->setValue( v ); 18 } // end function setValue 19 20 // call Implementation's getValue function 21 int Interface::getValue() const 22 { 23 return ptr->getValue(); 24 } // end function getValue 25 26 // destructor 27 Interface::~Interface() 28 { 29 delete ptr; 30 } // end ~Interface destructor |
Figure 10.27. Implementing a proxy class.
(This item is displayed on pages 564 - 565 in the print version)
1 // Fig. 10.27: fig10_27.cpp 2 // Hiding a class's private data with a proxy class. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include "Interface.h" // Interface class definition 8 9 int main() 10 { 11 Interface i( 5 ); // create Interface object 12 13 cout << "Interface contains: " << i.getValue() 14 << " before setValue" << endl; 15 16 i.setValue( 10 ); 17 18 cout << "Interface contains: " << i.getValue() 19 << " after setValue" << endl; 20 return 0; 21 } // end main
|
Class Implementation (Fig. 10.24) provides a single private data member called value (the data we would like to hide from the client), a constructor to initialize value and functions setValue and getValue.
We define a proxy class called Interface (Fig. 10.25) with an identical public interface (except for the constructor and destructor names) to that of class Implementation. The only private member of the proxy class is a pointer to an object of class Implementation. Using a pointer in this manner allows us to hide the implementation details of class Implementation from the client. Notice that the only mentions in class Interface of the proprietary Implementation class are in the pointer declaration (line 17) and in line 6, a forward class declaration. When a class definition (such as class Interface) uses only a pointer or reference to an object of another class (such as to an object of class Implementation), the class header file for that other class (which would ordinarily reveal the private data of that class) is not required to be included with #include. You can simply declare that other class as a data type with a forward class declaration (line 6) before the type is used in the file.
The member-function implementation file for proxy class Interface (Fig. 10.26) is the only file that includes the header file Implementation.h (line 5) containing class Implementation. The file Interface.cpp (Fig. 10.26) is provided to the client as a precompiled object code file along with the header file Interface.h that includes the function prototypes of the services provided by the proxy class. Because file Interface.cpp is made available to the client only as object code, the client is not able to see the interactions between the proxy class and the proprietary class (lines 9, 17, 23 and 29). Notice that the proxy class imposes an extra "layer" of function calls as the "price to pay" for hiding the private data of class Implementation. Given the speed of today's computers and the fact that many compilers can inline simple function calls automatically, the effect of these extra function calls on performance is often negligible.
Figure 10.27 tests class Interface. Notice that only the header file for Interface is included in the client code (line 7)there is no mention of the existence of a separate class called Implementation. Thus, the client never sees the private data of class Implementation, nor can the client code become dependent on the Implementation code.
Software Engineering Observation 10.13
A proxy class insulates client code from implementation changes. |