jeudi 3 mai 2018

How to add new functionalities to a class without change the called method in Java

I would like to design a class which can (or not) implement functionalities, and be open to add new ones in the future. Those functionalities must be executed by a method. For instance:

class Hero()

doDamage(){
//simpleDamage
}

And

class BetterHero() implements EnableCriticAttack

doDamage(){
//damage with critic attack chance
}

Also,

class WizardBetterHero() implements EnableCriticAttack, MagicDamage

doDamage(){
//damage with critic attack chance + magic damage
}

I would like to add new "special abilities" using the proper design pattern, so doDamage() method will know which abilities to use, and it does properly. I don't want to create different classes, just "add" new functionalities to the created one: open-close principle.

PD: I put the functionalities as interfaces, but it is just an example.

Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire