Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

Composite Pattern

This directory contains examples of the Composite design pattern implemented in TypeScript. The Composite Pattern is a structural design pattern that allows you to compose objects into tree structures to represent part-whole hierarchies. This pattern is especially useful when dealing with objects that can be grouped into larger, more complex structures, forming a unified interface for both individual objects and compositions. The goal is to allow for the client to treat both individual objects (simple) and complex objects the same, maybe even not knowing which they are. This helps make it easier to work with part-whole relationships and navigate through them. If at any point you would like to obtain and run these files refer to the root README.md found here.

Conceptual Example

The conceptual example can be found in conceptual.ts and ran from conceptual.js. The TypeScript files contains the code and explanations of what each element does in the design pattern.

Real-World Example

The real world example is a order system for a website in charge of packaging items and getting their price and a list of the order. The Componenet class is the base class where we define all common operations for simple and comlex objects. The key common operations are getPrice, to get the price of an order, and orderList which will list all elements in the order. The simple componenet is the Product class, this represents the actual products being sold. The Product holds it's price and name and returns then for getPrice and orderList, respectivly. The complex object is the Box class. The box has a list of childres which can be a Box or a Product and 'sums up' results for each of the key operations. The client code creates orders and then prints out the total price and the order contents.