Attach
additional responsibilities to an object dynamically. Decorators provide a
flexible alternative to sub classing for extending functionality.
Participants:
The
classes and/or objects participating in this pattern are:
- Component(Library Item)
- Concrete Component (Book, Video)
Defines
an object to which additional responsibilities can be attached.
- Decorator(Decorator)
- Component's interface.
Concrete
Decorator (browse able). Adds responsibilities to the component.
Example of Decorator Design pattern
Example of Decorator Design pattern
using System;
namespace Decorator.Structural
{
/// <summary>
/// MainApp
startup class for Structural
/// Decorator
Design Pattern.
/// </summary>
class MainApp
{
/// <summary>
/// Entry
point into console application.
/// </summary>
static void Main()
{
//
Create ConcreteComponent and two Decorators
ConcreteComponent c
= new ConcreteComponent();
ConcreteDecoratorA d1
= new ConcreteDecoratorA();
ConcreteDecoratorB d2
= new ConcreteDecoratorB();
//
Link decorators
d1.SetComponent(c);
d2.SetComponent(d1);
d2.Operation();
//
Wait for user
Console.ReadKey();
}
}
/// <summary>
/// The
'Component' abstract class
/// </summary>
abstract class Component
{
public abstract void Operation();
}
/// <summary>
/// The
'ConcreteComponent' class
/// </summary>
class ConcreteComponent : Component
{
public override void Operation()
{
Console.WriteLine("ConcreteComponent.Operation()");
}
}
/// <summary>
/// The
'Decorator' abstract class
/// </summary>
abstract class Decorator : Component
{
protected Component component;
public void SetComponent(Component component)
{
this.component
= component;
}
public override void Operation()
{
if (component
!= null)
{
component.Operation();
}
}
}
/// <summary>
/// The
'ConcreteDecoratorA' class
/// </summary>
class ConcreteDecoratorA : Decorator
{
public override void Operation()
{
base.Operation();
Console.WriteLine("ConcreteDecoratorA.Operation()");
}
}
/// <summary>
/// The
'ConcreteDecoratorB' class
/// </summary>
class ConcreteDecoratorB : Decorator
{
public override void Operation()
{
base.Operation();
AddedBehavior();
Console.WriteLine("ConcreteDecoratorB.Operation()");
}
void AddedBehavior()
{
}
}
}
|
Output
ConcreteComponent.Operation()
ConcreteDecoratorA.Operation() ConcreteDecoratorB.Operation() |
No comments:
Post a Comment