SteelReed

Search

Search posts, questions…

OOP & DesignQuestion 22 / 42

Middle

What is the Composite pattern, how is it used for tree structures, and what are its traps?

Short answer

Composite lets you treat a single item and a group the same way: both implement the same interface, and a group calls each child. It fits anything shaped like a tree.

Category: ElectronicsCategory: PhonesProduct: HeadphonesProduct: iPhoneProduct: Galaxy

3 points to say:

  1. Leaves and groups implement one interface.
  2. Callers need no instanceof.
  3. Watch out for very deep trees and cycles.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.