bridge pattern scala way using self types feature of scala with real world example

I have covered the Bridge pattern explanation in the previous article here is the link for the same scala-bridge-design-pattern

The Bridge design pattern could be represented in a way that is less verbose and closer to Scala using the self types feature of scala the initial VendorCalibration trait remains unchanged but the actual implementations become traits instead of classes and having traits would allow us to mix them in when needed later.


trait HuwCalibrationT extends VendorCalibration{

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

}


trait CiscoCalibrationT extends VendorCalibration {
override def getCalibration(rawdata: Double): Double =
{
return rawdata * 0.967;
}
}


trait BrocadeCalibrationT extends VendorCalibration {
override def getCalibration(rawdata: Double): Double =
{
return rawdata * 0.967;
}
}

lets code the group abstraction hierarchy


trait GroupCalibration {
self: VendorCalibration =>
def applyCorrection(rawdata: Double): Double;
}


class Group1 extends GroupCalibration {

self: VendorCalibration =>

def applyCorrection(rawdata: Double): Double =
{

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

}
}


class Group2 extends GroupCalibration {

self: VendorCalibration =>

def applyCorrection(rawdata: Double): Double =
{

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

}
}


class Group3 extends GroupCalibration {

self: VendorCalibration =>

def applyCorrection(rawdata: Double): Double =
{

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

}
}

Lets code the driver class


object Test extends App{

val rawdata:Double=34.56565;

val groupX = new Group1 with HuwCalibrationT;

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

val groupY = new Group1 with BrocadeCalibrationT;

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

val groupV = new Group2 with HuwCalibrationT;

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

val groupZ = new Group3 with BrocadeCalibrationT;

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

}