Home > Agile stuff > The beautiful Bridge pattern

The beautiful Bridge pattern

The reason for writing this little post is that I often get questions on patterns and one that stands out in particular has to do with the difference between the Strategy and the Bridge pattern. Most of the people I work(ed) with have trouble seeing the difference between these patterns and amazingly some say they never used the Bridge pattern at all. I find that very hard to believe and think that the have used it, but did not realize it was the bridge pattern.

I think that the problem has to do with the used terminology in the 1995 GoF book. The GoF describes the Bridge as “decouple an abstraction from its implementation so that the two can vary independently”…..

So I always think of the abstraction as a concept in my domain. The ‘what it IS part’. The implementation I consider the ‘what it DOES’ part.

Lets say that our abstraction is a Worker and that our implementation is a Job.

Now lets say that thanks to the economic crisis the workers must do other types of jobs. E.g. Programmers earn a few bucks on the side helping high school kids with their math and Busdrivers start selling newspapers.

Lets say you implemented this like this

class Programmer {
...
   void earnMoney() {
      if (isCrisis()) {
       teachMath();
      }
      else {
       writeCode();
      }
   }
}
class Busdriver{
...
   void earnMoney() {
      if (isCrisis()) {
       sellNewsPapers();
      }
      else {
       driveBus();
      }
   }
}

Using the Strategy you could make the Programmer and the Busdriver the ‘Context’ and encapsulate the type of Job as the Strategy. This could lead to the following code

class Programmer {
...
   void earnMoney(Job job) {
        job.doIt();
   }
}

And the same for the BusDriver.
That’s nice and all, we have higher cohesion and reduced coupling because the actual Jobis encapsulated.

Now lets say that for an additional requirement the Programmer and BusDriver have some rules for negotiating their working conditions. And that they can be offered any type of Job.
This could lead to the following code for the Programmer

class Programmer {
...
   void earnMoney(Job job) {
      if( isCrisis() ) {
         if( job.hourlyWage() > 100 ) {
            job.doIt();
         }
         else {
           ...
         }            
      }
      else {
       ....
      }
   }
}

The Busdriver ofcourse has its own set of rules! For the BusDriver the daily hours of work of the job is important.

class BusDriver {
...
   void earnMoney(Job job) {
      if( isCrisis() ) {
         if( job.dailyHours() < 6 ) {
            job.doIt();
         }
         else {
           ...
         }            
      }
      else {
       ....
      }
   }
}

The Job class would look something like

interface Job {
...
   int dailyHours();
   int hourlyWage();
   void doIt();
...
}

So, we now have variation in the workers because of their specific negotiation rules. And we have variation in jobs because of specific working conditions.

Could we do this with the Strategy?….
Not without duplication, because the Strategy pattern does not have variation on its context. You have one type of context which can use varying strategies. But you have to explicitly handle the different types of Workers!

So in order to clean this up, you also introduce variation in the context. You need to encapsulate the types of Workers. You introduce a Worker abstraction, make Programmer and Busdriver inherit from the Worker abstraction and move up an abstract earnMoney method to the Worker.

And there you have your Bridge pattern. The context is now called abstraction and the strategy is now called implementation. You can now cleanly combine Workers and Jobs!

So, there must be many times you used the Bridge pattern!!

Advertisements
Categories: Agile stuff Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: