Bridge pattern

The purpose of the bridge pattern is to create a separation between an interface that the client uses from the actual implementation. This will allow the client's interface (abstraction) to vary independently from the actual implementation. The bridge primarily uses aggregation to separate the responsibilities exposed by the interfaces.

Let's say we have a simple interface that provides the capability to perform an addition operation. We create a new interface and expose it to the client that also provides the multiply operation. And we implement the new interface by aggregating the object of the existing interface in a way that we call our actual implementation logic by calling the addition operation a number of times to achieve the correct result of the multiply operation.

The following diagram shows the general diagram for the bridge pattern as well as the diagram to show another near real-world example:

Our example in the code talks about the core microwave implementation that works very well, but then we add a layer of abstraction on top of it, which is exposing the premium version of the microwave interface but internally, in fact, it uses the same core microwave implementation.

The following code shows our core microwave interface, which is a complete and perfectly working one in its own right:

    /// <summary> 
/// Core Implementation Interface
/// </summary>
public interface ICoreMicrowave
{
//0 seconds to 1800 seconds
void AdjustTime(int seconds);

//0 to 10 steps (10=200 degree)
void AdjustHeatingTemperature(int temperature);

void Start();
}

We already have a Microwave class that implements this ICoreMicrowave interface.

Let's say we create an abstraction by creating a new interface that is presented to the client for use:

    /// <summary> 
/// It gives one touch functionality
/// </summary>
public interface INewPremiumMicrowave
{
void SelectFood(FoodType foodType);
void Start();
}

Take a look at its implementation; it basically uses an existing implementation in a smart way and proves that an abstraction can vary quite independently from its core implementation:

    public class NewPremiumMicrowave : INewPremiumMicrowave 
{
private ICoreMicrowave _microwave;
private int[] _temperatureValuesForFood;
private int[] _timeValuesForFood;

public NewPremiumMicrowave(ICoreMicrowave microwave)
{
_microwave = microwave;

//Storage of pre-determined values
_temperatureValuesForFood = new int[] { 180, 180, 150,
120, 100, 90, 80 };
_timeValuesForFood = new int[] { 300, 240, 180, 180, 150,
120, 60 };
}

public void SelectFood(FoodType foodType)
{
_microwave.AdjustTime(_temperatureValuesForFood[
(int)foodType]);
_microwave.AdjustHeatingTemperature(
_temperatureValuesForFood[(int)foodType]);
}

public void Start()
{
_microwave.Start();
}
}

In the sample implementation, you can see that the NewPremiumMicrowave class fundamentally uses the same microwave implementation just by smartly pre-setting some of the heating temperature and time duration values and by efficiently utilizing the existing microwave engine.

You can see that, when such kind of feature (that is, enhancement in the interface is needed without necessarily changing the implementation) needs to be implemented, the bridge pattern is the right way to do it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.197.213