samedi 3 janvier 2015

Decorator pattern - am i doing it wrong?

I'm creating a Java based game. My game has objects of several different types: "player", "enemy" (with several different sub types), "scene objects". All those classes extend one abstract class that's common for all other "actor" objects and thus has considerable number of abstract and protected methods.


Now, i want to add a shadow for some objects in the game. In theory, that sounds like a perfect job for a decorator pattern right? The problem is that i don't know if i'm doing something wrong because applying a shadow decorator would require me to implement all those abstract methods from my "root" class. That's not so easy because my root class is rather complex (there's physics involved, key-press listeners etc). Also, the way "player" behaves is not so similar to the way "scene object" behaves. I can't just implement "shadow" in a top class because sometimes i want a certain "actor" on the scene to lose it's shadow dynamically.


That's why i have few doubts:


1) should i even consider implementing decorator in situations like this?


2) if yes, how to painlessly implement a decorator that decorates complex base abstract classes?


Aucun commentaire:

Enregistrer un commentaire