Decorator Pattern
The Decorator pattern provides a way of attaching new state and behavior to an object dynamically.
The original object never comes to know that it is being “decorated”.
One important point to remember while implementing the Decorator pattern is that the decorator (class that takes the responsibility of extending or decorating the base class) will inherit the original class and also contain an object of it
This Pattern does is useful when you want to extend the base class functionality, but do not want to use subclasses and inheritance.
In shot and sweet terms. OPEN for extension, Closed for modification (do not confuse, if you have heard it somewhere else)
The Decorator pattern’s key feature is that it does not rely on inheritance for extending behavior.
(i.e. To extend the behavior of existing object, it does not need to inherit all the characteristics of the object to be decorated).
.
Is-a Relationship
The is-a relationship indicates if a class inherits from Interface the class objects can be used wherever Interface objects are expected.
To extend it further, if there are two classes ClassA and ClassB and both derive from an Interface IMyInterface, then any other class ClassD can use the ClassA and ClassB objects interchangeably.
It simply means if at any place object of ClassA is expected, if you pass object of ClassB, it should be fine.
This concept is the Core of Decorator Pattern.
The Following example explains the theory discussed so far
//Fresher Interface
public interface IFresher
{
string Name { get; set; }
string CodingSkills();
}
//Concrete Fresher Class
public class FreshDeveloper : IFresher
{
private string d_name;
#region IFresher Members
public string Name
{
get{return d_name;}
set{d_name = value;}
}
public string CodingSkills()
{
return string.Format(" AS a Fresher the developer{0} has {1} programming skills", Name, "Non Satisfactory");
}
#endregion
}
//Concrete One Year Experienced
public class OneYearExperienced : IFresher
{
private IFresher d_Developer;
public OneYearExperienced(IFresher developer)
{ d_Developer = developer; }
private string Enhancedskills()
{
return string.Format(" AS One Year Experienced, the developer{0} has {1} skills", Name,"Improved Coding");
}
#region IFresher Members
public string Name
{
get{ return d_Developer.Name;}
set { d_Developer.Name = value; }
}
public string CodingSkills()
{
string s1= d_Developer.CodingSkills();
string s2= Enhancedskills();
return s1 + "" + s2;
}
#endregion
}
//concrete FiveYearExperinced
public class FiveYearExperinced:IFresher
{
private IFresher d_fexp;
public FiveYearExperinced(IFresher developer)
{
d_fexp = developer;
}
private string Enhancedskills()
{
return string.Format(" AS Five Year Experienced, the developer{0} has {1} skills", Name,"Excellent Coding");
}
#region IFresher Members
public string Name
{
get {return d_fexp.Name; }
set { d_fexp.Name = value; }
}
public string CodingSkills()
{
string s1= d_fexp.CodingSkills();
string s2= Enhancedskills();
return s1 + s2;
}
#endregion
}
Let’s Call it passing the object of fresher
IFresher developer = new FreshDeveloper();
developer.Name = "Pradeep";
Response.Write(developer.CodingSkills());
The Result:
AS a Fresher the developer Pradeep has Non Satisfactory programming skills
Now let’s pass the fresher object to OneYearExperienced Class
IFresher developer = new FreshDeveloper();
developer.Name = "Pradeep";
OneYearExperienced oexp = new OneYearExperienced(developer);
Response.Write(oexp.CodingSkills());
The Result:
AS a Fresher the developer Pradeep has Non Satisfactory programming skills
AS One Year Experienced, the developer Pradeep has Improved Coding skills
AS One Year Experienced, the developer Pradeep has Improved Coding skills
Now let’s pass the OneYearExperienced object to FiveYearExperinced Class
IFresher developer = new FreshDeveloper();
developer.Name = "Pradeep";
OneYearExperienced oexp = new OneYearExperienced(developer);
FiveYearExperinced fexp = new FiveYearExperinced(oexp);
Response.Write(fexp.CodingSkills());
The Result:
AS a Fresher the developer Pradeep has Non Satisfactory programming skills
AS One Year Experienced, the developer Pradeep has Improved Coding skills
AS One Year Experienced, the developer Pradeep has Improved Coding skills
AS Five Year Experienced, the developer Pradeep has Excellent Coding skills
Note:
1. If the base class (that needs to be decorated), does not have virtual method available for over-riding, then only we need to use Interface.
2. An important point about the Decorator pattern is that it is based around new objects being created with their own sets of operations.
There are still some aspects to Decorator pattern, like implementing with abstract classes and virtual methods that have not been covered here.
Hope this was helpful.
Till next time we connect, Happy coding!!
Comments
Post a Comment