Skip to content

Alvarez-P/typescript-design-patterns

Repository files navigation

DESIGN PATTERNS IN

Overview 🤔

This project is a design patterns compilation with examples in Typescript.

Getting Started 🚀

To execute patterns must be:

  • Install dependencies:
pnpm i
  • Run command:
pnpm run dev

Tip

You can edit file ./src/index.ts to change patterns to execute using DesignPatternsManager class

  // Run all patterns
  await manager.runAll()

  // Run specific pattern
  await manager.run(manager.getPatterns().SINGLETON)

Definitions 📚

A design pattern is a general, reusable solution to a common problem in software design. It is a description of a set of interacting objects and their relationships, as well as the rules and guidelines for their use.

Characteristics

  • Reusability
  • Abstraction
  • Modularity
  • Flexibility

Classification

  • Creational
  • Structural
  • Behavioral

Creational Patterns 🏭

Restricts a class from instantiating multiple objects. It creates a single instance of a class and provides a global point of access to that instance.

Provides a way to create objects without specifying the exact class of object that will be created. It defines an interface for creating objects, and lets subclasses decide which class to instantiate.

Provides a way to create families of related objects without specifying their concrete classes. It defines an interface for creating objects, and lets subclasses decide which classes to instantiate and how to create them.

Separates the construction of complex objects from their representation. It allows you to construct objects step-by-step, and provides a way to create different representations of the same object.

Allows you to create new objects by copying existing objects, without making your code dependent on their classes. It provides a way to create objects that are initialized with values from another object.

Structural Patterns 🧩

Allows two incompatible objects to work together by converting the interface of one object into an interface expected by the other object.

Separates an object's abstraction from its implementation, allowing them to vary independently, favoring greater flexibility and extensibility.

Allows you to compose objects into a tree-like structure, where each node can be either a leaf node or a composite node. This pattern enables you to treat individual objects and compositions of objects uniformly, making it easier to work with complex structures.

Allows you to dynamically add or remove additional responsibilities from an object without affecting its external interface. This pattern enables you to extend the behavior of an object without modifying its underlying structure.

Provides a simplified interface to a complex system of classes, libraries, or frameworks. It hides the complexities of the system and provides a single interface to access the system's functionality.

Allows multiple objects to share the same state or behavior, reducing the amount of memory used and improving performance.

Acts as an intermediary between the client and the real object, adding additional functionality or controlling access to the original object.

Behavioral Patterns 🔁

Allows multiple objects to handle a request in a sequential manner. Each object in the chain has the opportunity to process the request or pass it to the next object in the chain.

Encapsulates a request as an object, allowing the request to be parameterized, queued, logged and reverted. The key idea behind this pattern is to provide the means to decouple client from receiver.

Allows you to traverse a collection of objects without exposing the underlying implementation of the collection. It provides a way to access the elements of a collection in a sequential manner, without having to know the details of the collection's internal structure.

Defines an object that encapsulates how a set of objects interact with each other. It acts as an intermediary between the objects, allowing them to communicate with each other without having a direct reference to one another.

Allows an object to capture its internal state and externalize it so that the object can be restored to its previous state later.

Provides a way for objects to be notified of changes to other objects without having a tight coupling between them.

Allows an object to change its behavior when its internal state changes. It provides a way for objects to be notified of changes to other objects without having a tight coupling between them.

Allows you to define a family of algorithms, encapsulate each one as a separate class, and make them interchangeable at runtime.

Defines a skeleton of an algorithm in a method, deferring some steps to subclasses. It lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

Allows you to add new operations to a class hierarchy without modifying the existing classes.

About

Design patterns in Typescript

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published