Skip to content

Instantly share code, notes, and snippets.

@jx13xx
Created January 26, 2022 11:44
Show Gist options
  • Save jx13xx/30f18516c5ad5abe5c4d201c50df77ab to your computer and use it in GitHub Desktop.
Save jx13xx/30f18516c5ad5abe5c4d201c50df77ab to your computer and use it in GitHub Desktop.
Liskov Substitution Principle
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)
class Rectangle extends Shape {
constructor(width, height){
super();
this.width = width;
this.height = height;
}
getArea(){
return this.width * this.height;
}
}
class Shape {
setColor(color){
}
render(area){
}
}
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