You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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