Bridging changes

Depending on the way you look at it, the Bridge design pattern may resemble Adapter, which we already discussed, or Strategy, which we'll discuss in the next chapter.

The idea behind the Bridge design pattern is to flatten the class hierarchy, which is currently three levels deep:

Infantry --> Rifleman  --> Upgraded Rifleman                                                                           --> Light Rifleman             
--> Grenadier --> Upgraded Grenadier
--> Light Grenadier

Why do we have this complex hierarchy?

It's because we have three orthogonal properties: weapon type, weapon strength, and movement speed.

Say instead, we were to pass those properties to the constructor of a class that implements the same interface we were using all along:

class Soldier(private val weapon: Weapon,
private val legs: Legs) : Infantry {
override fun attack(x: Long, y: Long) {
// Find target
// Shoot
weapon.causeDamage()
}

override fun move(x: Long, y: Long) {
// Compute direction
// Move at its own pace
legs.move()
}
}

The properties that Soldier receives should be interfaces, so we could choose their implementation later:

interface Weapon {
fun causeDamage(): PointsOfDamage
}

interface Legs {
fun move(): Meters
}

But what are Meters and PointsOfDamage? Are those classes or interfaces we declared somewhere?

Let's take a short detour.

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

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