Skip to content

Commit b10c967

Browse files
committed
add interface segregation principle
1 parent 078d2f9 commit b10c967

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@
1515
7. **Open Closed Principle** - The Open Closed Principle states that software entities (classes, modules, functions etc.) should be open for extension, but closed for modification.This means that a class/function/module should be able to be extended in functionality without having to go into the class and change it. Essentially you shouldn't need to change existing code to add new functionality and instead should only have to add new code.
1616

1717
8. **Liskov Substitution Principle** - This principle states that anywhere you use one type of class, you need to be able to use all type of subclasses of that class and it should work just fine.
18+
19+
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.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class Entity {
2+
constructor(name) {
3+
this.name = name;
4+
}
5+
}
6+
7+
const mover = {
8+
move() {
9+
console.log(`${this.name} moved`);
10+
},
11+
};
12+
13+
const attacker = {
14+
attack(targetEntity) {
15+
console.log(
16+
`${this.name} attacked ${targetEntity.name} for ${this.attackDamage} damage`
17+
);
18+
targetEntity.takeDamage(this.attackDamage);
19+
},
20+
};
21+
22+
const hasHealth = {
23+
takeDamage(amount) {
24+
this.health -= amount;
25+
console.log(`${this.name} has ${this.health} health remaining`);
26+
},
27+
};
28+
29+
class Character extends Entity {
30+
constructor(name, attackDamage, health) {
31+
super(name);
32+
this.attackDamage = attackDamage;
33+
this.health = health;
34+
}
35+
}
36+
37+
Object.assign(Character.prototype, mover);
38+
Object.assign(Character.prototype, attacker);
39+
Object.assign(Character.prototype, hasHealth);
40+
41+
class Wall extends Entity {
42+
constructor(name, health) {
43+
super(name);
44+
this.health = health;
45+
}
46+
}
47+
48+
Object.assign(Wall.prototype, hasHealth);
49+
50+
class Turret extends Entity {
51+
constructor(name, attackDamage) {
52+
super(name);
53+
this.attackDamage = attackDamage;
54+
}
55+
}
56+
57+
Object.assign(Turret.prototype, attacker);
58+
59+
const turret = new Turret("Turret", 5);
60+
const character = new Character("Character", 3, 100);
61+
const wall = new Wall("Wall", 200);
62+
63+
turret.attack(character);
64+
character.move();
65+
character.attack(wall);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class Entity {
2+
constructor(name, attackDamage, health) {
3+
this.name = name;
4+
this.attackDamage = attackDamage;
5+
this.health = health;
6+
}
7+
8+
move() {
9+
console.log(`${this.name} moved`);
10+
}
11+
12+
attack(targetEntity) {
13+
console.log(
14+
`${this.name} attacked ${targetEntity.name} for ${this.attackDamage} damage`
15+
);
16+
targetEntity.takeDamage(this.attackDamage);
17+
}
18+
19+
takeDamage(amount) {
20+
this.health -= amount;
21+
console.log(`${this.name} has ${this.health} health remaining`);
22+
}
23+
}
24+
25+
class Character extends Entity {}
26+
27+
class Wall extends Entity {
28+
constructor(name, health) {
29+
super(name, 0, health);
30+
}
31+
32+
move() {
33+
return null;
34+
}
35+
36+
attack() {
37+
return null;
38+
}
39+
}
40+
41+
class Turret extends Entity {
42+
constructor(name, attackDamage) {
43+
super(name, attackDamage, -1);
44+
}
45+
46+
move() {
47+
return null;
48+
}
49+
50+
takeDamage() {
51+
return null;
52+
}
53+
}
54+
55+
const turret = new Turret("Turret", 5);
56+
const character = new Character("Character", 3, 100);
57+
const wall = new Wall("Wall", 200);
58+
59+
turret.attack(character);
60+
character.move();
61+
character.attack(wall);

0 commit comments

Comments
 (0)