i am learning software design pattern and want to know the difference between bridge and decorator pattern
interface Iconcept
{
void action();
}
class concept : Iconcept
{
public void action()
{
Console.WriteLine("walking ");
}
}
class decoratorA : Iconcept
{
Iconcept concept;
public decoratorA(Iconcept concept)
{
this.concept = concept;
}
public void action()
{
concept.action();
Console.WriteLine("with his dog");
}
}
class decoratorB : Iconcept
{
Iconcept concept;
public decoratorB(Iconcept concept)
{
this.concept = concept;
}
public void action()
{
concept.action();
Console.WriteLine("in the rain");
}
}
class client
{
static void Main()
{
Iconcept concept = new concept();
concept.action();
new decoratorA(concept).action();
new decoratorB(concept).action();
Console.ReadLine();
}
}
decorator pattern
definition:Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
nowif we take this code for the decorator pattern, i have an implementation by the name concept and i am extending/decorating the implementation with new implementation class name as decoratorA and decoratorB
bridge pattern
definition: Decouple an abstraction from its implementation so that the two can vary independently.
i have a old implementation by the name concept and without doing any changes in the old implementation i am implementing a new implementation.
so what differentiates the two pattern
Aucun commentaire:
Enregistrer un commentaire