Child management
While the composite pattern tries to equalize different kinds of objects in a data structure, there is still a significant difference between compound and leaf nodes, namely that compound nodes own other objects. To use this functionality in the code, compound nodes must implement child management operations. In other words, they must implement operations for adding, removing, and accessing child objects. It is, however, not clear in which place the class hierarchy we should implement these methods.
We can approach this in two different ways. One possibility is to define these operations in the interface class (TBaseClass in our example). This is called design for uniformity and enables the clients to treat leaf and composite ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access