Sunday, June 10, 2007

Bridge Pattern

Applying the bridge pattern to a class involving separating the class into two parts, an interface and an implementation.

The implementation of the class has been moved to an implementation class hidden from general use.

class C
{
public:
C(int val);
~C();
int get_a() const;
int get_b() const;
private:
Cimpl *impl_;
};

class Cimpl
{
public:
Cimpl(int val) : a_(val),b_(val) {}
~Cimpl() {}
int get_a() const {return a_;}
int get_b() const {return b_;}
private:
int a_;
int b_;
};

C::C(int val): impl_(new Cimpl(val)) {}
C::~C() {delete impl_;}
int C::get_a() const
{
return impl_->get_a();
}
int C:: get_b() const
{
return impl_->get_b();
}

Employing a Bridge incurs a clear runtime cost, each member function call is both indirect and non-inline. The advantages lie in the ability to update client code without recompilation.

Applications: patch updates(bug fixes, typically) into installed software.

No comments: