GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Technologies
  • Databases
  • Computer Programming
  • Web Development
  • Java Technologies
  • Computer Science
  • Mobile Development
  • Big Data & Analytics
  • Microsoft Technologies
  • DevOps
  • Latest Technologies
  • Machine Learning
  • Digital Marketing
  • Software Quality

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialspoint.com — Design Patterns - Decorator Pattern

Design Patterns Tutorial
  • Design Patterns - Home
  • Design Patterns - Overview

Creational Design Patterns

  • Design Patterns - Factory Pattern
  • Abstract Factory Pattern
  • Design Patterns - Singleton Pattern
  • Design Patterns - Builder Pattern
  • Design Patterns - Prototype Pattern

Structural Design Patterns

  • Design Patterns - Adapter Pattern
  • Design Patterns - Bridge Pattern
  • Design Patterns - Filter Pattern
  • Design Patterns - Composite Pattern
  • Design Patterns - Decorator Pattern
  • Design Patterns - Facade Pattern
  • Design Patterns - Flyweight Pattern
  • Design Patterns - Proxy Pattern
  • Chain of Responsibility Pattern

Behavioral Design Patterns

  • Design Patterns - Command Pattern
  • Design Patterns - Interpreter Pattern
  • Design Patterns - Iterator Pattern
  • Design Patterns - Mediator Pattern
  • Design Patterns - Memento Pattern
  • Design Patterns - Observer Pattern
  • Design Patterns - State Pattern
  • Design Patterns - Strategy Pattern
  • Design Patterns - Template Pattern
  • Design Patterns - Visitor Pattern

J2EE Design Patterns

  • Design Patterns - Null Object Pattern
  • Design Patterns - MVC Pattern
  • Business Delegate Pattern
  • Composite Entity Pattern
  • Data Access Object Pattern
  • Front Controller Pattern
  • Intercepting Filter Pattern
  • Service Locator Pattern
  • Transfer Object Pattern

Design Patterns Useful Resources

  • Design Patterns - Questions/Answers
  • Design Patterns - Quick Guide
  • Design Patterns - Useful Resources
  • Design Patterns - Discussion
Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Online Resume Builder
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

Design Patterns - Decorator Pattern



Previous
Quiz
Next

Overview

Decorator pattern allows a user to add new functionality 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 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.

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.

Decorator Pattern UML Diagram

Step 1

Create an interface.

Shape.java

package com.tutorialspoint;

public interface Shape {
   void draw();
}

Step 2

Create concrete classes implementing the same interface.

Rectangle.java

package com.tutorialspoint;

public class Rectangle implements Shape {
   @Override
   public void draw() {
      System.out.println("Shape: Rectangle");
   }
}

Circle.java

package com.tutorialspoint;

public class Circle implements Shape {
   @Override
   public void draw() {
      System.out.println("Shape: Circle");
   }
}

Step 3

Create abstract decorator class implementing the Shape interface.

ShapeDecorator.java

package com.tutorialspoint;

public abstract class ShapeDecorator implements Shape {
   protected Shape decoratedShape;

   public ShapeDecorator(Shape decoratedShape){
      this.decoratedShape = decoratedShape;
   }

   public void draw(){
      decoratedShape.draw();
   }	
}

Step 4

Create concrete decorator class extending the ShapeDecorator class.

RedShapeDecorator.java

package com.tutorialspoint;

public class RedShapeDecorator extends ShapeDecorator {

   public RedShapeDecorator(Shape decoratedShape) {
      super(decoratedShape);		
   }

   @Override
   public void draw() {
      decoratedShape.draw();	       
      setRedBorder(decoratedShape);
   }

   private void setRedBorder(Shape decoratedShape){
      System.out.println("Border Color: Red");
   }
}

Example - Usage of Decorator Pattern

Use the RedShapeDecorator to decorate Shape objects.

DecoratorPatternDemo.java

package com.tutorialspoint;

public class DecoratorPatternDemo {
   public 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("\nCircle of red border");
      redCircle.draw();

      System.out.println("\nRectangle of red border");
      redRectangle.draw();
   }
}

interface Shape {
   void draw();
}

class Rectangle implements Shape {
   @Override
   public void draw() {
      System.out.println("Shape: Rectangle");
   }
}

class Circle implements Shape {
   @Override
   public void draw() {
      System.out.println("Shape: Circle");
   }
}

abstract class ShapeDecorator implements Shape {
   protected Shape decoratedShape;

   public ShapeDecorator(Shape decoratedShape){
      this.decoratedShape = decoratedShape;
   }

   public void draw(){
      decoratedShape.draw();
   }	
}

class RedShapeDecorator extends ShapeDecorator {

   public RedShapeDecorator(Shape decoratedShape) {
      super(decoratedShape);		
   }

   @Override
   public void draw() {
      decoratedShape.draw();	       
      setRedBorder(decoratedShape);
   }

   private void setRedBorder(Shape decoratedShape){
      System.out.println("Border Color: Red");
   }
}

Output

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: Red
Tutorix - AI Tutor
Previous Quiz Next
Advertisements
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.