Skip to content

Commit 0cfa349

Browse files
committed
liskov substitution Good example as functions
1 parent 3d20678 commit 0cfa349

File tree

2 files changed

+55
-42
lines changed

2 files changed

+55
-42
lines changed

README.md

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,68 +1242,43 @@ renderLargeRectangles(rectangles);
12421242

12431243
**Good:**
12441244
```javascript
1245-
class Shape {
1246-
setColor(color) {
1245+
function makeShape() {
1246+
function setColor(color) {
12471247
// ...
12481248
}
12491249

1250-
render(area) {
1250+
function render(area) {
12511251
// ...
12521252
}
1253-
}
12541253

1255-
class Rectangle extends Shape {
1256-
constructor() {
1257-
super();
1258-
this.width = 0;
1259-
this.height = 0;
1260-
}
1261-
1262-
setWidth(width) {
1263-
this.width = width;
1264-
}
1254+
return {
1255+
setColor,
1256+
render
1257+
};
1258+
}
12651259

1266-
setHeight(height) {
1267-
this.height = height;
1268-
}
1260+
function makeRectangle(width, height) {
1261+
let shape = makeShape();
1262+
shape.getArea = () => width * height;
12691263

1270-
getArea() {
1271-
return this.width * this.height;
1272-
}
1264+
return shape;
12731265
}
12741266

1275-
class Square extends Shape {
1276-
constructor() {
1277-
super();
1278-
this.length = 0;
1279-
}
1280-
1281-
setLength(length) {
1282-
this.length = length;
1283-
}
1267+
function makeSquare(length) {
1268+
let shape = makeShape();
1269+
shape.getArea = () => length * length;
12841270

1285-
getArea() {
1286-
return this.length * this.length;
1287-
}
1271+
return shape;
12881272
}
12891273

12901274
function renderLargeShapes(shapes) {
12911275
shapes.forEach((shape) => {
1292-
switch (shape.constructor.name) {
1293-
case 'Square':
1294-
shape.setLength(5);
1295-
break;
1296-
case 'Rectangle':
1297-
shape.setWidth(4);
1298-
shape.setHeight(5);
1299-
}
1300-
13011276
const area = shape.getArea();
13021277
shape.render(area);
13031278
});
13041279
}
13051280

1306-
const shapes = [new Rectangle(), new Rectangle(), new Square()];
1281+
const shapes = [makeRectangle(3, 4), makeRectangle(4, 5), makeSquare(4)];
13071282
renderLargeShapes(shapes);
13081283
```
13091284
**[⬆ back to top](#table-of-contents)**
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function makeShape() {
2+
function setColor(color) {
3+
// ...
4+
}
5+
6+
function render(area) {
7+
// ...
8+
}
9+
10+
return {
11+
setColor,
12+
render
13+
};
14+
}
15+
16+
function makeRectangle(width, height) {
17+
let shape = makeShape();
18+
shape.getArea = () => width * height;
19+
20+
return shape;
21+
}
22+
23+
function makeSquare(length) {
24+
let shape = makeShape();
25+
shape.getArea = () => length * length;
26+
27+
return shape;
28+
}
29+
30+
function renderLargeShapes(shapes) {
31+
shapes.forEach((shape) => {
32+
const area = shape.getArea();
33+
shape.render(area);
34+
});
35+
}
36+
37+
const shapes = [makeRectangle(3, 4), makeRectangle(4, 5), makeSquare(4)];
38+
renderLargeShapes(shapes);

0 commit comments

Comments
 (0)