Home > Extreme Programming > The Bridge Pattern – When a single class hierarchy is not enough

The Bridge Pattern – When a single class hierarchy is not enough

September 10th 2009 | David Cooksey

The Bridge Pattern – When a single class hierarchy is not enough

The bridge pattern allows both the implementation and the abstraction of a programming scenario to vary. Let’s take a look at a specific use case in order to understand the benefit the bridge pattern provides.

Imagine we are writing a top-down scrolling action game. The player will be able to choose from a variety of vehicles and will be up against a maze full of passive and active obstacles. In order to increase replayability, the vehicles available will include tanks, helicopters, and motorcycles, with expansions planned to include additional vehicles. Tanks will have a cannon and a machine gun, helicopters will have missiles and a machine gun, while the motorcycle will allow the player to throw grenades and wield a samurai sword. Throughout the course of the game, upgrades will alter the abilities of each weapon (better missiles, more ammo for the machine gun, swords of awesome lethality, etc).

So, how do we plan our class structure in such a way that we can treat each vehicle the same at the high level, while allowing for flexibility in both the selected vehicle and the weapons it is currently using?

Ideally our top-level implementation should allow us to do something like PlayerVehicle.FireWeapon1() without concerning ourselves with the specific vehicle or weapon the player is using.

The bridge pattern gives us the flexibility we need.

First we create a vehicle base class.

  public abstract class Vehicle
{
public IWeapon weapon1;
public IWeapon weapon2;

public abstract void Move();
public void ShootWeapon1()
{
weapon1.Fire();
}

public void ShootWeapon2()
{
weapon2.Fire();
}
}

By leveraging the “has-a” relationship between a vehicle and its weapons we allow the weapons to vary. The exposed ShootWeapon1 and ShootWeapon2 functions perform the same function in this case as calling .weapon1.Fire() on the vehicle itself. By making Vehicle an abstract class we leave all details of movement up to its concrete implementations.

public class Tank : Vehicle
{
public Tank()
{
weapon1 = new SimpleCannon();
weapon2 = new BasicMachineGun();
}

public override void Move()
{
// Check for physical obstacle, if no obstacle move the tank.
}
}

public class Helicopter : Vehicle
{
public Helicopter()
{
weapon1 = new AirToAirMissile();
weapon2 = new BasicMachineGun();
}

public override void Move()
{
// Move helicopter
}
}

As a result, both the Tank and Helicopter listed above will work as the vehicle in the following code sample.

      Vehicle vehicle = new Tank();

vehicle.Move();
vehicle.ShootWeapon1();
vehicle.ShootWeapon2();

The bridge pattern allows us to change vehicles and weapons independently. This concept is extensible to as many degrees as are necessary to allow independent variation. For example, a futuristic update to the game might add varying kinds of passive or reactive shields to the vehicles. No problem, just create an IShield interface and add it to the Vehicle abstract class.

Essentially, the bridge pattern is just an implementation of a recognition that two concepts exist in a “has-a” relationship and need to vary independently. As such, it provides the required flexibility with no drawback other than a small increase in the complexity of the class hierarchy.

David Cooksey is a Senior .NET Consultant at Thycotic Software, an agile software services and product development company based in Washington DC. Secret Server is our flagship password management software product.

Categories: Extreme Programming
  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: