mardi 25 août 2015

Add dynamically methods to an object

Could you help me to find a solution to my problem?

I have a class that represents an entity (I will call it "Entity") I would like to dynamically add methods to my Entity according to different criteria.

For example:

If $_POST['TYPE'] == 'typeA', I would like to add following methods:

  • method1()
  • method2()
  • method3()
  • method4()

If $_POST['MODE'] == 'modeA', I would like to add following methods:

  • method5()
  • method6()
  • method7()

The problem is that there will be a lot of possible methods and if I add all of them to my class, I'm affraid that my class becomes too big.

So what is the best solution to do that?

Should I add all possible methods to my class?

Should I use inheritance and create all possible kinds of objects (Type1Mode1Entity, Type1Mode2Entity, ...)?

Do you know a design pattern (decorator?) to do that?

Ben

Aucun commentaire:

Enregistrer un commentaire