Decorator template design using inheritance and interfaces

I would like to implement the Decorator design pattern using inheritance ( Decorator extends Component), because I need access to the protected fields / methods of the Component class.

The problem is that the Component class is an algorithm; it performs some preliminary processing during construction and contains a significant amount of data. Now every time I decorate a component, I will create a new instance of Decorator, which will require the creation of a new (useless) instance of Component, which performs unnecessary calculations and stores unnecessary data.

I wanted to use interfaces instead of inheritance, but then I could not get access to the information protected by the component.

Can you worry about wasting resources when extending the Component class? And if so, how can I avoid this without losing access to the information I need?

Finally, I can create an instance of Decorator supplying it with “dummy” data so that it does the minimum amount of computation, but this solution seems messy.

Thank.

+5
source share
2 answers

I'm not sure if this will look like an example decorator at all. Sounds more like a normal old inheritance.

Can you worry about wasting resources when extending the Component class?

Obviously, it depends on how much resources you spend.

And if so, how can I avoid this without losing access to the information I need?

, "" Component, , . .

+2

, Component "". ?

+2

All Articles