scala bridge design pattern real life example in scala

Bridge Pattern is a structural design pattern which can be used to vary not only your implementations, but also your abstractions.

Lets jump into an example to understand this pattern.

Lets say we want to apply some algorithm to raw data of a speed metric that we receive from the ems based on the vendor device type reporting the metric . We currently have three vendors huw,cisco and brocade. We also have to apply algorithm based on the frequency range called as group size and currently we have group1,group2 and group3.

You expect that the algorithm will be refined many times as usability data is collected on the group size calculation. So your dilemma is that the group size algorithm is going to evolve and the vendor calculation algorithm is also going to evolve. Just abstracting the vendor calibration algorithm will not help as we are also going to need to vary the group size abstraction because it is going to change over time as the group size algorithm will be improved based on the user feedback.

We can consider using the Bridge Pattern here which will allow us to vary the implementation and the abstraction by placing the two in separate class hierarchies.Below is the class hierarchy we are going to build.

As we can see in the above diagram now we have two hierarchies, one for the group size and a separate one for vendor specific implementations. The bridge allows you to vary either side of the two hierarchies independently.By using this pattern we can decouples an implementation so that it is not bound permanently to an interface. Abstraction and implementation can be extended independently and changes to the concrete abstraction classes will not affect the client.

Lets code the example

Lets code the VendorCalibration implementation hierarchy first


trait VendorCalibration {

def getCalibration(rawdata: Double): Double;

}


class HuwCalibration extends VendorCalibration {

override def getCalibration(rawdata: Double): Double =
{
return rawdata * 0.99876;
}

}


class BrocadeCalibration extends VendorCalibration {

override def getCalibration(rawdata: Double): Double =
{
return rawdata * 0.967;
}

}


class CiscoCalibration extends VendorCalibration {

override def getCalibration(rawdata: Double): Double =
{
return rawdata * 0.967;
}
}

lets code the group abstraction hierarchy


trait GroupCalibration {
def applyCorrection(rawdata: Double): Double;
}


class Group1(val vendorCalibration: VendorCalibration) extends GroupCalibration {

def applyCorrection(rawdata: Double): Double =
{

val groupCalibrated = rawdata * 0.9889 + 0.0345;
vendorCalibration.getCalibration(groupCalibrated)

}
}


class Group2(val vendorCalibration: VendorCalibration) extends GroupCalibration {

def applyCorrection(rawdata: Double): Double =
{

val groupCalibrated = rawdata * 0.789 + 0.0445;
vendorCalibration.getCalibration(groupCalibrated)

}
}


class Group3(val vendorCalibration: VendorCalibration) extends GroupCalibration {

def applyCorrection(rawdata: Double): Double =
{

val groupCalibrated = rawdata * 0.99889 + 0.0145;
vendorCalibration.getCalibration(groupCalibrated)

}
}

Lets code the driver class


object Test extends App{

val rawdata:Double=34.56565;

val groupX = new Group1(new HuwCalibration());

System.out.println(groupX.applyCorrection(rawdata));

val groupY = new Group1(new BrocadeCalibration());

System.out.println(groupY.applyCorrection(rawdata));

val groupV = new Group2(new HuwCalibration());

System.out.println(groupV.applyCorrection(rawdata));

val groupZ = new Group3(new BrocadeCalibration());

System.out.println(groupZ.applyCorrection(rawdata));

}

2 thoughts on “scala bridge design pattern real life example in scala”

Leave a Reply

Your email address will not be published. Required fields are marked *