Welcome to the Design Patterns in TypeScript repository! 🎨 This collection showcases common design patterns in TypeScript, helping developers learn and build better, more scalable applications.
Design patterns are proven solutions to common software design problems. This repository covers a variety of patterns categorised into Creational, Structural, and Behavioural types, with real-world examples written in TypeScript.
Each pattern includes:
- A concise explanation.
- TypeScript implementation.
- Use cases and benefits.
- Factory Pattern: Encapsulate object creation logic for better scalability.
- Builder Pattern: Construct complex objects step-by-step.
- Singleton Pattern: Ensure a class has only one instance.
- Adapter Pattern: Bridge mismatched interfaces to ensure compatibility.
- Decorator Pattern: Dynamically add behavior to objects.
- Facade Pattern: Simplify complex subsystems with a unified interface.
- Observer Pattern: Establish a publisher-subscriber relationship between objects.
- Strategy Pattern: Define interchangeable algorithms for runtime flexibility.
- Node.js (v14 or higher)
- TypeScript (v4.0 or higher)
-
Clone the repository:
git clone https://github.com/NandhakumarE/design-patterns-ts.git
-
Navigate to the project folder:
cd design-patterns-ts -
Install dependencies:
npm install
Every design pattern folder includes a test runner file to execute its implementation. To run an example:
-
Use
npx ts-nodeto execute the test file from the src directory:npx ts-node src/<pattern-folder>/<PatternTestFile>.ts
Replace
<pattern-folder>with the specific pattern folder (e.g.,builder-pattern) and<PatternTestFile>with the test file name (e.g.,BuilderPatternTest.ts).Example:
npx ts-node src/builder-pattern/BuilderPatternTest.ts