PC Review


Reply
Thread Tools Rate Thread

Decorator Pattern - adding values dynamically

 
 
Doug
Guest
Posts: n/a
 
      26th Jul 2006
I am looking at using the decorator pattern to create a rudimentary
stored proc generator but am unsure about something. For each class
that identifies a part of the stored proc, what if I want to add a
value dynamically. I'm including some code to show what I mean. This
is real basic on what I want to do:

using System;

namespace ClassLibrary1
{
public interface Value
{
string GetValue();
}

public abstract class Decorator : Value
{
protected Value _nextValue;

public Decorator(Value theValue)
{
_nextValue = theValue;
}
public virtual string GetValue()
{
return _nextValue.GetValue();
}
}

public class Header : Value
{
private string _description = "*********************************";

public Header()
{}
public string GetValue()
{
return _description;
}
}

public class Name : Decorator
{
private string _description = "Name:";

public Name(Value component) : base (component)
{
}
public override string GetValue()
{
return _nextValue.GetValue() + "\r\n" + _description;
}
}

public class Date : Decorator
{
private string _description = "Date:";

public Date(Value component) : base (component)
{
}
public override string GetValue()
{
return _nextValue.GetValue() + "\r\n" + _description;
}
}

public class Developer : Decorator
{
private string _description = "Developer:";

public Developer(Value component) : base (component)
{
}
public override string GetValue()
{
return _nextValue.GetValue() + "\r\n" + _description;
}
}

}

I've tested this and called it like so:

ClassLibrary1.Value StoredProc = new ClassLibrary1.Developer(new
ClassLibrary1.Date(new ClassLibrary1.Name(new
ClassLibrary1.Header())));
MessageBox.Show(StoredProc.GetValue());

My question would be if I wanted to pass an actual name into the Name
class or a date into the Date class, etc. I'm assuming I do that in
the constructor of each class, but wouldn't that make calling each
class messy? Does that perhaps negate the using of the Decorator
pattern here?

 
Reply With Quote
 
 
 
 
Greg Young
Guest
Posts: n/a
 
      27th Jul 2006
Cross post ... answerred in dotnet.general
"Doug" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

>I am looking at using the decorator pattern to create a rudimentary
> stored proc generator but am unsure about something. For each class
> that identifies a part of the stored proc, what if I want to add a
> value dynamically. I'm including some code to show what I mean. This
> is real basic on what I want to do:
>
> using System;
>
> namespace ClassLibrary1
> {
> public interface Value
> {
> string GetValue();
> }
>
> public abstract class Decorator : Value
> {
> protected Value _nextValue;
>
> public Decorator(Value theValue)
> {
> _nextValue = theValue;
> }
> public virtual string GetValue()
> {
> return _nextValue.GetValue();
> }
> }
>
> public class Header : Value
> {
> private string _description = "*********************************";
>
> public Header()
> {}
> public string GetValue()
> {
> return _description;
> }
> }
>
> public class Name : Decorator
> {
> private string _description = "Name:";
>
> public Name(Value component) : base (component)
> {
> }
> public override string GetValue()
> {
> return _nextValue.GetValue() + "\r\n" + _description;
> }
> }
>
> public class Date : Decorator
> {
> private string _description = "Date:";
>
> public Date(Value component) : base (component)
> {
> }
> public override string GetValue()
> {
> return _nextValue.GetValue() + "\r\n" + _description;
> }
> }
>
> public class Developer : Decorator
> {
> private string _description = "Developer:";
>
> public Developer(Value component) : base (component)
> {
> }
> public override string GetValue()
> {
> return _nextValue.GetValue() + "\r\n" + _description;
> }
> }
>
> }
>
> I've tested this and called it like so:
>
> ClassLibrary1.Value StoredProc = new ClassLibrary1.Developer(new
> ClassLibrary1.Date(new ClassLibrary1.Name(new
> ClassLibrary1.Header())));
> MessageBox.Show(StoredProc.GetValue());
>
> My question would be if I wanted to pass an actual name into the Name
> class or a date into the Date class, etc. I'm assuming I do that in
> the constructor of each class, but wouldn't that make calling each
> class messy? Does that perhaps negate the using of the Decorator
> pattern here?
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
decorator pattern help rodchar Microsoft C# .NET 2 17th Jun 2009 03:57 PM
Decorator pattern in C# proxyuser Microsoft C# .NET 4 19th Aug 2008 12:30 AM
Mapping a Decorator Pattern in NHibernate Diego Jancic Microsoft C# .NET 3 11th Jun 2007 08:40 AM
Decorator pattern - adding values dynamically Doug Microsoft Dot NET 5 26th Jul 2006 10:22 PM
Decorator pattern for data layer component Einar Høst Microsoft C# .NET 2 12th Jul 2004 02:34 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:39 PM.