Skip to content

Commit

Permalink
add interface segregation principle
Browse files Browse the repository at this point in the history
  • Loading branch information
arpitmehrotra committed Feb 11, 2023
1 parent 078d2f9 commit b10c967
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
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.

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.

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.
65 changes: 65 additions & 0 deletions interface-segregation-principle/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class Entity {
constructor(name) {
this.name = name;
}
}

const mover = {
move() {
console.log(`${this.name} moved`);
},
};

const attacker = {
attack(targetEntity) {
console.log(
`${this.name} attacked ${targetEntity.name} for ${this.attackDamage} damage`
);
targetEntity.takeDamage(this.attackDamage);
},
};

const hasHealth = {
takeDamage(amount) {
this.health -= amount;
console.log(`${this.name} has ${this.health} health remaining`);
},
};

class Character extends Entity {
constructor(name, attackDamage, health) {
super(name);
this.attackDamage = attackDamage;
this.health = health;
}
}

Object.assign(Character.prototype, mover);
Object.assign(Character.prototype, attacker);
Object.assign(Character.prototype, hasHealth);

class Wall extends Entity {
constructor(name, health) {
super(name);
this.health = health;
}
}

Object.assign(Wall.prototype, hasHealth);

class Turret extends Entity {
constructor(name, attackDamage) {
super(name);
this.attackDamage = attackDamage;
}
}

Object.assign(Turret.prototype, attacker);

const turret = new Turret("Turret", 5);
const character = new Character("Character", 3, 100);
const wall = new Wall("Wall", 200);

turret.attack(character);
character.move();
character.attack(wall);
61 changes: 61 additions & 0 deletions interface-segregation-principle/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class Entity {
constructor(name, attackDamage, health) {
this.name = name;
this.attackDamage = attackDamage;
this.health = health;
}

move() {
console.log(`${this.name} moved`);
}

attack(targetEntity) {
console.log(
`${this.name} attacked ${targetEntity.name} for ${this.attackDamage} damage`
);
targetEntity.takeDamage(this.attackDamage);
}

takeDamage(amount) {
this.health -= amount;
console.log(`${this.name} has ${this.health} health remaining`);
}
}

class Character extends Entity {}

class Wall extends Entity {
constructor(name, health) {
super(name, 0, health);
}

move() {
return null;
}

attack() {
return null;
}
}

class Turret extends Entity {
constructor(name, attackDamage) {
super(name, attackDamage, -1);
}

move() {
return null;
}

takeDamage() {
return null;
}
}

const turret = new Turret("Turret", 5);
const character = new Character("Character", 3, 100);
const wall = new Wall("Wall", 200);

turret.attack(character);
character.move();
character.attack(wall);

0 comments on commit b10c967

Please sign in to comment.