Skip to content

Commit f208229

Browse files
committed
add composition vs inheritance
1 parent 335d6a2 commit f208229

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@
1919
9. **Interface Segregation Principle** - The main idea of the interface segregation principle is that any class that implements an interface must use all functions/properties of the interface. JavaScript does not have actual interfaces, but a class inheritance structure is similar enough. This means that any class that inherits from another class needs to use all of the methods/properties from the base class. This encourages writing small classes instead of large classes.
2020

2121
10. **Dependency Inversion Principle** - The main idea of the dependency inversion principle is that any class that uses a dependency should only ever use the dependency through a predefined interface/wrapper. This makes it so that your code will never directly depend on a low level API for its operations. The reason this is so important is because if you ever need to change or remove that dependency it becomes really difficult when it is used all over your code. By wrapping this dependency in an interface you can depend on the interface you created which will make changing out the dependency painless.
22+
23+
11. **Inheritance vs Composition** - With inheritance, you describe exactly what objects are and how they're related to each other but with composition, you're describing what an object can do.

composition-vs-inheritance/after.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function swimmer({ name }) {
2+
return {
3+
swim: () => console.log(`${name} swam`),
4+
};
5+
}
6+
7+
function flyer({ name }) {
8+
return {
9+
fly: () => console.log(`${name} flew`),
10+
};
11+
}
12+
13+
function attackerAndWalker({ name }) {
14+
return {
15+
attack: () => console.log(`${name} attacked`),
16+
walk: () => console.log(`${name} walked`),
17+
};
18+
}
19+
20+
function swimmingMonsterCreator(name) {
21+
const monster = { name };
22+
return {
23+
...monster,
24+
...attackerAndWalker(monster),
25+
...swimmer(monster),
26+
};
27+
}
28+
29+
function flyingSwimmingMonsterCreator(name) {
30+
const monster = { name };
31+
return {
32+
...monster,
33+
...swimmer(monster),
34+
...attackerAndWalker(monster),
35+
...flyer(monster),
36+
};
37+
}
38+
39+
const obj = swimmer({ name: "Fish" });
40+
obj.swim();
41+
42+
const swimmingObj = swimmingMonsterCreator("Swim Monster");
43+
swimmingObj.swim();
44+
swimmingObj.attack();
45+
swimmingObj.walk();
46+
47+
const flyingswimmingObj = flyingSwimmingMonsterCreator("Flyer Swimmer Monster");
48+
flyingswimmingObj.swim();
49+
flyingswimmingObj.fly();
50+
flyingswimmingObj.attack();
51+
flyingswimmingObj.walk();

composition-vs-inheritance/before.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Monster {
2+
constructor(name) {
3+
this.name = name;
4+
}
5+
6+
attack() {
7+
console.log(`${this.name} attacked`);
8+
}
9+
10+
walk() {
11+
console.log(`${this.name} walked`);
12+
}
13+
}
14+
15+
class FlyingMonster extends Monster {
16+
fly() {
17+
console.log(`${this.name} flew`);
18+
}
19+
}
20+
21+
class SwimmingMonster extends Monster {
22+
swim() {
23+
console.log(`${this.name} swam`);
24+
}
25+
}
26+
27+
// Code gets duplicated here as we are following inheritance
28+
class FlyingSwimmingMonster extends Monster {
29+
fly() {
30+
console.log(`${this.name} flew`);
31+
}
32+
swim() {
33+
console.log(`${this.name} swam`);
34+
}
35+
}
36+
37+
const bear = new Monster("bear");
38+
bear.walk();
39+
bear.attack();
40+
41+
const eagle = new FlyingMonster("eagle");
42+
eagle.walk();
43+
eagle.attack();
44+
eagle.fly();
45+
46+
const shark = new SwimmingMonster("shark");
47+
shark.walk();
48+
shark.attack();
49+
shark.swim();

0 commit comments

Comments
 (0)