Design Pattern means that “we had some problem then we found the solution and gave name to this solution” and Decorator is also one of the solution so we need to find the problem that Decorator has solved and how ?
This design pattern comes under Structural Category .
================================================================
C++ Decorator Design Pattern :
Standard Definition:
Decorator Design Pattern attaches additional responsibilities to an object dynamically. Decorators provides a flexible alternative to sub classing for extending functionality. Some time we want add responsibilities to individual object not entire class.
In layman language:
Adding extra behaviour or responsibility to an object not entire class .
Problem:
We want to add some extra functionality to some objects or individual object at run time BUT not entire class or all object.
Solution:
We have one core class and its abstract class, we will create one class named as decorator (its derived from abstract class, internally it will call core class functionality) , for each extra functionality or extra behaviour or extra state we will derive a new class from decorator class it will add extra thing to core functionality, this solution called as “decorator design pattern“.
When we will use decorator design pattern:
- When we want to add extra functionality or extra behaviour or extra state to few object not all objects.
- when we don’t want add functionality to entire class.
- when inheritance is not right choice to add functionality in main class.
Advantage:
- Loose coupling (each derived decorator is represented in separate class so we can handle the different responsibility in different class).
- Flexible (We can add extra responsibility to an object at any time without polluting existing code).
Class Diagram:
Class Diagram of real example:
Source code of real example in c++:
/* * File:decorator.cpp * This file is described the Decorator Design Pattern with help of example * Author: Alpha * Date: 3 Jan 2021 */ //Header File #include<iostream> //Visual Class class Visual { public: virtual void Draw() = 0; }; //Text View class actual operation happens inside class TextView : public Visual { public: void Draw() { std::cout<<"Drawing the text in textview"<<std::endl; } }; //Decorator class class Decorator: public Visual { Visual* m_vis; public: Decorator(Visual* vis):m_vis(vis){} void Draw(){ m_vis->Draw();} }; //Border Decorator class BorderDecorator:public Decorator { int m_width; void DrawBorder() {std::cout<<"Drawing Border to text"<<std::endl;} public: BorderDecorator(Visual* vis): Decorator(vis){} ~BorderDecorator(){} void Draw() { Decorator::Draw(); DrawBorder(); } }; //Client int main() { std::cout<<"Decorator Design pattern, adding responsibility to indivial object"<<std::endl; Visual* v = new TextView; std::cout<<"Creating Decorator object No extra feature"<<std::endl; Decorator obj1(v); obj1.Draw(); std::cout<<"Creating Border Decorator object, extra feature"<<std::endl; BorderDecorator obj2(v); obj2.Draw(); delete v; return 0; };
Compilation command:
g++ -std=c++11 decorator.cpp
Output:
Decorator Design pattern, adding responsibility to indivial object
Creating Decorator object No extra feature
Drawing the text in textview
Creating Border Decorator object, extra feature
Drawing the text in textview
Drawing Border to text