-
-
Save kibolho/b551cdee4527d7d8296086d501c258da to your computer and use it in GitHub Desktop.
SOLID - Liskov Substitution Principle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Vehicle { | |
function startEngine() { | |
// Standard engine startup functionality | |
} | |
function accelerate() { | |
// Standard acceleration functionality | |
} | |
} | |
class Car extends Vehicle { | |
function startEngine() { | |
this.engageIgnition(); | |
super.startEngine() | |
} | |
private function engageIgnition() { | |
// Ignition procedure | |
} | |
} | |
class ElectricBus extends Vehicle { | |
function accelerate() { | |
this.increaseVoltage(); | |
this.connectIndividualEngines(); | |
} | |
private function increaseVoltage() { | |
// Electrical Logic | |
} | |
private function connectIndividualEngines() { | |
// Connection Logic | |
} | |
} | |
// Driver can use ElectricBus or Car only knowing the vehicle functions. | |
class Driver { | |
function go(v: Vehicle) { | |
v.startEngine(); | |
v.accelerate(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment