mituri

SOLID Interface segregation principle

Feb 14th, 2021
728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Interface segregation principle */
  2.  
  3. /* segregate interfaces and make it smaller */
  4. /* whenever you have an interface, you need everything that implements that interface, to use every single portion of that interface */
  5. /*  each for objects, each for classes only ever implement the information that can used and never implement that cant used*/
  6.  
  7. /* for Bad example
  8.     - Wall cant move, so having this functionality dosent make sense, so it dosent use all Entity parts, so we braking the interface segregation principle
  9. */
  10.  
  11. /* Bad */
  12. class Entity {
  13.   constructor(name, attackDamage, health) {
  14.     this.name = name
  15.     this.attackDamage = attackDamage
  16.     this.health = health
  17.   }
  18.  
  19.   move() {
  20.     console.log(`${this.name} moved`)
  21.   }
  22.  
  23.   attack(targetEntity) {
  24.     console.log(`${this.name} attacked ${targetEntity.name} for ${this.attackDamage} damage`)
  25.     targetEntity.takeDamage(this.attackDamage)
  26.   }
  27.  
  28.   takeDamage(amount) {
  29.     this.health -= amount
  30.     console.log(`${this.name} has ${this.health} health remaining`)
  31.   }
  32. }
  33.  
  34. class Character extends Entity {}
  35.  
  36. class Wall extends Entity {
  37.   constructor(name, health) {
  38.     super(name, 0, health)
  39.   }
  40.  
  41.   move() {
  42.     return null
  43.   }
  44.  
  45.   attack() {
  46.     return null
  47.    }
  48. }
  49.  
  50. class Turret extends Entity {
  51.   constructor(name, attackDamage) {
  52.     super(name, attackDamage, -1)
  53.   }
  54.  
  55.   move() {
  56.     return null
  57.   }
  58.  
  59.   takeDamage() {
  60.     return null
  61.    }
  62. }
  63.  
  64. const turret = new Turret('Turret', 5)
  65. const character = new Character('Character', 3, 100)
  66. const wall = new Wall('Wall', 200)
  67.  
  68. turret.attack(character)
  69. character.move()
  70. character.attack(wall)
  71.  
  72.  
  73.  
  74. /* GOOD */
  75. class Entity {
  76.     constructor(name) {
  77.     this.name = name
  78.   }
  79. }
  80.  
  81. const mover = {
  82.     move() {
  83.     console.log(`${this.name} moved`)
  84.   }
  85. }
  86.  
  87. const attacker = {
  88.     attack(targetEntity) {
  89.     console.log(`${this.name} attacked ${targetEntity.name} for ${this.attackDamage} damage`)
  90.     targetEntity.takeDamage(this.attackDamage)
  91.   }
  92. }
  93.  
  94. const hasHealth = {
  95.     takeDamage(amount) {
  96.     this.health -= amount
  97.     console.log(`${this.name} has ${this.health} health remaining`)
  98.   }
  99. }
  100.  
  101. class Character extends Entity {
  102.     constructor(name, attackDamage, health) {
  103.     super(name)
  104.     this.attackDamage = attackDamage
  105.     this.health = health
  106.   }
  107. }
  108.  
  109. Object.assign(Character.prototype, mover)
  110. Object.assign(Character.prototype, attacker)
  111. Object.assign(Character.prototype, hasHealth)
  112.  
  113. class Wall extends Entity {
  114.     constructor(name, health) {
  115.     super(name)
  116.     this.health = health
  117.   }
  118. }
  119. Object.assign(Wall.prototype, hasHealth)
  120.  
  121. class Turret extends Entity {
  122.     constructor(name, attackDamage) {
  123.     super(name)
  124.     this.attackDamage = attackDamage
  125.   }
  126. }
  127. Object.assign(Turret.prototype, attacker)
  128.  
  129. const turret = new Turret('Turret', 5)
  130. const character = new Character('Character', 3, 100)
  131. const wall = new Wall('Wall', 200)
  132.  
  133. turret.attack(character)
  134. character.move()
  135. character.attack(wall)
  136.  
Advertisement
Add Comment
Please, Sign In to add comment