views:

64

answers:

3

Allright, this might be a strange question and maybe I am heading the wrong direction, but here is my problem:

I have a SearchForm object which holds a collection of Tag objects. When a Tag object is clicked by the user, it highlights itself and triggers an event. The SearchForm class listens to this event and submits itself. The Tag object has no relation to the SearchForm.

So far, so good.

Now some Tags must activate other Tags when clicked. Each Tag knows what other Tags it must activate. But in order to do so it must KNOW about the whole collection of Tags held by the searchform.

Would it be "bad" if a Tag instance (= collection item) has a reference to the complete list of all other Tags (= collection)?

Of course some something like this would be doable:

Tag is clicked -> SearchForm is notified -> checks if the clicked tag must activate some others -> SearchForm activates the needed Tags itself.

But this seems a bit akward, or not?

A: 

I think it's just an observer design pattern.

I don't know javascript but I guess it should be doable.

In any case avoid hard-coding your list.

cadrian
+4  A: 

It doesn't sound like there's anything wrong with that. If there's an item in a collection that needs to know about other items in the collection, it's perfectly reasonable for it to know about the collection as a whole as well.

Most GUI frameworks, and the DOM itself that presumably you're working with, have collection items that know about the collection they're in (in the DOM, on any element, you can call parentElement to get its parent).

In general, it's good if objects don't "know" about more than the minimum they need, in order to reduce coupling. But if an object does need to know about something, giving it a reference to that isn't unreasonable.

Brian Campbell
+1  A: 

It's not uncommon for an item in a collection to know its whereabouts... think linked lists or trees. If you don't want the tag to know the whole list you might add a reference to its related tags only.

Manrico Corazzi