TypeScript Composite Pattern Example

In this article, we will learn how to use and implement the Composite Pattern in TypeScript with an example.
Composite is a structural design pattern that lets you compose objects into tree structures and then work with these structures as if they were individual objects.

TypeScript Composite Pattern Example

The below diagram shows the generic structure of the Composite Pattern:
Let's refer to the above structure to create an example to demonstrates the usage of the Composite Pattern.

Let's create composite.ts file and add the following code to it:
export interface Component {
    operation(): void;

export class Composite implements Component {

    private list: Component[];
    private s: String;

    constructor(s: String) {
        this.list = [];
        this.s = s;

    public operation(): void {
        console.log("`operation of `", this.s)
        for (var i = 0; i < this.list.length; i += 1) {

    public add(c: Component): void {

    public remove(i: number): void {
        if (this.list.length <= i) {
            throw new Error("index out of bound!");
        this.list.splice(i, 1);

export class Leaf implements Component {
    private s: String;
    constructor(s: String) {
        this.s = s;
    public operation(): void {
        console.log("`operation` of Leaf", this.s, " is called.");


Let's create demo.ts file and add the following code to it:
import { Leaf, Composite } from "./composite";

export function show() : void {
 var leaf1 = new Leaf("1"),
  leaf2 = new Leaf("2"),
  leaf3 = new Leaf("3"),

 composite1 = new Composite("Comp1"),
 composite2 = new Composite("Comp2");





  • Compile the above code using the TypeScript compiler.
  • Above code is compiled to plan JavaScript code
  • Run Javascript code using node
design_patterns_in_typescript-master\composite> tsc --target ES5 .\demo.ts
design_patterns_in_typescript-master\composite> node .\demo.js
`operation of ` Comp1
`operation` of Leaf 1  is called.
`operation` of Leaf 2  is called.
`operation of ` Comp2
`operation` of Leaf 1  is called.
`operation` of Leaf 1  is called.
`operation` of Leaf 2  is called.
`operation of ` Comp2
`operation` of Leaf 1  is called.
`operation` of Leaf 3  is called.

When to Use Composite Pattern

  • Use the Composite pattern when you have to implement a tree-like object structure.
  • Use the pattern when you want the client code to treat both simple and complex elements uniformly.

All TypeScript Design Patterns

1. Creational Design Patterns

Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
  1. TypeScript Singleton Pattern Example
  2. TypeScript Factory Design Pattern with Example
  3. TypeScript Abstract Factory Pattern Example
  4. TypeScript Builder Pattern Example
  5. TypeScript Prototype Pattern Example

2. Structural Design Patterns

Structural patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient.
  1. TypeScript Bridge Pattern Example
  2. TypeScript Adapter Pattern Example
  3. TypeScript Decorator Pattern Example
  4. TypeScript Composite Pattern Example
  5. TypeScript Flyweight Design Pattern Example
  6. TypeScript Facade Pattern Example
  7. TypeScript Proxy Pattern Example

3. Behavioral Design Patterns

Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.
  1. TypeScript Command Pattern Example
  2. TypeScript Chain of Responsibility Pattern Example
  3. TypeScript Visitor Pattern Example
  4. TypeScript Template Method Pattern Example
  5. TypeScript Strategy Pattern Example
  6. TypeScript State Pattern Example
  7. TypeScript Observer Pattern Example
  8. TypeScript Memento Pattern Example
  9. TypeScript Mediator Pattern Example
  10. TypeScript Iterator Pattern Example
  11. TypeScript Interpreter Design Pattern Example