DEV Community

Discussion on: The Composite Pattern - Design Patterns Meet the Frontend

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
coly010 profile image
Colum Ferry • Edited

Maybe not blog and blog collections as such, but I have definitely seen situations where a Post has Text and Comments, but Comments also have Text and Comments.

Reddit for example.

Both Post and Comments would composite objects in this regard however.

The Text would need to be an object of its own, with no children to fit this pattern.

But the Text may have other data it holds such as likes, reactions etc.

If we ask a Post how many reactions it contains, it can ask it's Text object for its number of reactions, then ask it's comments for its number of reactions, which will iterate through each top level comment, ask it's Text for number of reaction, then it'll ask it's own children for their number of reactions, fulfilling the recursive tree structure of the pattern.