Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Interface segregation principle */
- /* segregate interfaces and make it smaller */
- /* whenever you have an interface, you need everything that implements that interface, to use every single portion of that interface */
- /* each for objects, each for classes only ever implement the information that can used and never implement that cant used*/
- /* for Bad example
- - Wall cant move, so having this functionality dosent make sense, so it dosent use all Entity parts, so we braking the interface segregation principle
- */
- /* Bad */
- 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)
- /* GOOD */
- 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)
Advertisement
Add Comment
Please, Sign In to add comment