Important
My personal notes and deep dive on the following Design Patterns
git clone git@github.com:monty-src/design-patterns.git
cd design-patterns && npm i --verbose
npm test
- π 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.
Note
π¦ src/factory-method
β£ π factory-method.ts
β£ π README.md
π¦ test/factory-method
β£ π factoryMethod.test.ts
β£ π factoryNotification.test.ts
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
π¦ src/abstract-factory
β£ π abstract-factory.ts
β£ π README.md
π¦ test/abstract-factory
β£ π abstractFactory.test.ts
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
π¦ src/builder
β£ π builder.ts
β£ π README.md
π¦ test/builder
β£ π builder.test.ts
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
π¦ src/prototype
β£ π prototype.ts
β£ π README.md
π¦ test/prototype
β£ π prototype.test.ts
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
π¦ src/singleton
β£ π singleton.ts
β£ π README.md
π¦ test/singleton
β£ π singleton.test.ts
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