Design Patterns – Advantages of Composite Design Pattern

design-patterns

Apparently, a composite design pattern represents individual entities and collective entities in the same manner.

What is the advantage to this? For instance, if I subclass a class, why would I want to treat objects of both classes in the same way?

For instance, if 'tuna' is a subclass of 'fish', why would I want to have the same implementations for methods of objects in both classes?

Best Answer

The relationship being described in composite design patterns isn't a subclass relationship, it's a collection relationship. In other words, it's about treating a tuna the same as a school of tuna. That way for operations like translating, rotating, scaling, drawing, etc. the caller doesn't need to care if it is acting on one object or an entire collection.

Related Topic