CS FundamentalsHardGATE CS · TCS NQT Prime · Product Company SDE

A file-system explorer needs to treat individual files and whole folders (which may themselves contain files and folders) uniformly through the same interface, so client code can recurse without checking types. Which pattern fits?

  1. A. Decorator pattern
  2. B. Composite pattern
  3. C. Flyweight pattern
  4. D. Bridge pattern

Correct answer: B. Composite pattern

Composite lets individual objects (leaves) and compositions of objects (composites) be treated through a single common interface, which is exactly what lets a file-tree be walked uniformly regardless of whether a node is a file or a folder.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic