© Vaskaran Sarcar 2020
V. SarcarDesign Patterns in C#https://doi.org/10.1007/978-1-4842-6062-3_11

11. Composite Pattern

Vaskaran Sarcar1 
(1)
Garia, Kolkata, West Bengal, India
 

This chapter covers the Composite pattern.

GoF Definition

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

Concept

Consider a shop that sells different kinds of dry fruit, such as cashews, dates, and walnuts. Each of these items has a certain price. Let’s assume that you can purchase any of these individual items, or you can purchase “gift packs” (or boxed items), which are composed of different dry fruit items. In this case, the cost of a packet is the sum of its component ...

Get Design Patterns in C#: A Hands-on Guide 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.