5

I have a problem whereby I have a class Item, which has a list of Subitems. When a child item has been changed or deleted, I need the parent Item to know about it.

I was thinking that the Observer pattern would come in handy here. But does it make sense for an Item class to both extend extend Observerable and implement Observer?

Cheers.

Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111
Nick
  • 1,269
  • 5
  • 31
  • 45
  • put the code with your question – Engineer Mar 04 '14 at 12:10
  • You can both observe whom is drinking beer and others can observe whether you drink beer or not. So, why not? – Smutje Mar 04 '14 at 12:20
  • Yes, yes it does make sense. – nablex Mar 04 '14 at 12:20
  • @Smutje this may get complicated if too many people are drinking beer and you have to toast with everyone. This kind of coupling becomes a really hard problem then. – SpaceTrucker Mar 04 '14 at 20:31
  • @SpaceTrucker Yes, that's what computers can do best - keeping track of boring and redundant tasks, while you can concentrate on that special lady, what the computer would surely f*** up :-) – Smutje Mar 04 '14 at 20:33

4 Answers4

2

Yes it makes sense sometimes to have an observer also be observed.

Ask yourself why you want to apply the pattern, however. Observing just to give updates might be more complicated to debug, compared to just updating directly, e.g., the child calls his parent when he updates.

Generally, Observables don't want to know the details about their Observers (decoupling, information hiding) so that you can make virtually any class an Observer. If that is what you need, then the pattern is good for you. If not, then adding this may result in needless complexity (harder to understand and debug the code).

Edit (I had this backwards): Do your child (Observable) items already know all the details about their parents (Observer)? If they do, then using Observer might be over-design. If children don't want to know the details of their parent, then Observer could be useful.

When making observers be observable, watch out for cycles https://stackoverflow.com/a/964820/1168342

Community
  • 1
  • 1
Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111
  • A tipical use (usually bad prattice) in Swing Components is to declare a Listener to itself. Making the Component implements ActionListener and make Component to listen itself. – Ezequiel Mar 04 '14 at 20:01
1

Your argumentation and situation make sense to use Observer Design pattern. There is very handfull article about Oberver design pattern. There is also very simple example in java so there is no sense to paste it here. Please look on it.

RMachnik
  • 3,598
  • 1
  • 34
  • 51
0

The simplest answer is when you find that you have events flowing in both directions, most likely it is time to introduce a Mediator. Sometimes these objects are coordinators. Most people consider the Controller in MVC a Mediator.

The alternative is to create a peer protocol, which is maybe less structured, and more elastic in that you can do things like discovery that are generally not part of Mediator as it knows the parties it's coordinating in advance.

So my answer would be just making every thing observe various aspects of other things in its ecosystem will lead to chaos pretty quickly unless some kind of communication protocol is in place.

Rob
  • 11,446
  • 7
  • 39
  • 57
0

If you need to add a subitem to an item or remove one from it, you have to call a method on that item instance. The methods could look like:

public void addToSubitems(Subitem subitem);
public void removeFromSubitems(Subitem subitem);

So the item stays responsible for managing its subitems. When returning a collection of subitems from the item, always return an unmodifiable collection. By making the Item class responsible for managing the items, it has the possibility to take aktions on addition, removal, etc.

If the item also needs to get notified about changes of its subitems, you can also consider making the subitems immutable and implementing a change as an atomic replace of an items subitem. Or the item will attach an observer to a subitem as soon as it is added to that item.

SpaceTrucker
  • 13,377
  • 6
  • 60
  • 99