1

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 and if I add all of them to my class, I'm affraid that my class becomes too big. I also would like to avoid editing my class every times a new situation arises.

I firstly thought about having a class by case but I have too many cases (about 5 criteria and for each one 6 or 7 possible different values) so about 50 or 60 different cases in full.

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

1 Answer 1

3

Create separate classes for your cases that extend abstract class with common methods, then use a factory DP to load the required class

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you Auris, I also hesitated to create a new class by case but I have a lot of different cases (about 8 different $_POST['type'], 5 different $_POST['mode'], and 3 or 4 other criteria and I must have about 50 or 60 cases in full) so if necessary, I will try to eliminate useless cases (if I can) but, and I hope you understand me, I would prefer another solution
you probably will need to abstract your structure quite a bit and split it in to groups of cases, so you are looking in to building a small library instead of one class to handle them all. But that is what OOP is all about. once you create your Entity structure (that for a case like yours will probably look more like a tree than a flat set of classes.) all you will have to do is write a factory class that returns a correct object. Adding methods to objects is not possible, you can only load objects in to objects, but that is avery messy approach and using a factory is a much cleaner approach.
Thank you for your answer, your explanation gave me an idea. I'm going to create a common entity with common methods and maybe different classes typed Mode1Module, Mode2Module, Type1Module, ... linked to my Entity and containing my methods
Hi, this could work. But the module linking could be a bit of a pain (all depends on how you write your loader. Anyways, glad I could help in some way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.