473,785 Members | 3,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Decorator pattern in C#

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 [original] pattern
laid out...there is no IComponent interface. This is perfectly
acceptable; the decorators can inherit directly from the component and
maintain an object of that class as well. ...However, this code does
realy on the original Component [declaring a particular method as
virtual.] If this is not the case, and we cannot go in and change the
Component class, an interface is necessary."

Well, if we can't change the Component class, then how can the
Component class implement a common interface? Conceptually, this is
the same thing as inheriting in this context. If you don't have
access to changing the class, you can't do it. What am I missing?
Aug 18 '08 #1
5 3645
proxyuser wrote:
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 [original] pattern
laid out...there is no IComponent interface. This is perfectly
acceptable; the decorators can inherit directly from the component and
maintain an object of that class as well. ...However, this code does
realy on the original Component [declaring a particular method as
virtual.] If this is not the case, and we cannot go in and change the
Component class, an interface is necessary."

Well, if we can't change the Component class, then how can the
Component class implement a common interface?
There seems to be some information missing from your question. What is
this common interface you speak of? What is the set to whose members it
is common?
Conceptually, this is
the same thing as inheriting in this context. If you don't have
access to changing the class, you can't do it. What am I missing?
This may help:

The decorator pattern is very roughly interface inheritance combined
with aggregated implementation. Since you can't change the underlying
class, you subclass its main interface (e.g. Component), store an
instance of the class as a private member (this is the aggregation bit),
and (1) delegate all operations that make sense to the wrapped instance,
and (2) reimplement all operations that you wanted to change, but
couldn't access in the wrapped class.

-- Barry

--
http://barrkel.blogspot.com/
Aug 18 '08 #2

"Barry Kelly" <ba***********@ gmail.comwrote in message
news:8b******** *************** *********@4ax.c om...
>Well, if we can't change the Component class, then how can the
Component class implement a common interface?

There seems to be some information missing from your question. What is
this common interface you speak of? What is the set to whose members it
is common?
It's whatever is the abstract class. In GoF (fourth printing, 1995), it's
VisualComponent on p. 176, or Component on p. 177.
The decorator pattern is very roughly interface inheritance combined
with aggregated implementation. Since you can't change the underlying
class, you subclass its main interface (e.g. Component)...
Yes, but this doesn't seem quite right. Component is not the preexisting
class. In the example on p. 176, the preexisting class is TextView, and
that is the class we want to decorate. So they add an abstract class above
it, called VisualComponent . This requires that the TextView class is
changed to inherit from VisualComponent . On p. 182 they say "TextView is a
VisualComponent ", which is what makes it interchangeable .

And yet on p. 177, they say to use Decorator when "a class definition maybe
be hidden or otherwise unavailable for subclassing." If TextView, for
example, were hidden or unavailable, then how can it be changed to inherit
from VisualComponent as the design requres?
Aug 18 '08 #3
proxyuser wrote:
"Barry Kelly" <ba***********@ gmail.comwrote in message
news:8b******** *************** *********@4ax.c om...
Well, if we can't change the Component class, then how can the
Component class implement a common interface?
There seems to be some information missing from your question. What is
this common interface you speak of? What is the set to whose members it
is common?

It's whatever is the abstract class. In GoF (fourth printing, 1995), it's
VisualComponent on p. 176, or Component on p. 177.
I don't have that book.
The decorator pattern is very roughly interface inheritance combined
with aggregated implementation. Since you can't change the underlying
class, you subclass its main interface (e.g. Component)...

Yes, but this doesn't seem quite right. Component is not the preexisting
class.
You see, when you mentioned "Component" in a .NET forum, I assumed you
meant System.Componen tModel.Componen t.

And I do not mean "the preexisting class" by Component; I meant the base
class, the one that defines the interface through which the class you
are trying to decorate interacts with the rest of the system.
In the example on p. 176, the preexisting class is TextView, and
that is the class we want to decorate. So they add an abstract class above
it, called VisualComponent . This requires that the TextView class is
changed to inherit from VisualComponent . On p. 182 they say "TextView is a
VisualComponent ", which is what makes it interchangeable .

And yet on p. 177, they say to use Decorator when "a class definition maybe
be hidden or otherwise unavailable for subclassing." If TextView, for
example, were hidden or unavailable, then how can it be changed to inherit
from VisualComponent as the design requres?
I suspect that you are misreading the authors' intentions; it is not
that TextView is *changed* to inherit from VisualComponent , but rather
that TextView, for the sake of the example, is *asserted* to inherit
from VisualComponent . It seems to me that it makes little sense to
magically introduce a VisualComponent if the rest of the whole UI system
doesn't know what to do with a VisualComponent .

This page here seems to conform to my expectations of what the authors
intend:

http://www.exciton.cs.rice.edu/JavaR...ires/pat4d.htm

In any case, to make it concrete, and bring the discussion to something
that everyone can follow, assume you have a TextView deriving directly
or indirectly from Control - i.e. System.Windows. Forms.Control.

Now, a typical example of a decorator is a GUI widget that adds
scrollbars. If you want to add scrollbars to this TextView (the one
whose existence I've asserted just above) using the decorator pattern
(i.e. pretending we aren't running on Windows and that Control doesn't
already encapsulate a window handle etc.), you would need to write a
separate class which descends from Control, and contains a TextView
instance. It would handle drawing the scrollbars and keeping track of
their state, and when it needed to draw its client area, it would hand
off the appropriate clipped section for the TextView to draw on; and
similarly for events, etc. - it would pass them on after checking if
they are only applicable to the scrollbars. It would also probably e.g.
transform mouse events as appropriate for the visible client area.

The whole point of the decorator pattern outlined above is that you
don't have to rely on TextView not being sealed, or on TextView having
source code you can modify. Because TextView is just an implementation
of Control, you can write your own Control - i.e. conform to the
interface - and only delegate to the inner instance when desired.

And because this instance is held internally at runtime, you can make
decisions about reimplementatio n versus delegation at runtime, as well
as composing multiple layers of decorators as needed. Basically, the
decorator pattern can be more flexible than direct inheritance, for the
same reason that aggregation is more flexible than inheritance.

-- Barry

--
http://barrkel.blogspot.com/
Aug 18 '08 #4
I have Bishop's book, which is supposedly a remake of the Gang of
Four's book on the same subject, but tailored to C#. One criticism of
Bishop on Amazon.com is that her code sometimes doesn't work and is
too conceptual (which would defeat the purpose of the book, since you
can just buy the Gang of Four's book if C# is not your target).

Perhaps this example is one of those "broken code" instances people
have complained about.

That said, I have both Bishop and the Go4's books.

RL
Aug 18 '08 #5
On Aug 18, 11:58*pm, "proxyuser" <proxyu...@nosp am.comwrote:
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 [original] pattern
laid out...there is no IComponent interface. *This is perfectly
acceptable; the decorators can inherit directly from the component and
maintain an object of that class as well. *...However, this code does
realy on the original Component [declaring a particular method as
virtual.] *If this is not the case, and we cannot go in and change the
Component class, an interface is necessary."

Well, if we can't change the Component class, then how can the
Component class implement a common interface? *Conceptually, this is
the same thing as inheriting in this context. *If you don't have
access to changing the class, you can't do it. *What am I missing?
Thanks I got to know about a good book

-Cnu
Aug 20 '08 #6

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

Similar topics

5
1818
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 {
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
2473
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
2890
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
0
9645
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
10325
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
10148
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...
0
9950
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...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
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();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.