Clients can work with individual either composite structure in similar manner. In such way calling avgSalary on parent organization wouldn’t be broken and a result would be the average salary over all the emloyees whether working on parent organization directly or through child organizations. All what is needed for that is just to implement getSalary method for such child organization which would return an average salary of its internal employees. To feel all the power of a pattern just imagine a situation when a parent organization can store not only employees but also a child organizations.
![simple patterns simple patterns](https://thumbs.dreamstime.com/z/light-gray-simple-seamless-pattern-29999047.jpg)
This is a simple representation of usage, when organization can store only typical employee (with name and getSalary method). An organization stores employees in an array and can count average salary of all employees. In this example we handle getting an average salary of employees for a concrete organization. For composite is also typical to perform some pre- and post-hook operations. If the recipient is a Composite, it usually forwards request to child components. If the recipient is a simple object (Leaf), then request is handled directly by this simple object. Composite - implements child-related operations in the Component interface and stores Leaf (primitive) objects.Ĭlients are using the Component interface to interact with objects in the composition set.
![simple patterns simple patterns](https://wallup.net/wp-content/uploads/2018/09/26/255251-abstract-patterns-backgrounds-simple.jpg)
Leaf - represents primitive objects in the composition.
![simple patterns simple patterns](https://diy-100ideas.com/wp-content/uploads/2015/01/hedgehog-free-pattern-cross-stitch-3.jpg)
#Simple patterns how to#
This article is created in continuation of easy patterns series description and presents a Composite pattern which solves the problem of how to use recursive composition among set of different objects so clients don’t have to make this distinction.