/Decorator pattern

Decorator pattern

Asalam Alaikum,
This is Saad Talaat how are you hope you are fine.

This post will be about Design patterns. and we will be talking about the Decorator pattern.

The Decorator Pattern is about Decorating(*) an Object with another so object will be Like “Nested” in each others so to get out a value from an object we need to count all values that are in other Objects that Decorate the the object we need to get the value out of.

to be more specific and clear. suppose we have alot of Computer Science Students which are Extended from a Student class and we need to calculate the total degree for specific students so we call a student and Decorate it with the next student and decorate that next student with the next student..so when we invoke the method CalculateDegree(); of the super decorator  it will iterate to all object till it get to the super Decorator.

So instead of calling each students degree and do arithmetic operations. you will simply call the  CalculateDegree(); of the super Decorator object and it will return the total degree

About the pics : So when i want to get the degree of Student 1 and 2 and 3 i just Decorate them in Each others. so when i do

Student3.CalculateDegree(); it will return me total degree of Student 1,2,3. so when we call Student3.CalculateDegree(); it will invoke Student2.CalculateDegree(); which will calculate Student1.CalculateDegree(); which will return first student degree which will be added to second Student Degree and both will be added to 3rd Student Degree and return the total value :-

Decorator Pattern Description :-

Decorator Pattern attaches additional responsibilities to an object dynamically.Decorators provide a flexible alternative to subclassing for extending functionality.in the code way

Student Class :

Let’s implement the Decorator:-

and we implement base classes for students :-

Let’s implement our Decorator

After Doing all the codes let’s see what can give me an output.

Next post isa will be about Real Decorating…

(*)Decorating : like wrapping