๐ Design Pattern
โ๏ธ ๐ด๐๐๐๐๐๐๐๐๐
Compose objects into tree structures to represent part or whole of hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
๐ ๐น๐๐๐-๐พ๐๐๐๐
๐บ๐๐๐๐๐๐๐
Imagine you're building a graphical editor. Users can create basic shapes like Text, Circle, etc., and combine them into complex images.
A basic solution might involve one class for simple objects and another for containers. But this forces clients to handle each differently, increasing complexity.
๐ฏ ๐๐จ๐ฅ๐ฎ๐ญ๐ข๐จ๐ง โ ๐๐จ๐ฆ๐ฉ๐จ๐ฌ๐ข๐ญ๐ ๐๐๐ญ๐ญ๐๐ซ๐ง
Composite pattern helps us to recursive composition so that clients are not required to make this distinction. This pattern has an abstract class for both primitives and their containers.
For instance, in the graphics system this abstract named Graphic which declares operation like Draw for graphical objects. It also declares operations that all composite objects share, such as Get Child, Add, Remove.
The Picture class defines a list of graphic objects and implements draw to call draw on its children. In addition, the picture implements child-related operations. Since the picture interface inherits from the Graphic interface, picture objects can compose other pictures recursively.
๐ง ๐๐ก๐๐ง ๐ญ๐จ ๐๐ฌ๐
โ๏ธ When individual objects and object groups should be handled identically.
โ๏ธ When tree structures or recursive hierarchies are involved.
๐ ๐น๐๐๐๐๐๐
๐ท๐๐๐๐๐๐๐
๐ Chain of Responsibility often uses parent references.
๐จ Decorator works well with Composite to extend behavior.
๐งฌ Flyweight can reduce memory usage but removes parent references.
๐ Iterator helps traverse composite structures.
๐งญ Visitor centralizes operations across Composite and Leaf classes.
๐ ๐ช๐๐
๐ ๐ฌ๐๐๐๐๐๐
๐ GitHub - https://lnkd.in/dy9h_VuA
๐ฌ ๐๐จ๐ฎ๐ซ ๐๐ฎ๐ซ๐ง!
โ๐๐๐ฏ๐ ๐ฒ๐จ๐ฎ ๐๐ฏ๐๐ซ ๐ฎ๐ฌ๐๐ ๐ญ๐ก๐ ๐๐จ๐ฆ๐ฉ๐จ๐ฌ๐ข๐ญ๐ ๐๐๐ญ๐ญ๐๐ซ๐ง ๐ข๐ง ๐ฒ๐จ๐ฎ๐ซ ๐ฉ๐ซ๐จ๐ฃ๐๐๐ญ๐ฌ?
hashtag#DesignPatterns hashtag#SoftwareEngineering hashtag#DevTips hashtag#CompositePattern
Top comments (0)