Skip to content

Instantly share code, notes, and snippets.

@raulmoyareyes
Created July 29, 2018 15:48
Show Gist options
  • Save raulmoyareyes/060612862cfc79d11a3c11132d347368 to your computer and use it in GitHub Desktop.
Save raulmoyareyes/060612862cfc79d11a3c11132d347368 to your computer and use it in GitHub Desktop.
[ Wrong ] - Liskov Substitution Principle
class Rectangle {
constructor() {
this.width = 0;
this.height = 0;
}
setWidth(width) {
this.width = width;
}
setHeight(height) {
this.height = height;
}
getArea() {
return this.width * this.height;
}
}
class Square extends Rectangle {
setWidth(width) {
this.width = width;
this.height = width;
}
setHeight(height) {
this.width = height;
this.height = height;
}
}
function renderLargeRectangles(rectangles) {
rectangles.forEach((rectangle) => {
rectangle.setWidth(4);
rectangle.setHeight(5);
const area = rectangle.getArea();
});
}
const rectangles = [new Rectangle(), new Rectangle(), new Square()];
renderLargeRectangles(rectangles);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment