Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ac6a51d

Browse files
Merge pull request ryanmcdermott#182 from DavidVujic/liskov-substitution
Liskov Substitution (SOLID section) - width/height/length as constructor params
2 parents 47dbe9d + 90e9b24 commit ac6a51d

File tree

1 file changed

+7
-28
lines changed

1 file changed

+7
-28
lines changed

‎README.md‎

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,17 +1257,9 @@ class Shape {
12571257
}
12581258

12591259
class Rectangle extends Shape {
1260-
constructor() {
1260+
constructor(width, height) {
12611261
super();
1262-
this.width = 0;
1263-
this.height = 0;
1264-
}
1265-
1266-
setWidth(width) {
12671262
this.width = width;
1268-
}
1269-
1270-
setHeight(height) {
12711263
this.height = height;
12721264
}
12731265

@@ -1277,12 +1269,8 @@ class Rectangle extends Shape {
12771269
}
12781270

12791271
class Square extends Shape {
1280-
constructor() {
1272+
constructor(length) {
12811273
super();
1282-
this.length = 0;
1283-
}
1284-
1285-
setLength(length) {
12861274
this.length = length;
12871275
}
12881276

@@ -1293,21 +1281,12 @@ class Square extends Shape {
12931281

12941282
function renderLargeShapes(shapes) {
12951283
shapes.forEach((shape) => {
1296-
switch (shape.constructor.name) {
1297-
case 'Square':
1298-
shape.setLength(5);
1299-
break;
1300-
case 'Rectangle':
1301-
shape.setWidth(4);
1302-
shape.setHeight(5);
1303-
}
1304-
1305-
const area = shape.getArea();
1306-
shape.render(area);
1307-
});
1308-
}
1284+
const area = shape.getArea();
1285+
shape.render(area);
1286+
});
1287+
}
13091288

1310-
const shapes = [new Rectangle(), new Rectangle(), new Square()];
1289+
const shapes = [new Rectangle(4, 5), new Rectangle(4, 5), new Square(5)];
13111290
renderLargeShapes(shapes);
13121291
```
13131292
**[⬆ back to top](#table-of-contents)**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /