creativity in code.

creativity is that spark of electricity before an explosion.
it allows us, in a single instance to explore all possibilities without hesitation.

Programming - Decorator Structural Design Pattern


Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.


The classes and/or objects participating in this pattern are:


  • Componentand
    and and
    • defines the interface for objects that can have responsibilities added to them dynamically.
  • ConcreteComponentand
    and and
    • defines an object to which additional responsibilities can be attached.
  • Decoratorand
    and
    • maintains a reference to a Component object and defines an interface that conforms to Component's interface.
  • ConcreteDecoratorand
    and
    • adds responsibilities to the component.
// Decorator pattern -- Structural example

using
System;

namespace
DoFactory.GangOfFour.Decorator.Structural
{

// MainApp test application

class MainApp
{
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.Read();
}
}

// "Component"

abstract class Component
{
public abstract void Operation();
}

// "ConcreteComponent"

class ConcreteComponent : Component
{
public override void Operation()
{
Console.WriteLine("ConcreteComponent.Operation()");
}
}

// "Decorator"

abstract class Decorator : Component
{
protected Component component;

public void SetComponent(Component component)
{
this.component = component;
}

public override void Operation()
{
if (component != null)
{
component.Operation();
}
}
}

// "ConcreteDecoratorA"

class ConcreteDecoratorA : Decorator
{
private string addedState;

public override void Operation()
{
base.Operation();
addedState = "New State";
Console.WriteLine("ConcreteDecoratorA.Operation()");
}
}

// "ConcreteDecoratorB"

class ConcreteDecoratorB : Decorator
{
public override void Operation()
{
base.Operation();
AddedBehavior();
Console.WriteLine("ConcreteDecoratorB.Operation()");
}

void AddedBehavior()
{
}
}
}


Output expected:


ConcreteComponent.Operation()
ConcreteDecoratorA.Operation()
ConcreteDecoratorB.Operation()



Other pages of interest:
[+] Programming - Observer Behavioral Design Pattern
[+] Programming - Visitor Behavioral Design Pattern
[+] Microsoft releases Protocol documentation for several of its products to appease EU regulation
[+] Microsoft Announces New Support for Silverlight by Content Companies Worldwide
[+] Silverlight 2 on Mobile from Nokia
[+] Programming - Design patterns in Software
[+] .NET Enterprise provides a truly robust and scalable platform to build business applications on I..
[+] The Future of Javascript - The Creator ponders
[+] Java and .NET both bring something to the Enterprise arena


Comments(0)

add comment Add Comment

Add your comment

Email address:
Your name:
Do you want your email to be visible?: