/design-patterns

Repository containing examples and description of most popular design patterns

Primary LanguageC#

Design Patterns

This repository contains examples and descriptions of most popular design patterns. The purpose for creating it was to thoroughly learn about design patterns and TypeScript language.

Based on GoF book we can devide design patterns to three groups:

  • Creational - how to create an object,
  • Structural - how to realize relationships among objects,
  • Behavioral - how objects can communicate with other objects.

Before my own description and impleemntation it is worth to mention resources on which I based while learning about the aptterns:

Behavioral

Chain of resposibility

It's about decoupling logic responsible for handling some action, which has some distinct steps to few handlers which will be responsible for one specific operation in entire process of given action and then chain all handlers. Chained handlers create a pipeline.

ChainOfResponsibilityDiagram

Example code in TypeScript:

Base handler

export interface IRequestHandler
{
    nextHandler: IRequestHandler 
    handle(resuestType: string): void;
}

Implementation of handler:

export class FirstStepRequestHandler implements IRequestHandler
{
    nextHandler: IRequestHandler;    
    
    handle(requestType: string): void {
        if(requestType == 'FirstRequest')
        {
            console.log('Handle first request!');
        }
        else if(this.nextHandler != null)
        {
            this.nextHandler.handle(requestType);
        }
    }
}

Demonstration of use:

    demonstrateChainOfResponsibility()
    {
        let firstHandler = new FirstStepRequestHandler();
        let secondHandler = new SecondStepRequestHandler();
        let thirdhandler = new ThirdStepRequestHandler();

        firstHandler.nextHandler = secondHandler;
        secondHandler.nextHandler = thirdhandler;

        firstHandler.handle("ThirdReuqest");
    }

Command

It's about encapsulating request as an object.

Example

Order a dish in restaurant. Waiter(invoker) takes order from us and then talk to(command) cook(receiver) about new dish.

Invoker

export class Waiter{

    constructor(private _cook: Cook){}

    takeOrder(dishName: string): void
    {
        let order = new AsianCousineOrderCommand(this._cook, dishName);
        order.execute()
    }
}

Base command

export interface ICommand
{
    execute():void
}

Command

export class AsianCousineOrderCommand implements ICommand
{
    constructor(private _cook: Cook, private _dishName: string){}

    execute(): void {
        this._cook.cookDish(this._dishName);
    }
}

Receiver

export class Cook
{
    cookDish(dishType: string): void
    {
        console.log("Cooked " + dishType);
    }
}