Паттерн: Компоновщик (Composite)
Фрагмент: Composite/Component/Leaf, операции Add, Remove [C++]
  1. Composite* aComposite = new Composite;
  2. Leaf* aLeaf = new Leaf;
  3.  
  4. Component * aComponent;
  5. Composite* test;
  6.  
  7. aComponent = aComposite;
  8. if (test = aComponent->GetComposite()) {
  9. test->Add(new Leaf);
  10. }
  11.  
  12. aComponent = aLeaf;
  13.  
  14. if (test = aComponent->GetComposite()) {
  15. test->Add(new Leaf); // не добавит лист
  16. }