Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Decorator pattern

Decorator pattern

The Decorator Pattern allows new functionality to be added to an existing object without changing its structure. This type of design pattern belongs to the structural patterns, and it acts as a wrapper around an existing class.

The Decorator Pattern modifies the behavior of an object dynamically by wrapping it in a decorator class.

This pattern creates a decorator class to wrap the original class and, while keeping the class method signatures intact, provides extra functionality.

We demonstrate the use of the Decorator Pattern with the example below. In it, we will decorate a shape with different colors without changing the shape class.

Summary

Intent

Dynamically add extra responsibilities to an object without changing its structure. The Decorator Pattern provides a flexible alternative to inheritance for extending functionality.

Main problem solved

Use Cases

Implementation

Key code

Application Example

  1. Sun Wukong's 72 Transformations: Sun Wukong (ConcreteComponent) gains new abilities through transformations (Decorator).
  2. Framed painting: a painting (ConcreteComponent) can be enhanced for display by adding glass (ConcreteDecorator) and a frame (ConcreteDecorator).

Advantages

Disadvantages

Usage recommendation

Notes

structures

The Decorator Pattern contains the following core roles:

By nesting and wrapping multiple decorator objects, the Decorator Pattern can achieve multi-level functional enhancement. Each concrete decorator class can selectively add new functionality while keeping the object interface consistent.

Create a class that implements

We will create a Shape interface as the bridge implementation and concrete classes that implement the Shape entity class that implements the Shape interface. Then we create an abstract decorator class that implements the ShapeDecoratorinterface, and takes the Shape object as its instance variable.

RedShapeDecorator is the entity class that implements ShapeDecorator .

DecoratorPatternDemo Class uses RedShapeDecorator to decorate Shape object.

Step 1

Create an interface:

Shape.java

public

interface

Shape

{

void

draw

(

)

;

}

Step 2

Create concrete classes that implement 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 concrete classes that implement the Shape the abstract decorator class for the interface.

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 entity decorator class that extends the ShapeDecorator class.

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

Use RedShapeDecorator to decorate Shape object.

DecoratorPatternDemo.java

public

class

DecoratorPatternDemo

{

public

static

void

main

(

String

[

]

args

)

{

Shape

circle

=

new

Circle

(

)

;

ShapeDecorator

redCircle

=

new

RedShapeDecorator

(

new

Circle

(

)

)

;

ShapeDecorator

redRectangle

=

new

RedShapeDecorator

(

new

Rectangle

(

)

)

;

//

Shape redCircle = new RedShapeDecorator(new Circle());

//

Shape redRectangle = new RedShapeDecorator(new Rectangle());

System

.

out

.

println

(

"

Circle with normal border

"

)

;

circle

.

draw

(

)

;

System

.

out

.

println

(

"

\n

Circle of red border

"

)

;

redCircle

.

draw

(

)

;

System

.

out

.

println

(

"

\n

Rectangle of red border

"

)

;

redRectangle

.

draw

(

)

;

}

}

Step 6

Run the program, output result:



Circle with normal border

Shape: Circle



Circle of red border

Shape: Circle

Border Color: Red



Rectangle of red border

Shape: Rectangle

Border Color: Red