473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

design q: decorator pattern

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
Jul 10 '08 #1
8 2856
Hello,

Chris Forone wrote:
is there a possibility to implement the decorator-pattern without
new/delete (nor smartpt)?
Why should new/delete be mandatory for the decorator pattern in C++?

Bernd Strieder

Jul 10 '08 #2
Bernd Strieder schrieb:
Hello,

Chris Forone wrote:
>is there a possibility to implement the decorator-pattern without
new/delete (nor smartpt)?

Why should new/delete be mandatory for the decorator pattern in C++?

Bernd Strieder
in gof-book there is an example:

window->SetContents(
new BorderDecorator(
new ScrollDecorator(textView)
)
);

Border/ScrollDecorator are dyn. allocated...
Jul 10 '08 #3
Michael DOUBEZ schrieb:
Chris Forone a écrit :
>Bernd Strieder schrieb:
>>>is there a possibility to implement the decorator-pattern without
new/delete (nor smartpt)?

Why should new/delete be mandatory for the decorator pattern in C++?
in gof-book there is an example:

window->SetContents(
new BorderDecorator(
new ScrollDecorator(textView)
)
);

Border/ScrollDecorator are dyn. allocated...

This is not necessary

class foo
{
TextView textView;
ScrollDecorator scroll;
BorderDecorator border;
WindowApp window;

public:
foo():scroll(&textView),border(&scroll)
{
window.SetContents(&border);
}
};
drawbacks:
- deco-objs in class foo, although i poss. dont need them
- ctor for every combination of deco-objs
Jul 10 '08 #4
Bernd Strieder schrieb:
Hello,

Chris Forone wrote:
>is there a possibility to implement the decorator-pattern without
new/delete (nor smartpt)?

Why should new/delete be mandatory for the decorator pattern in C++?

Bernd Strieder
possibly some design-ideas?

i have an abstract class visual, class module and class decorator derive
from visual. module can draw himself, some decorators apply color,
texture, normals to the module. i think the deco-patt. is a good
approach poss. with smartptrs to ensure obj deletion...

cheers, chris
Jul 10 '08 #5
Hello,

Chris Forone wrote:
Bernd Strieder schrieb:
>Chris Forone wrote:
>>is there a possibility to implement the decorator-pattern without
new/delete (nor smartpt)?

Why should new/delete be mandatory for the decorator pattern in C++?
possibly some design-ideas?
There is too much open to reach an answer. And it starts to become
off-topic.

My first idea would be inheriting module in decorator and overriding the
things I want to change. But why not applying that color, texture,
normals on the module instance manually. The decoration makes only
sense if it happens often, and the decorated object has its own reasons
to exist.

Since this is on GUI code, many GUI frameworks have some facilities for
object destruction of the kind destruct and deallocate the window and
everything within. Better you try first to adjust to the usual ways of
your framework before trying something new.

What I really don't like about the way you have applied the decorator
pattern is, that there are 3 instances of subclasses of visual
involved, where only one should be, one visual decorated twice. In GUI
frameworks a base class like visual often has lots of state, which
probably needs to be copied for every level of applying decorator. That
seems pretty odd.

If your framework has been designed with all that in mind, e.g. the
decorators really are only thin wrappers, then it might be possible to
establish some owner relation between the decorators/decorated objects
and make their root responsible for the others (probably the outermost
decorator) and attach the outermost decorator to its embedding object
in a way that destruction can be done. If smart pointers are used then
only c'tors of the classes are involved

Bernd Strieder

Jul 10 '08 #6
On Jul 10, 6:07 am, Chris Forone <4...@gmx.atwrote:
is there a possibility to implement the decorator-pattern without
new/delete (nor smartpt)?
Yes, as Michael Doubez has shown, but then the Decorator is sharing
its component with something else, which isn't optimum.
if not, how to ensure correct deletion of the objects?
By simply using a smart pointer, or putting deletes in the appropriate
places. Why would you want to avoid using new/delete or smart
pointers?

The point of the Decorator pattern is that the Decorator has sole use
of the concrete Component it contains so it can control what the
component receives from the world at large. To ensure this, the
Decorator must have ownership of the component it contains.
Jul 10 '08 #7
On Jul 10, 12:39 pm, Chris Forone <4...@gmx.atwrote:
Bernd Strieder schrieb:
Chris Forone wrote:
is there a possibility to implement the decorator-pattern without
new/delete (nor smartpt)?
Why should new/delete be mandatory for the decorator pattern in C++?
in gof-book there is an example:
window->SetContents(
new BorderDecorator(
new ScrollDecorator(textView)
)
);
Border/ScrollDecorator are dyn. allocated...
In this particular case, they are both dynamically allocated;
the window is the only object which has a pointer to the
BorderDecorator, and the BorderDecorator is the only object
which has a pointer to the ScrollDecorator. Presumably, in this
particular case, they have established a convention that the
relevant classes are responsible for the delete (or they are
using the Boehm collector, but given when the book was written,
I doubt it).

In this particular case. There's no reason why it would always
be the case. In other cases, the objects involved may have
automatic lifetime, or be managed elsewhere. In many of my
decorators, I've found it useful to add a second parameter, a
bool deleteWhenFinished. If you pass it true, the decorator
does delete the decorated object in the destructor, if you pass
it false (which is the default), it doesn't. Garbage collection
would help here most of the time, but at least in one case, the
decorated objects use limited resources and require
deterministic destruction, so even with garbage collection, I'd
keep the flag and the (optional) delete.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 11 '08 #8
On Jul 10, 2:51 pm, "Daniel T." <danie...@earthlink.netwrote:
On Jul 10, 6:07 am, Chris Forone <4...@gmx.atwrote:
is there a possibility to implement the decorator-pattern
without new/delete (nor smartpt)?
Yes, as Michael Doubez has shown, but then the Decorator is
sharing its component with something else, which isn't
optimum.
That wasn't the case in Michael's example. There was a higher
level component which set up the pattern, but that's always the
case.
if not, how to ensure correct deletion of the objects?
By simply using a smart pointer, or putting deletes in the
appropriate places. Why would you want to avoid using
new/delete or smart pointers?
Because it's not generally necessary? Because in some cases,
someone else is already managing the object lifetime? (One of
the frequent uses of the decorator pattern in my code is a
filtering streambuf. And if the decorated streambuf comes from
std::cin, I'd better not delete it.)
The point of the Decorator pattern is that the Decorator has
sole use of the concrete Component it contains so it can
control what the component receives from the world at large.
Sole use for a certain period of time, maybe (although even this
isn't really guaranteed---or even usual).
To ensure this, the Decorator must have ownership of the
component it contains.
I don't know where you get this from. There are cases where the
decorator must be the only object accessing the decorated, for
the period of time the decorator exists, but there probably the
exceptions.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 11 '08 #9

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

Similar topics

8
by: Alex Vinokur | last post by:
Any links to get started with Design Patterns, including samples? Thanks in advance. Alex Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html...
11
by: FluffyCat | last post by:
In Febraury - April of 2002 I put together in Java examples of all 23 of the classic "Gang Of Four" design patterns for my website. Partly I wanted to get a better understanding of those patterns....
3
by: yb | last post by:
Hi, I just started reading design patterns and looking at the Lexi example. I'm very new to this so please bear with me. I understand the Decorator pattern, but a bit confused by the Maze...
5
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...
1
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...
3
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...
5
by: Navodit | last post by:
Hi I have a very typical problem which I believe might be more easily solvable if it were designed better: I have 3 dropdowns: dropdown1 (State), dropdown2 (County), dropdown3 (crop). So the...
4
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...
11
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.