© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022
V. SarcarJava Design Patternshttps://doi.org/10.1007/978-1-4842-7971-7_13

13. Composite Pattern

Vaskaran Sarcar1  
(1)
Garia, Kolkata, India
 

This chapter covers the Composite pattern.

GoF Definition

It composes objects into tree structures to represent part-whole hierarchies. The Composite pattern lets clients treat individual objects and compositions of objects uniformly.

Concept

Consider a shop that sells different kinds of dried fruit such as cashews, dates, and walnuts. Each item has a certain price. Let’s assume that you can purchase individual items or you can purchase gift packs (or boxed items) which are composed of different dried fruit items. In this ...

Get Java Design Patterns: A Hands-On Experience with Real-World Examples now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.