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:
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) {
this.list[i].operation();
}
}
public add(c: Component): void {
this.list.push(c);
}
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.");
}
}
Usage
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");
composite1.add(leaf1);
composite1.add(leaf2);
composite1.add(leaf3);
composite1.remove(2);
composite2.add(leaf1);
composite2.add(leaf3);
composite1.operation();
composite2.operation();
}
show();
Run:
- 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.
- TypeScript Singleton Pattern Example
- TypeScript Factory Design Pattern with Example
- TypeScript Abstract Factory Pattern Example
- TypeScript Builder Pattern Example
- 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.
- TypeScript Bridge Pattern Example
- TypeScript Adapter Pattern Example
- TypeScript Decorator Pattern Example
- TypeScript Composite Pattern Example
- TypeScript Flyweight Design Pattern Example
- TypeScript Facade Pattern Example
- TypeScript Proxy Pattern Example
3. Behavioral Design Patterns
Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.
- TypeScript Command Pattern Example
- TypeScript Chain of Responsibility Pattern Example
- TypeScript Visitor Pattern Example
- TypeScript Template Method Pattern Example
- TypeScript Strategy Pattern Example
- TypeScript State Pattern Example
- TypeScript Observer Pattern Example
- TypeScript Memento Pattern Example
- TypeScript Mediator Pattern Example
- TypeScript Iterator Pattern Example
- TypeScript Interpreter Design Pattern Example