An element of the collection knows about the collection in which it is contained - is this smell?

Well, that might be a weird question, and maybe I'm heading the wrong direction, but here is my problem:

I have a SearchForm object that contains a collection of Tag objects. When a tag object is clicked by a user, it selects itself and fires an event. The SearchForm class listens for this event and represents itself. The Tag object is not related to SearchForm.

So far so good.

Now some tags should activate other tags when clicked. Each tag knows which other tags it must activate. But for this, he must KNOW about the entire collection of tags stored in the search form.

Would it be “bad” if the tag instance (= collection element) has a link to a complete list of all other tags (= collection)?

Of course, 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 does this seem a little uncomfortable, or not?

+3
source share
3 answers

There doesn’t seem to be anything wrong with this. If the collection has an element that should know about other elements of the collection, then it is quite reasonable to know about this collection as a whole.

Most GUIs and the DOM itself, presumably you work with, have collection elements that know about the collection they are in (in the DOM, you can call on any element parentElementto get its parent).

, , "" , , , . -, , .

+4

, ... . , , .

+1

, .

javascript, , .

.

0

Source: https://habr.com/ru/post/1720895/


All Articles