open-closed principle vs Liskov
unknown
javascript
2 years ago
1.6 kB
11
Indexable
Never
// BRAK ZASTOSOWANIA OPEN/CLOSED class Vehicle { constructor(fuelCapacity, fuelEfficiency) { this.fuelCapacity = fuelCapacity; this.fuelEfficiency = fuelEfficiency; } getRange() { let range = this.fuelCapacity * this.fuelEfficiency; if (this instanceof HybridVehicle) { range += this.electricRange; } return range; } } class HybridVehicle extends Vehicle { constructor(fuelCapacity, fuelEfficiency, electricRange) { super(fuelCapacity, fuelEfficiency); this.electricRange = 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' // POPRAWNE ZASTOSOWANIE OPEN/CLOSE 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'