TypeScript Decorator Pattern Example

In this article, we will learn how to use and implement the Decorator Pattern in TypeScript with an example.

Intent

  • Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
  • Client-specified embellishment of a core object by recursively wrapping it.
  • Wrapping a gift, putting it in a box, and wrapping the box.

TypeScript Decorator Pattern Example

The below diagram shows the generic structure of the Decorator Pattern:


Let's refer to the above structure to create an example to demonstrates the usage of the Decorator Pattern.
Let's create a decorator .ts file and add the following code to it:
export interface Component {
    operation(): void;
}

export class ConcreteComponent implements Component {
    private s: String;

    constructor(s: String) {
        this.s = s;
    }

    public operation(): void {
        console.log("`operation` of ConcreteComponent", this.s, " is being called!");
    }
}

export class Decorator implements Component {
    private component: Component;
    private id: Number;

    constructor(id: Number, component: Component) {
        this.id = id;
        this.component = component;
    }

    public get Id(): Number {
        return this.id;
    }

    public operation(): void {
        console.log("`operation` of Decorator", this.id, " is being called!");
        this.component.operation();
    }
}

export class ConcreteDecorator extends Decorator {
    constructor(id: Number, component: Component) {
        super(id, component);
    }

    public operation(): void {
        super.operation();
        console.log("`operation` of ConcreteDecorator", this.Id, " is being called!");
    }
}

Usage

Let's create demo.ts file and add the following code to it:
import { Decorator, ConcreteDecorator, ConcreteComponent } from "./decorator";

export function show() : void {
 var decorator1: Decorator = new ConcreteDecorator(1, new ConcreteComponent("Comp1"));

 decorator1.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\decorator> tsc --target ES5 .\demo.ts
design_patterns_in_typescript-master\decorator> node .\demo.js
`operation` of Decorator 1  is being called!
`operation` of ConcreteComponent Comp1  is being called!
`operation` of ConcreteDecorator 1  is being called!

When to Use Decorator Pattern

  • Use the Decorator pattern when you need to be able to assign extra behaviors to objects at runtime without breaking the code that uses these objects.
  •  Use the pattern when it’s awkward or not possible to extend an object’s behavior using inheritance.

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




Comments