Created
January 26, 2022 11:44
-
-
Save jx13xx/30f18516c5ad5abe5c4d201c50df77ab to your computer and use it in GitHub Desktop.
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
function renderLargeShapes(shapes){ | |
shapes.forEach((shape) => { | |
const area = shape.getArea(); | |
shape.render(area) | |
}) | |
} | |
const shapes = [new Rectangle(4, 5), new Rectangle(4, 5), | |
new Shape(5)]; | |
renderLargeShapes(shapes) | |
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 Rectangle extends Shape { | |
constructor(width, height){ | |
super(); | |
this.width = width; | |
this.height = height; | |
} | |
getArea(){ | |
return this.width * this.height; | |
} | |
} |
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 Shape { | |
setColor(color){ | |
} | |
render(area){ | |
} | |
} |
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 Square extends Shape { | |
constructor(length){ | |
super(); | |
this.length = length; | |
} | |
getArea(){ | |
return this.length * this.length; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment