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.

Implementation

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.

decorator_pattern_uml_diagram

Step 1

Create an interface.

Shape.java

Step 2

Create concrete classes implementing the similar interface.

Rectangle.java

Circle.java

Step 3

Construct abstract decorator class implementing the Shape interface.

ShapeDecorator.java

Step 4

Build concrete decorator class extending the Shape Decorator class.

RedShapeDecorator.java

Step 5

Make use of the RedShapeDecorator to decorate Shape objects.

DecoratorPatternDemo.java

Step 6

Verify the output.

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

Design Patterns Topics