I'm pretty new with patterns, and I'm learning Pattern Decorator for the program I have to write.
Studying online, I found an example Decorator pattern (this is Java pseudo-code):
class Solution1 { static interface Component { void doStuff(); } static class MyComponent implements Component { public void doStuff() {
When I analyzed this example, I realized that in the past I made a very similar image, but in a different way:
import java.util.*; class Solution2 { static interface Component { void doStuff(); } static class MyComponent implements Component { public void doStuff() {
In my opinion, the second example can be used in the same situations where you can use the Decorator template, but it is more flexible (you can, for example, remove or rearrange the components in the list), so my questions are:
- Is solution 1 (the correct decorator pattern) better than solution 2? Why?
- Is it possible to add functions to remove instances in solution 1?
- Is it possible to add functions to reorder instances in solution 1?
source share