Europe: +41 78 715 83 09 - Asia: +84 ‎975 112 112
contact@finix.asia

Blog

3 Jan 2018

SOLID: Liskov Substitution Principle

//
Comments0

Technologies used:   Kotlin 1.2.10 | Maven 3.3.9 | Spek 1.1.5

Liskov Substitution Principle

The Liskov substitution principle, written by Barbara Liskov in 1988, states that functions that reference base classes must be able to use objects of derived (child) classes without knowing it. It’s important for a programmer to notice that, unlike some other Gang of Four principles, whose breaking might result in bad, but working code, the violation of this principle will most likely lead to buggy or difficult to maintain code.

Let’s introduce this in Kotlin

package vn.finixasia.didemo.bean.vehicle

open class TrasportationDevice {

    var name: String? = null
    var speed: Double = 0.toDouble()

    var engine: Engine? = null
    open fun startEngine() {}
}

And

package vn.finixasia.didemo.bean.vehicle

import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Component

@Component
@Qualifier("CarBean")
class Car : TrasportationDevice() {
    override fun startEngine() {
        println("Car Started")
    }
}

There is no problem here, the car is a child of TransportationDevice and he overrides startEngine.

Let’s add now another vehicle

package vn.finixasia.didemo.bean.vehicle

import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Component

@Component
@Qualifier("BicycleBean")
class Bicycle : TrasportationDevice() {
    override fun startEngine() {
        println("How Bike Started?") //Problem
    }

}

Here there is not really a problem, the Bicycle doesn’t have an engine, and so it should not implement the engine. But we don’t respect the Liskov rules and so we should create specific version TransportationDevice with and without the engine as follow:

package vn.finixasia.didemo.bean.vehicle

open class DeviceWithEngine: TrasportationDevice() {
    open fun startEngine() {}
}

And

package vn.finixasia.didemo.bean.vehicle

open class DeviceWithoutEngine: TrasportationDevice()

And remove from TransportationDevice the startEngine() function

package vn.finixasia.didemo.bean.vehicle

open class TrasportationDevice {

    var name: String? = null
    var speed: Double = 0.toDouble()

    var engine: Engine? = null
}

The Car and the Bicycle are written as:

package vn.finixasia.didemo.bean.vehicle

import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Component

@Component
@Qualifier("CarBean")
class Car : DeviceWithEngine() {
    override fun startEngine() {
        println("Car Started")
    }
}

Thus our car become more specialized And

package vn.finixasia.didemo.bean.vehicle

import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Component

@Component
@Qualifier("BicycleBean")
class Bicycle : DeviceWithoutEngine()

the same for our Bicycle to respect Lyskov principle

 

 

 

Leave a Reply

Translate »