473,769 Members | 3,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Decorator pattern - adding values dynamically

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.GetV alue();
}
}

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.GetV alue() + "\r\n" + _description;
}
}

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

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

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

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

}

I've tested this and called it like so:

ClassLibrary1.V alue StoredProc = new ClassLibrary1.D eveloper(new
ClassLibrary1.D ate(new ClassLibrary1.N ame(new
ClassLibrary1.H eader())));
MessageBox.Show (StoredProc.Get Value());

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?

Jul 26 '06 #1
5 1817
I think part of your confusion here is that you are not using the decorator
pattern here (this is basic inheritance?). The decorator pattern would state
that you override to add functionality but you still call the subject's
method (unless you are wanting to work with an instance decorator). consider
the following code.

public class Foo {
public virtual void Bar() {
Console.WriteLi ne("Foo.Bar");
}
}

now there is a requirement that it must also print "hello" we create a
decorator (keep in mind we are only dealing with one ..

public class FooHelloDecorat or {
Foo m_Subject;
public FooHelloDecorat or(Foo _Subject) {
m_Subject = _Subject;
}

public Foo Unwrap() {
return m_Subject;
}
public overrides void Bar() {
Console.WriteLi ne("hello");
m_Subject.Bar() ;
}
}

we apply our decorator by saying

Foo f = new Foo();
f = new FooHelloDecorat or(f);
..
..
..
later we can say
Foo OriginalFoo = f.Unwrap(); //takes the decorator off
As for your question .. this is a general problem with inheritance ..
generics can help you to a great degree with this problem as you can make
the base generic then derive the base to a closed type.

Cheers,

Greg
"Doug" <dn******@dtgne t.comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
>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.GetV alue();
}
}

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.GetV alue() + "\r\n" + _description;
}
}

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

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

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

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

}

I've tested this and called it like so:

ClassLibrary1.V alue StoredProc = new ClassLibrary1.D eveloper(new
ClassLibrary1.D ate(new ClassLibrary1.N ame(new
ClassLibrary1.H eader())));
MessageBox.Show (StoredProc.Get Value());

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?

Jul 26 '06 #2
Sorry didn't type everything :)

correction*

(keep in mind we are only dealing with one method but a decorator would
override every method in the object passing through the calls to its
subject, it has to or else the calls would not work)

Cheers,

Greg
"Greg Young" <dr************ *******@hotmail .comwrote in message
news:uT******** ******@TK2MSFTN GP04.phx.gbl...
>I think part of your confusion here is that you are not using the decorator
pattern here (this is basic inheritance?). The decorator pattern would
state that you override to add functionality but you still call the
subject's method (unless you are wanting to work with an instance
decorator). consider the following code.

public class Foo {
public virtual void Bar() {
Console.WriteLi ne("Foo.Bar");
}
}

now there is a requirement that it must also print "hello" we create a
decorator (keep in mind we are only dealing with one ..

public class FooHelloDecorat or {
Foo m_Subject;
public FooHelloDecorat or(Foo _Subject) {
m_Subject = _Subject;
}

public Foo Unwrap() {
return m_Subject;
}
public overrides void Bar() {
Console.WriteLi ne("hello");
m_Subject.Bar() ;
}
}

we apply our decorator by saying

Foo f = new Foo();
f = new FooHelloDecorat or(f);
.
.
.
later we can say
Foo OriginalFoo = f.Unwrap(); //takes the decorator off
As for your question .. this is a general problem with inheritance ..
generics can help you to a great degree with this problem as you can make
the base generic then derive the base to a closed type.

Cheers,

Greg
"Doug" <dn******@dtgne t.comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
>>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.GetV alue();
}
}

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.GetV alue() + "\r\n" + _description;
}
}

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

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

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

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

}

I've tested this and called it like so:

ClassLibrary1. Value StoredProc = new ClassLibrary1.D eveloper(new
ClassLibrary1. Date(new ClassLibrary1.N ame(new
ClassLibrary1. Header())));
MessageBox.Sho w(StoredProc.Ge tValue());

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?


Jul 26 '06 #3
Hi Greg,

Are you sure I'm not using the decorator pattern in my example? I
mimicked the pattern from the book Foundations of Object-Oriented
Programming Using .NET 2.0 Patterns
by Christian Gross. In Chapter 5 it uses an example of making a
hamburger and describing how the decorator pattern can be applied to
stack the ingredients of the hamburger. I believe I followed that
pattern pretty closely when I wrote my sample code (although I could be
wrong).

Jul 26 '06 #4
Well my decorator pattern is how they list it in GoF which is considerred
the major reference.

There are some decorator patterns similar to what you have but they are
called "instance decorators" in that pattern you do not encapsulate the
subject but you still _always_ call the base. The intent of a decorator
pattern is to add functionality, not to change it.

What you are doing there is overriding and changing the behavior of the
original object this is basic inheritance.

I don't have a copy of the reference you mention but will try to find a
copy.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
"Doug" <dn******@dtgne t.comwrote in message
news:11******** **************@ p79g2000cwp.goo glegroups.com.. .
Hi Greg,

Are you sure I'm not using the decorator pattern in my example? I
mimicked the pattern from the book Foundations of Object-Oriented
Programming Using .NET 2.0 Patterns
by Christian Gross. In Chapter 5 it uses an example of making a
hamburger and describing how the decorator pattern can be applied to
stack the ingredients of the hamburger. I believe I followed that
pattern pretty closely when I wrote my sample code (although I could be
wrong).

Jul 26 '06 #5
http://research.umbc.edu/~tarr/dp/le...orator-2pp.pdf

is a nice little presentation on the decorator (in java but code should be
fairly readable and examples still apply).

Cheers,

Greg
"Greg Young" <dr************ *******@hotmail .comwrote in message
news:et******** ******@TK2MSFTN GP04.phx.gbl...
Well my decorator pattern is how they list it in GoF which is considerred
the major reference.

There are some decorator patterns similar to what you have but they are
called "instance decorators" in that pattern you do not encapsulate the
subject but you still _always_ call the base. The intent of a decorator
pattern is to add functionality, not to change it.

What you are doing there is overriding and changing the behavior of the
original object this is basic inheritance.

I don't have a copy of the reference you mention but will try to find a
copy.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
"Doug" <dn******@dtgne t.comwrote in message
news:11******** **************@ p79g2000cwp.goo glegroups.com.. .
>Hi Greg,

Are you sure I'm not using the decorator pattern in my example? I
mimicked the pattern from the book Foundations of Object-Oriented
Programming Using .NET 2.0 Patterns
by Christian Gross. In Chapter 5 it uses an example of making a
hamburger and describing how the decorator pattern can be applied to
stack the ingredients of the hamburger. I believe I followed that
pattern pretty closely when I wrote my sample code (although I could be
wrong).


Jul 26 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1533
by: Doug | last post by:
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 {
3
2191
by: Gregory | last post by:
I recently reviewed the decorator pattern in the GOF book and noticed a problem. Let look at the example given in the book. For simplicity I removed intermediate Decorator class. // Interface class class VisualComponent { public: VisualComponent();
0
6573
by: JosAH | last post by:
Greetings, last week we talked a bit about the Visitor design pattern. This week we'll talk a bit about additional functionality that is sometimes wanted, i.e. the functionality is optional. Assume there is a lot of optional functionality that people want. This article discusses the Decorator (or 'Wrapper') pattern. For the sake of the example we'll use array manipulation. People always fiddle diddle with arrays, i.e. they copy...
9
3459
by: Christian Hackl | last post by:
Hi! I've got a design question related to the combination of the NVI idiom (non-virtual interfaces, ) and popular object-oriented patterns such as Proxy or Decorator, i.e. those which have the basic idea of deriving from a base class and delegating to an object of it at the same time. My problem is that I cannot seem to combine those two techniques in a flawless way. For a very simple, non real life example (for which I shall omit...
9
1631
by: Tyno Gendo | last post by:
Hi I'm trying to learn patterns, which I hope to use in my PHP code, although I'm finding it hard to get any real impression of how patterns fit in properly, I've done the following test code for Decorator pattern and want to know: a) is it correct, this is decorator pattern? b) how would i use this in practice with a database, eg. how would i store the 'attributes' in tables, and how would the 'pattern' be used in
4
2472
by: thomas.karolski | last post by:
Hi, I would like to create a Decorator metaclass, which automatically turns a class which inherits from the "Decorator" type into a decorator. A decorator in this case, is simply a class which has all of its decorator implementation inside a decorator() method. Every other attribute access is being proxied to decorator().getParent(). Here's my attempt: -------------------------------------------------------
11
1719
by: George Sakkis | last post by:
I have a situation where one class can be customized with several orthogonal options. Currently this is implemented with (multiple) inheritance but this leads to combinatorial explosion of subclasses as more orthogonal features are added. Naturally, the decorator pattern comes to mind (not to be confused with the the Python meaning of the term "decorator"). However, there is a twist. In the standard decorator pattern, the decorator...
8
2889
by: Chris Forone | last post by:
hello group, is there a possibility to implement the decorator-pattern without new/delete (nor smartpt)? if not, how to ensure correct deletion of the objects? thanks & hand, chris
5
3645
by: proxyuser | last post by:
The context of this question is actually from the book "C# 3.0 Design Patterns" (Bishop). She makes the point that one of the reasons you'd use Decorator is if you can't change the original component class. On p. 17, she explains a code example: "...this code deviates from the pattern laid out...there is no IComponent interface. This is perfectly acceptable; the decorators can inherit directly from the component and maintain an...
0
9590
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9424
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10051
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.