What pattern am I looking for?

R

Rune Allnor

Hi all.

I have a collection (e.g. std::list or std::set) of items.
During the execution of some function in the item class
the conclusion is reached that "this item has done its job,
and should be removed from the collection". [ The case in
point is a GUI application, where the item is a corner point
in some polygon, that recieves some 'delete' event. ]

There has to be some standard pattern for handling these
kinds of things.

- Which pattern would this be?
- Where can I find descriptions?

Rune
 
M

Michael Doubez

Hi all.

I have a collection (e.g. std::list or std::set) of items.
During the execution of some function in the item class
the conclusion is reached that "this item has done its job,
and should be removed from the collection". [ The case in
point is a GUI application, where the item is a corner point
in some polygon, that recieves some 'delete' event. ]

If it is point in a polygon, it could know the points before and after
itself and adjust them to remove itself from the list (before doing
something dramatic such as delete this).

Doing such a thing with a standard container depends on the
container's implementation. Rolling your own might be a solution.

From a purely design point of view, whether it is the object's
responsibility to remove itself from the collection is IMHO an open
discussion. That is supposing iit is the only one to know it should be
removed.
There has to be some standard pattern for handling these
kinds of things.

- Which pattern would this be?

AKAIS some form of observer pattern or a mark and sweep system.

The most common I believe is the observer pattern implemented with
something like Boost.Signals (or QT slot/signal).
 
R

Rune Allnor

I have a collection (e.g. std::list or std::set) of items.
During the execution of some function in the item class
the conclusion is reached that "this item has done its job,
and should be removed from the collection". [ The case in
point is a GUI application, where the item is a corner point
in some polygon, that recieves some 'delete' event. ]

If it is point in a polygon, it could know the points before and after
itself and adjust them to remove itself from the list (before doing
something dramatic such as delete this).

That's part of the plan. Some sort of edge list.
Doing such a thing with a standard container depends on the
container's implementation. Rolling your own might be a solution.

That's kind of what I am doing. I'm using Qt, but need to know
the various 'standard' philosophies.
From a purely design point of view, whether it is the object's
responsibility to remove itself from the collection is IMHO an open
discussion. That is supposing iit is the only one to know it should be
removed.

The way I am handling this at the moment, is through Qt's
MouseEvent mechanism, as opposed to a call stack. That is,
the point itself is the first item to know that a 'delete'
event has been recieved, and it is the item's responibility
to communicate this fact back to the container of which it
is an item.

I suppose it is is regarded OT to this to ask how the
question how to best resolve the question in the Qt world,
so I don't...
AKAIS some form of observer pattern or a mark and sweep system.

The most common I believe is the observer pattern implemented with
something like Boost.Signals (or QT slot/signal).

Thanks. I'll have to think about how to incorporate this
kind of idea into the present set-up.

Rune
 
D

DeMarcus

Rune said:
Hi all.

I have a collection (e.g. std::list or std::set) of items.
During the execution of some function in the item class
the conclusion is reached that "this item has done its job,
and should be removed from the collection". [ The case in
point is a GUI application, where the item is a corner point
in some polygon, that recieves some 'delete' event. ]

There has to be some standard pattern for handling these
kinds of things.

- Which pattern would this be?
- Where can I find descriptions?

Rune

You want the collection to know about items, but you don't want items to
know about the collection.

You may want to have a look at the Observer pattern.
http://en.wikipedia.org/wiki/Observer_pattern

Here your item would be the subject, and the collection the observer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top