Friday, March 24, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home JavaScript

Inheritance vs Composition: Using a Role-Playing Game in JavaScript as an Example

learningcode_x1mckf by learningcode_x1mckf
December 28, 2022
in JavaScript
0
Inheritance vs Composition: Using a Role-Playing Game in JavaScript as an Example
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


You might also like

Toolkit Allows JavaScript Devs to Program Embedded Devices – The New Stack

Select data value from grandparent div? – JavaScript – SitePoint

How to Handle Errors in JavaScript – Programming – MUO – MakeUseOf

Inheritance Issues

  • Duplication of code in kids
  • Extreme complexity within the inheritance hierarchy
  • Altering the habits of the dad or mum can result in errors within the kids

On this article, we are going to take a look at what these issues are about and the way we are able to remedy them utilizing composition.

the issue with object-oriented languages is that they’ve acquired all this implicit surroundings that they carry round with them. You wished a banana however what you bought was a gorilla holding the banana and your complete jungle. – Joe Armstrong, creator of Erlang

Position-Enjoying Sport Inheritance

Contemplate the method of making a hierarchy of role-playing recreation characters. Initially, two varieties of characters are required – Warrior and Mage, every of which has a specific amount of well being and a reputation. These properties are public and might be moved to the dad or mum Character class.

class Character  
   constructor(title)  
      this.title = title; 
      this.well being = 100;
    

A warrior can strike, spending his stamina:

class Warrior extends Character  
   constructor(title)  
      tremendous(title); 
      this.stamina = 100; 
    
   struggle()  
      console.log(`$this.title takes a mighty swing!`); 
      this.stamina--; 
    

And a mage can forged spells that spend some quantity of mana:

class Mage extends Character 
   constructor(title)  
      tremendous(title); 
      this.mana = 100; 
    
   forged()  
      console.log(`$this.title casts a fireball!`); 
   this.mana--; 
    

Paladin Class Drawback

Now, let’s introduce a brand new class, Paladin. A Paladin can each struggle and forged spells. How can we remedy this? Listed here are a few options that share the identical lack of class:

  • We are able to make Paladin a descendant of Character and implement each the struggle() and forged() strategies in it from scratch. On this case, the DRY precept is violated as a result of every of the strategies will probably be duplicated upon creation and can want fixed synchronization with the strategies of the Mage and Fighter lessons to trace modifications.
  • The struggle() and forged() strategies might be carried out on the Character class degree so that every one three character sorts have them. This can be a barely higher answer, however on this case, the developer should override the struggle() methodology for the mage and the forged() methodology for the warrior, changing them with empty strategies or consoling an error.

Composition

These issues might be solved with a practical method utilizing composition. It is sufficient to begin not from their sorts, however from their features. Principally, we have now two key options that decide the skills of the characters – the flexibility to struggle and the flexibility to forged spells.

These options might be set utilizing manufacturing facility features that reach the state that defines the character:

const canCast = (state) => ( 
   forged: (spell) =>  
      console.log(`$state.title casts $spell!`); 
      state.mana--; 
    
) 
const canFight = (state) => ( 
   struggle: () =>  
      console.log(`$state.title slashes on the foe!`); 
      state.stamina--; 
    
)

Thus, a personality is outlined by a set of those skills and preliminary properties, each common (title and well being) and personal (stamina and mana):

const fighter = (title) =>  
   let state =  title, well being: 100, stamina: 100  
   return Object.assign(state, canFight(state)); 
 
const mage = (title) =>  
   let state =  title, well being: 100, mana: 100  
   return Object.assign(state, canCast(state)); 
 
const paladin = (title) =>  
   let state =  title, well being: 100, mana: 100, stamina: 100  
   return Object.assign(state, canCast(state), canFight(state)); 

Conclusion

With composition, you’ll be able to keep away from inheritance issues, and javascript is the right language to take action.

L O A D I N G
. . . feedback & extra!



Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

Toolkit Allows JavaScript Devs to Program Embedded Devices – The New Stack

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Toolkit Allows JavaScript Devs to Program Embedded Devices  The New Stack Source link

Read more

Select data value from grandparent div? – JavaScript – SitePoint

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Select data value from grandparent div? - JavaScript  SitePoint Source link

Read more

How to Handle Errors in JavaScript – Programming – MUO – MakeUseOf

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

How to Handle Errors in JavaScript - Programming  MUO - MakeUseOf Source link

Read more

How to Use the Javascript Slice Method – hackernoon.com

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

How to Use the Javascript Slice Method  hackernoon.com Source link

Read more

Clean Code in JavaScript – SitePoint

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Clean Code in JavaScript  SitePoint Source link

Read more
Next Post
7 Best JavaScript File Uploads APIs in 2023

7 Best JavaScript File Uploads APIs in 2023

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Java Devops Engineer âú‚¬û€œ LWG2162 at Mediro ICT

Java Devops Engineer âú‚¬û€œ LWG2162 at Mediro ICT

November 26, 2022
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Senior Java Developer – IT-Online

March 18, 2023
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

LeetCode 1472. Design Browser History (Javascript Edition) | by … – DataDrivenInvestor

February 7, 2023

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Java Developer Survey Reveals Increased Need for Java … – PR Newswire
  • What You Should Definitely Pay Attention to When Hiring Java Developers – Modern Diplomacy
  • Java Web Frameworks Software Market Research Report 2023 … – Los Alamos Monitor

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?