473,769 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2889
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(&t extView),border (&scroll)
{
window.SetConte nts(&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.atwro te:
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.atwro te:
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 deleteWhenFinis hed. 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 objektorientier ter Datenverarbeitu ng
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...@earth link.netwrote:
On Jul 10, 6:07 am, Chris Forone <4...@gmx.atwro te:
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 objektorientier ter Datenverarbeitu ng
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
1693
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 http://sourceforge.net/users/alexvn
11
4309
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. They are still online at http://www.fluffycat.com/java/patterns.html Since September 2003 I've mainly been using PHP, and now that PHP 5 is becoming more available I am going to try the same thing I did in Java with PHP.
3
4107
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 example used for creational patterns. Is this example a bit contrived? I would have thought a decorator pattern would make more sense instead of deriving mazes with different attributes.
5
1817
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();
5
1162
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 idea is that each state has different counties and different permutations of state and counties lead to different sets of crops. I have a total of 63 combinations of sets of crops available right now. Each of these combinations can consist of one...
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...
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
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...
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();...
0
5310
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...
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
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.