Reputation: 1322
I'm trying to make a reusable tool for my game projects. As you know, every game looks better with its text effects. So that's what I want to do. Creating text effects dynamically.
The Situation;
To make clear everything; If I add a wave effect and shine effect together, I want my Text object waving and shining at the same time.
To make this smoothly, Which design pattern is beneficial ? If you were me How would you approach to this problem?
Notes:
Upvotes: 2
Views: 185
Reputation: 405965
I think you want to look into the Decorator pattern. This would allow you to mix and match different kinds of effects.
The decorator pattern can be used to extend (decorate) the functionality of a certain object statically, or in some cases at run-time, independently of other instances of the same class, provided some groundwork is done at design time. This is achieved by designing a new decorator class that wraps the original class.
In your example, wave effect and shine effect would be two different decorator classes that both extend Text. You'd get the waving and shining effect by wrapping a Text object in both decorators.
Upvotes: 2
Reputation: 12626
This is nearly a no-brainer. Of course the Decorator pattern.
As you can see, decorator itself inherits from a component so you can decorate another decorator. This is how it typically looks like in practice
new ShinyDecorator(new WaveDecorator(new TextComponent()));
You sure can design your own usage since patterns are just a hint what to do, but not how to do it. You don't have to obey the diagram.
Upvotes: 3