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? 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?
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?
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).
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). 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). This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
{
|
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();
|
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...
|
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...
|
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
| |
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:
-------------------------------------------------------
|
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...
|
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
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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();...
| |
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...
| |