More C++ Idioms/Friendship and the Attorney-Client

= Attorney-Client =

Intent
Control the granularity of access to the implementation details of a class

Motivation
A  declaration in C++ gives complete access to the internals of a class. Friend declarations are, therefore, frowned upon because they break carefully crafted encapsulation. The friendship feature of C++ does not provide any way to selectively grant access to a subset of private members of a class. Friendship in C++ is an all-or-nothing proposition. For instance, the following class  declares class   its friend. Class  therefore has access to all the private members  of class. This may not be desirable because it increases coupling. Class  cannot be distributed without class.

Providing selective access to a subset of members is desirable because the remaining private members can change interface if needed without breaking client code. It helps reduce coupling between classes. The Attorney-Client idiom allows a class to precisely control the amount of access they give to their friends.

Solution and Sample Code
The Attorney-client idiom works by adding a level of indirection. A client class that wants to control access to its internal details, appoints an attorney and makes it a. The  class is carefully crafted to serve as a proxy to the. Unlike a typical proxy class,  replicates only a subset of  ’s private interface. For instance, consider class  wants to control access to its implementation details. wants its  to provide access to   and   only.

The  class restricts access to a cohesive set of functions. All methods of class  are inline static, each taking a reference to an instance of the   and forwarding the function calls to it. Some things about  are idiomatic. Its implementation is entirely private, which prevents other unexpected classes gaining access to the internals of. determines which other classes, member functions, or free functions get access to it. It declares them as  to allow access to its implementation and through it. Without,   would have declared the same set of friends giving them unrestrained access to the internals of.

It is possible to have multiple attorney classes providing access to different sets of implementation details of the client. For instance, class  may provide access to the   method only. An interesting case emerges where an attorney class serves as a mediator for several different classes and provides cohesive access to their implementation details. Such a design is conceivable in case of inheritance hierarchies because friendship in C++ is not inheritable, but private virtual function overrides in derived classes can be called if base's private virtual functions are accessible. In the following example, the Attorney-Client idiom is applied to class  and the   function. gets called via polymorphism. To access the implementation details of, however, the same idiom may be applied.

Known Uses

 * Boost.Iterators library
 * Boost.Serialization: class boost::serialization::access