-
Notifications
You must be signed in to change notification settings - Fork 0
Composite

Structural pattern
Used when` there is a need to uniform and simplify client's interactions with individual components as well as collection of components. At the UML diagram above we have an abstract class Component, which has two subclasses -
- Leaf - individual component.
- Composite - collection of other Components.
In this structure client does not care if he is interacting with Leaf or Composite classes, as long as they are hidden behind Component interface.
Advantages:
+ Simplifies client's architecture by unifying the way how he works with either composite or individual classes.
In this project we have example with the following model:
LightSource - interface, that represents the source of light. (Analog of Component class). Contains two methods :
- switchState() - Turns on/off the light
- setColor(color : RgbColor) - Sets given color to a light source.
LightSource is implemented by two classes:
-
Bulb - individual component (Analog of Leaf class)
-
IlluminationPlate - composite class, might contain other IlluminationPlates inside of it, as well as Bulbs.
If the structure is organized in the following way, client does not care if he's working with a individual bulb, or a whole illumination system, that contains hundreds of bulbs.