Question

I am working on the design phase of a paper folding project. I have a few queries in this. Part of the problem statement says " A web can contain one or more ribbons in it. It can also contain a group of ribbons folded together inside it."

How do you model the association for this? I found two solutions

i) Maintain two associations, one between the web and ribbon and the other between the ribbon
and the Group class. This way, the web contains only ribbons and when you process the ribbons, you'll have to check whether it is part of a group. Note that the behaviour of the ribbon will change if it is part of a group

ii) Introduce a new class called Childelemnt and associate it to web. Both Ribbon and Group will share the interface of Childelement.

Which one do you think is better? If you have any other solution, please let me know.

Thanking you all in advance,

Pradeep

Was it helpful?

Solution

It makes sense for objects to share an interface if they share operations or properties. In your case, both ribbons and groups can be part of a web, so it makes sense for then to share an interface, which is your second option (which is sometimes called the composite pattern). I recommend calling that interface WebElement.

There are some added complexities, like the fact that a ribbon can be part of the web, a group, or (conceivably) unattached. You'll need logic to enforce those rules. Since ribbons can be members of webs and groups, then you will likely have to maintain two associations, as you mention in your first option, but the two are unrelated to each other, and are a consequence of your system having two types of container (web and group).

Lastly, to alter the behavior of a ribbon depending on whether it is part of a group or not, you might want to employ the strategy pattern.

Licensed under: CC-BY-SA with attribution
scroll top