Skip to content

Instantly share code, notes, and snippets.

@kibolho
Last active July 21, 2021 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kibolho/b551cdee4527d7d8296086d501c258da to your computer and use it in GitHub Desktop.
Save kibolho/b551cdee4527d7d8296086d501c258da to your computer and use it in GitHub Desktop.
SOLID - Liskov Substitution Principle
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