Latest web development tutorials

Decorator

Decorator (Decorator Pattern) allows you to add new features to an existing object without changing its structure. This type of design patterns belong structural model, it is available as a wrapper class.

This pattern creates a decorative, used to wrap the original class and class methods in maintaining the integrity of the signature under the premise of providing additional functionality.

We use the following examples to demonstrate the decorator pattern. Among them, we will form a decoration in different colors, without changing the shape of the class.

Introduction

Intent: dynamically to an object to add some additional responsibilities.Increased functionality, the decorator pattern compared subclassing more flexible.

Mainly to solve: In general, we often used to extend a class inheritance to achieve, due to the introduction of a static class inheritance features and extended functionality with the increase, the subclass would be swell.

When to use: you do not want an increase in the case of many sub-category extension classes.

How to solve: dividing the specific functional responsibilities, while inheriting the decorator pattern.

Key Code: 1, Component class acts as an abstract role, should not the specific implementation.2, a modified class references and inheritance Component class, specific extension classes override the parent class method.

Application examples: 1, the Monkey King 72 change when he becomes "temple", he simply was a monkey, but he has a temple function.2, regardless of a picture frame there can be hung on the wall, but usually there is a picture frame, and the frame was actually hanging on the wall. Before hanging on the wall, painting can be covered with glass, mounted to the frame house; then painting, glass and frame formed of an object.

Advantages: decorative and decoration can be developed independently, are not coupled to each other, decorative pattern is an alternative mode of inheritance, decorative patterns can dynamically extend a class that implements the function.

Disadvantages: more complex multilayer decorative.

Usage scenarios: 1, extending a class function.2, the dynamic increase functionality, dynamic revoked.

Note: You can substitute inheritance.

achieve

We will create aShapeinterface and implementation of the entity classesShapeinterface. Then we create a realization of abstract decorativeShapeDecoratorShape interface and theShapeobject as its instance variables.

RedShapeDecoratoris to achieve the entity classesShapeDecorator.

DecoratorPatternDemo,our demonstration classes usingRedShapeDecoratorto decorateShapeobject.

Decorator pattern UML diagram

step 1

Create an interface.

Shape.java

public interface Shape {
   void draw ();
}

Step 2

Create entity class that implements the interface.

Rectangle.java

public class Rectangle implements Shape {

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

Circle.java

public class Circle implements Shape {

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

Step 3

Create achieve abstract decorativeShapeinterface.

ShapeDecorator.java

public abstract class ShapeDecorator implements Shape {
   protected Shape decoratedShape;

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

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

Step 4

Create an extended entity decorativeShapeDecoratorclass.

RedShapeDecorator.java

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");
   }
}

Step 5

RedShapeDecoratoruse to decorateShapeobject.

DecoratorPatternDemo.java

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 ();
   }
}

Step 6

Verify 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