Skip to content

monty-src/design-patterns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

65 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

My Design Patterns Notes

Important

My personal notes and deep dive on the following Design Patterns

Clone & Install

git clone git@github.com:monty-src/design-patterns.git
cd design-patterns && npm i --verbose
npm test

Table of Contents

Creational Patterns

  • 🏭 Factory Method: is a design pattern that simplifies object creation by providing a centralized place to instantiate different types of objects, making your code more flexible and easier to manage.
  • 🏭 Abstract Factory: is a design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes.
  • 🏠 Builder: is a design pattern that separates the construction of a complex object from its representation, allowing the same construction process to create different representations.
  • πŸ”‚ Prototype: is a design pattern that allows you to create new objects by cloning existing ones, rather than instantiating new objects from scratch.
  • πŸ”’ Singleton: is a design pattern that ensures a class has only one instance and provides a global point of access to it.

Structural Patterns

File Structure

πŸ“¦ src/factory-method
 ┣ πŸ“œ factory-method.ts
 ┣ πŸ“œ README.md
πŸ“¦ test/factory-method
 ┣ πŸ“œ factoryMethod.test.ts
 ┣ πŸ“œ factoryNotification.test.ts

Test

npm run test:factory-method
classDiagram
  class iFactoryNotification {
    <<interface>>
    +send(message: string): void
  }

  class FactoryNotificationCreatorFactory {
    <<abstract>>
    +createNotification(): iFactoryNotification
    +notifyUser(message: string): void
  }

  class FactoryEmailNotification {
    +send(message: string): void
  }

  class FactoryEmailNotificationCreator {
    +createNotification(): iFactoryNotification
  }

  class FactorySMSNotification {
    +send(message: string): void
  }

  class FactorySMSNotificationCreator {
    +createNotification(): iFactoryNotification
  }

  iFactoryNotification <|-- FactoryEmailNotification
  iFactoryNotification <|-- FactorySMSNotification

  FactoryNotificationCreatorFactory <|-- FactoryEmailNotificationCreator
  FactoryNotificationCreatorFactory <|-- FactorySMSNotificationCreator

  FactoryEmailNotificationCreator --> FactoryEmailNotification : creates
  FactorySMSNotificationCreator --> FactorySMSNotification : creates
Loading

File Structure

πŸ“¦ src/abstract-factory
 ┣ πŸ“œ abstract-factory.ts
 ┣ πŸ“œ README.md
πŸ“¦ test/abstract-factory
 ┣ πŸ“œ abstractFactory.test.ts

Test

npm run test:abstract-factory
classDiagram
    class EmailNotification {
        <<abstract>>
        +send(message: string) void
    }

    class SMSNotification {
        <<abstract>>
        +send(message: string) void
    }

    class NotificationFactory {
        <<abstract>>
        +createEmailNotification() EmailNotification
        +createSMSNotification() SMSNotification
    }

    class ModernEmailNotification {
        +send(message: string) void
    }

    class ModernSMSNotification {
        +send(message: string) void
    }

    class ModernNotificationFactory {
        +createEmailNotification() EmailNotification
        +createSMSNotification() SMSNotification
    }

    class VintageEmailNotification {
        +send(message: string) void
    }

    class VintageSMSNotification {
        +send(message: string) void
    }

    class VintageNotificationFactory {
        +createEmailNotification() EmailNotification
        +createSMSNotification() SMSNotification
    }

    EmailNotification <|-- ModernEmailNotification
    EmailNotification <|-- VintageEmailNotification
    SMSNotification <|-- ModernSMSNotification
    SMSNotification <|-- VintageSMSNotification
    NotificationFactory <|-- ModernNotificationFactory
    NotificationFactory <|-- VintageNotificationFactory
    ModernNotificationFactory --> ModernEmailNotification
    ModernNotificationFactory --> ModernSMSNotification
    VintageNotificationFactory --> VintageEmailNotification
    VintageNotificationFactory --> VintageSMSNotification

Loading

File Structure

πŸ“¦ src/builder
 ┣ πŸ“œ builder.ts
 ┣ πŸ“œ README.md
πŸ“¦ test/builder
 ┣ πŸ“œ builder.test.ts

Test

npm run test:builder
classDiagram
    class iNotification {
        <<abstract>>
        +send(message: string): void
    }

    class EmailNotification {
        +send(message: string): void
    }

    class SMSNotification {
        +send(message: string): void
    }

    class NotificationBuilder {
        <<interface>>
        +setRecipient(recipient: string): this
        +setSubject(subject: string): this
        +setMessage(message: string): this
        +build(): iNotification
    }

    class EmailNotificationBuilder {
        -recipient: string
        -subject: string
        -message: string
        +setRecipient(recipient: string): this
        +setSubject(subject: string): this
        +setMessage(message: string): this
        +build(): iNotification
    }

    class SMSNotificationBuilder {
        -recipient: string
        -message: string
        +setRecipient(recipient: string): this
        +setSubject(subject: string): this // Throws Error
        +setMessage(message: string): this
        +build(): iNotification
    }

    iNotification <|-- EmailNotification
    iNotification <|-- SMSNotification
    NotificationBuilder <|.. EmailNotificationBuilder
    NotificationBuilder <|.. SMSNotificationBuilder

Loading

File Structure

πŸ“¦ src/prototype
 ┣ πŸ“œ prototype.ts
 ┣ πŸ“œ README.md
πŸ“¦ test/prototype
 ┣ πŸ“œ prototype.test.ts

Test

npm run test:prototype
classDiagram
  class iNotification {
    <<interface>>
    +clone() iNotification
    +send() void
  }

  class EmailNotification {
    +recipient: string
    +subject: string
    +body: string
    +clone() iNotification
    +send() void
  }

  class SMSNotification {
    +phoneNumber: string
    +message: string
    +clone() iNotification
    +send() void
  }

  class NotificationManager {
    -prototypes: Record<string, iNotification>
    +registerPrototype(type: string, prototype: iNotification) void
    +createNotification(type: string) iNotification
  }

  iNotification <|.. EmailNotification
  iNotification <|.. SMSNotification
  NotificationManager --> iNotification : manages

Loading

File Structure

πŸ“¦ src/singleton
 ┣ πŸ“œ singleton.ts
 ┣ πŸ“œ README.md
πŸ“¦ test/singleton
 ┣ πŸ“œ singleton.test.ts

Test

npm run test:singleton
classDiagram
  class INotification {
      <<interface>>
      +send() void
  }

  class NotificationService {
      -static instance: NotificationService
      -constructor()
      +static getInstance() NotificationService
      +sendNotification(notification: INotification) void
  }

  INotification <|.. MockNotification
  INotification <|.. EmailNotification
  INotification <|.. SMSNotification
  NotificationService --> INotification : Uses

Loading