D
Doug
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?
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?