Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decorator Pattern vs Inheritance with examples

I've been experimenting with the decorator pattern to extend functionality of code you do not want to touch for example and I see how to implement it however I am now unsure why you don't just inherit from the original class and extend that way.

I have read that the decorator pattern allows you to add functionality at runtime whereas inheritance means its there at compile time.

I don't understand this.

Could someone explain this, provide examples and explain when its better to use decorator vs inheritance.

Thanks

like image 912
Jon Avatar asked Jul 26 '12 11:07

Jon


People also ask

What is difference between decorator and inheritance?

With decorators, responsibilities can be added and removed at run-time simply by attaching and detaching them. In contrast, inheritance requires creating a new class for each additional responsibility (e.g., BorderedScrollableTextView, BorderedTextView).

Does decorator pattern use inheritance?

The decorator pattern provides a flexible alternative to inheritance for extending objects functionality. This pattern is designed in a way that multiple decorators can be stacked on top of each other, each adding new functionality.

What is Decorator design pattern example?

The decorator design pattern is a structural pattern, which provides a wrapper to the existing class. Decorator design pattern uses abstract classes or interfaces with the composition to implement the wrapper.

What is the applicability and example of decorator pattern?

This pattern creates a decorator class which wraps the original class and provides additional functionality keeping class methods signature intact. We are demonstrating the use of decorator pattern via following example in which we will decorate a shape with some color without alter shape class.


1 Answers

Suppose you create a View class that displays your items in a certain way. Now you decide you also want a version of it which is scrollable, so you create a ScrollableView which inherits the View. Later you decide you also want a version with a border so you now need to make a BorderedView and a BorderdScrollableView.

If on the other hand you could make a decorator for each added styling. You would have the following classes:

  • View
  • ScrollableDecorator
  • BorderedDecorator

When you want a bordered scroll view you do:

new BorderedDecorator(new ScrollableDecorator(new View())).

So you can configure any combination of this with just the 3 classes. And you can add or remove them at runtime (suppose you click a button that says add border, you now wrap your view with a BorderDecorator ... while whith inheritance you need to implemented this view class if you haven't already, or you need to create a new view instance and copy all relevant data from the first view to the second view which is not as easy to do as just adding or removing wrappers).

like image 180
Razvi Avatar answered Oct 14 '22 12:10

Razvi