Design Patterns Decorator Pattern - Design Patterns

What is Design Pattern Decorator Pattern?

Decorator pattern allow a user to add new functionality to an existing object without changing its structure. This kind of design pattern comes in structural pattern as this pattern acts as a wrapper to live class.

This pattern creates a decorator class which wraps the original class and provide additional functionality keeping class method signature intact.
We are demonstrating the use of decorator pattern via following instance in which we will adorn a shape with some colour without modify shape class.


We're going to form a Shape interface and actual classes implementing the Shape interface. We will then create an abstract decorator class Shape Decorator implementing the Shape interface and having Shape entity as its example inconsistent.

RedShapeDecorator is insubstantial class implementing Shape Decorator.

DecoratorPatternDemo, our presentation class will use RedShapeDecorator to adorn Shape objects.


Step 1

Create an interface.

Step 2

Create concrete classes implementing the similar interface.

Step 3

Construct abstract decorator class implementing the Shape interface.

Step 4

Build concrete decorator class extending the Shape Decorator class.

Step 5

Make use of the RedShapeDecorator to decorate Shape objects.

Step 6

Verify the output.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Design Patterns Topics