display | more...

Often, in object-oriented programming, the programmer has to choose whether one entity maintains visibility to another, or vice versa. The entity that is aware of the other entity is the one who would lose reusability due to its new dependency on the other entity. Often, the programmer would like to keep both entities independent from one another, in which case a bridge can be created, which is a small component that acts as a proxy between those entities. In way of providing the necessary services from one entity to another, it would communicate with one to acquire the service, and communicate the result to another. By creating a component that is completely un-reusable, the programmer can keep all other entities concerned independent.

The Bridge Pattern is also known as a pimpl pattern (defined somewhere in the bowels of the C++ Report I think). To understand what it is, here's an example:


class ClassX

    void some_function1();
    int  some_function2(char);

    ClassXImpl* impl; // Doesn't have to be a pointer.
                      // This is problem specific


class ClassXImpl


    void some_function1_impl();
    int  some_function2_impl(char);
    int  helper_function();
    long some_other_helper_function();

    int  variable1_;
    char variable2_;

  friend class ClassX;


#include "ClassXImpl.hh"

// do your implementation of ClassX here


// do your implementation of ClassXImpl here

The idea here is that the ClassX implemenation (i.e. the ClassX.cc file) only implements what is defined in the interface (i.e. some_function1() and some_function2(char)) but actually forwards a lot of work off to ClassXImpl which contains all private functions, variables and constructors to which ClassX has full access since it is declared as a friend of ClassXImpl.

So what's the point? The reason you do this is to isolate other compilation units from any changes you make to the ClassX implementation that they don't need to care about. If everyone uses ClassX and you decided to not use the pimpl, and you added a private variable to the header file that nobody ever sees, you've still forced them all to recompile, and that can take hours for no reason whatsoever. If you use the pimpl then you can add that variable to ClassXImpl which nobody ever uses. Thus, you get to enhance the behaviour (i.e. the ClassX.cc file without having to add to the ClassX header file. Nobody knows you made the change, and nobody needs to care. Your recompile changed from hours to seconds.

For instance, if you added const char* variable3_ to ClassXImpl.hh then in ClassX.cc you can reference that all you want and never have to change ClassX.hh.

It's best to use this pattern when you have a very low level class that is included by a lot of other modules and you don't want a small change to private data to have a catastrophic effect on the build process. If you're in a situation where you can only patch the production code and can't change the header file you're going to be in trouble -- you'll be saying to yourself, "DAMN! I could really use a nice pimpl right now."

See also pimpl idiom.

Log in or register to write something here or to contact authors.