Skip to content

Commit 31404a2

Browse files
authored
Merge pull request dubesar#588 from sourabhvarshney111/Decorator
Added Decorator Pattern
2 parents 29334ba + 3a7b64f commit 31404a2

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Reference: https://www.tutorialspoint.com/design_pattern/decorator_pattern.htm
3+
*/
4+
5+
interface Shape {
6+
void draw();
7+
}
8+
9+
class Rectangle implements Shape {
10+
11+
@Override
12+
public void draw() {
13+
System.out.println("Shape: Rectangle");
14+
}
15+
}
16+
17+
class Circle implements Shape {
18+
19+
@Override
20+
public void draw() {
21+
System.out.println("Shape: Circle");
22+
}
23+
}
24+
25+
abstract class ShapeDecorator implements Shape {
26+
protected Shape decoratedShape;
27+
28+
public ShapeDecorator(Shape decoratedShape){
29+
this.decoratedShape = decoratedShape;
30+
}
31+
32+
public void draw(){
33+
decoratedShape.draw();
34+
}
35+
}
36+
37+
class RedShapeDecorator extends ShapeDecorator {
38+
39+
public RedShapeDecorator(Shape decoratedShape) {
40+
super(decoratedShape);
41+
}
42+
43+
@Override
44+
public void draw() {
45+
decoratedShape.draw();
46+
setRedBorder(decoratedShape);
47+
}
48+
49+
private void setRedBorder(Shape decoratedShape){
50+
System.out.println("Border Color: Red");
51+
}
52+
}
53+
54+
public class DecoratorPattern {
55+
public static void main(String[] args) {
56+
57+
Shape circle = new Circle();
58+
59+
Shape redCircle = new RedShapeDecorator(new Circle());
60+
61+
Shape redRectangle = new RedShapeDecorator(new Rectangle());
62+
System.out.println("Circle with normal border");
63+
circle.draw();
64+
65+
System.out.println("\nCircle of red border");
66+
redCircle.draw();
67+
68+
System.out.println("\nRectangle of red border");
69+
redRectangle.draw();
70+
}
71+
}
72+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
### Decorator Pattern
2+
3+
### Introduction
4+
5+
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.
6+
7+
This pattern creates a decorator class which wraps the original class and provides additional functionality keeping class methods signature intact.
8+
9+
### Advantages
10+
11+
- It is flexible than inheritance because inheritance adds responsibility at compile time but decorator pattern adds at run time.
12+
- We can have any number of decorators and also in any order.
13+
- It extends functionality of object without affecting any other object.
14+
15+
### Disadvantages
16+
17+
- Code maintainability is difficult because this pattern creates lots of similar decorators which are sometimes hard to maintain and distinguish.
18+
19+
### Programming
20+
21+
- We are 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.
22+
23+
- RedShapeDecorator is concrete class implementing ShapeDecorator.
24+
25+
- DecoratorPatternDemo, our demo class will use RedShapeDecorator to decorate Shape objects.

0 commit comments

Comments
 (0)