- Transfer Object Pattern
- Service Locator Pattern
- Intercepting Filter Pattern
- Front Controller Pattern
- Data Access Object Pattern
- Composite Entity Pattern
- Business Delegate Pattern
- Design Patterns - MVC Pattern
- Design Patterns - Visitor Pattern
- Design Patterns - Template Pattern
- Design Patterns - Strategy Pattern
- Design Patterns - Null Object Pattern
- Design Patterns - State Pattern
- Design Patterns - Observer Pattern
- Design Patterns - Memento Pattern
- Design Patterns - Mediator Pattern
- Design Patterns - Iterator Pattern
- Design Patterns - Interpreter Pattern
- Design Patterns - Command Pattern
- Chain of Responsibility Pattern
- Design Patterns - Proxy Pattern
- Design Patterns - Flyweight Pattern
- Design Patterns - Facade Pattern
- Design Patterns - Decorator Pattern
- Design Patterns - Composite Pattern
- Design Patterns - Filter Pattern
- Design Patterns - Bridge Pattern
- Design Patterns - Adapter Pattern
- Design Patterns - Prototype Pattern
- Design Patterns - Builder Pattern
- Design Patterns - Singleton Pattern
- Abstract Factory Pattern
- Design Patterns - Factory Pattern
- Design Patterns - Overview
- Design Patterns - Home
Design Patterns Resources
- Design Patterns - Discussion
- Design Patterns - Useful Resources
- Design Patterns - Quick Guide
- Design Patterns - Questions/Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Design Patterns - Decorator Pattern
Decorator pattern allows a user to add new functionapty to an existing object without altering its structure. This type of design pattern comes under structural pattern as this pattern acts as a wrapper to existing class.
This pattern creates a decorator class which wraps the original class and provides additional functionapty 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.
Implementation
We re going to create a Shape interface and concrete classes implementing the Shape interface. We will then create an abstract decorator class ShapeDecorator implementing the Shape interface and having Shape object as its instance variable.
RedShapeDecorator is concrete class implementing ShapeDecorator.
DecoratorPatternDemo, our demo class will use RedShapeDecorator to decorate Shape objects.
Step 1
Create an interface.
Shape.java
pubpc interface Shape { void draw(); }
Step 2
Create concrete classes implementing the same interface.
Rectangle.java
pubpc class Rectangle implements Shape { @Override pubpc void draw() { System.out.println("Shape: Rectangle"); } }
Circle.java
pubpc class Circle implements Shape { @Override pubpc void draw() { System.out.println("Shape: Circle"); } }
Step 3
Create abstract decorator class implementing the Shape interface.
ShapeDecorator.java
pubpc abstract class ShapeDecorator implements Shape { protected Shape decoratedShape; pubpc ShapeDecorator(Shape decoratedShape){ this.decoratedShape = decoratedShape; } pubpc void draw(){ decoratedShape.draw(); } }
Step 4
Create concrete decorator class extending the ShapeDecorator class.
RedShapeDecorator.java
pubpc class RedShapeDecorator extends ShapeDecorator { pubpc RedShapeDecorator(Shape decoratedShape) { super(decoratedShape); } @Override pubpc void draw() { decoratedShape.draw(); setRedBorder(decoratedShape); } private void setRedBorder(Shape decoratedShape){ System.out.println("Border Color: Red"); } }
Step 5
Use the RedShapeDecorator to decorate Shape objects.
DecoratorPatternDemo.java
pubpc class DecoratorPatternDemo { pubpc static void main(String[] args) { Shape circle = new Circle(); Shape redCircle = new RedShapeDecorator(new Circle()); Shape redRectangle = new RedShapeDecorator(new Rectangle()); System.out.println("Circle with normal border"); circle.draw(); System.out.println(" Circle of red border"); redCircle.draw(); System.out.println(" Rectangle of red border"); redRectangle.draw(); } }
Step 6
Verify the output.
Circle with normal border Shape: Circle Circle of red border Shape: Circle Border Color: Red Rectangle of red border Shape: Rectangle Border Color: RedAdvertisements