Suppose I have the following class structure:
A forest can have any number of trees, but each tree can belong to only one forest. If the forest is deleted, the tree is deleted.
A tree must have at least one branch, but can have many more, however each branch can belong to only one tree. If the tree is deleted, the branch is deleted.
A branch can have any number of leaves, and each leaf can belong to only one branch. If the branch is deleted, the leaf is deleted.
I believe this is a ‘composition’ structure, where each child depends on its parent for existence.
How might I represent a structure where each branch can contain other branches, which can contain other branches, and so on and so forth, essentially like this:
Notice the infinite loop I’ve added to branch, suggesting that a branch can contain any number of ‘child’ branches, which can in turn contain any number of child branches etc etc, similar to how a real tree might function, where each branch can contain smaller branches, which can in turn contain smaller branches:
How might I therefore create a structure where:
Each branch class can contain an infinite (and unknown) number of ‘child’ branch classes
Each branch can have access to itself and its child branches only (i.e it cannot see parent data)
Each branch has the ability to make decisions for itself and its child branches only (i.e. the chain of power over subordinates increases as you move up the chain towards the parent)
In other words, a simple heirarchy structure, but with a flexible hierarchy depth.
It is this unknown number of parent/child generations which is causing me difficulty. If I knew that a ‘grandparent’ branch can only contain ‘parent’ branches, which in turn can only contain ‘child’ branches, then I could easily hardcode 3 classes to represent grandparent/parent/child branches.
However since the number of ‘generations’ is unknown, I can’t seem to get my head around how this might be represented in a UML Class Diagram, and later implemented.
How might I best approach this?