Last active
March 9, 2022 15:18
Open-Closed principle (SOLID) blog - Good
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 { | |
constructor(fuelCapacity, fuelEfficiency) { | |
this.fuelCapacity = fuelCapacity; | |
this.fuelEfficiency = fuelEfficiency; | |
} | |
getRange() { | |
return this.fuelCapacity * this.fuelEfficiency; | |
} | |
} | |
class HybridVehicle extends Vehicle { | |
constructor(fuelCapacity, fuelEfficiency, electricRange) { | |
super(fuelCapacity, fuelEfficiency); | |
this.electricRange = electricRange; | |
} | |
getRange() { | |
return (this.fuelCapacity * this.fuelEfficiency) + this.electricRange; | |
} | |
} | |
const standardVehicle = new Vehicle(10, 15); | |
const hybridVehicle = new HybridVehicle(10, 15, 50); | |
console.log(standardVehicle.getRange()); // Outputs '150' | |
console.log(hybridVehicle.getRange()); // Outputs '200' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment