Skip to content

🔧 A collection of essential design pattern examples in JavaScript 🧰

License

Notifications You must be signed in to change notification settings

AllThingsSmitty/basic-design-patterns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 

Basic Design Patterns

A collection of essential design patterns for software development using practical examples, such Singleton, Factory Method, Observer, and more, to help developers write more efficient and maintainable code.

Contents

Quick Start Guide

Not sure which pattern to use? Here's a quick reference:

Problem Pattern Use When
Need exactly one instance Singleton App needs a single shared resource (logger, config, DB)
Creating many similar objects Factory Method You have multiple types but want to hide the "new" keyword
Building complex objects step-by-step Builder Objects have many optional parameters or complex setup
Multiple ways to do something Strategy Algorithm choice varies; avoid massive if/else chains
Adding features to objects Decorator Wrapping objects with additional behavior (Java: logging, caching)
Simplifying complex systems Facade Hide complexity of subsystems behind simple interface
Working with tree/hierarchy Composite Folders/files, UI components, organization trees
Existing code doesn't match Adapter Integrating legacy code or third-party libraries
Tracking changes to objects Observer Event listeners, pub/sub systems, state changes
Different behavior per state State Objects behave differently based on internal state

Learning Path (Recommended Order):

  1. Singleton - Simplest; one instance only
  2. Factory Method - Essential for object creation
  3. Observer - Critical for event-driven code
  4. Strategy - Powerful alternative to conditionals
  5. Decorator - Advanced object enhancement
  6. Builder - For complex object construction
  7. Others - Build on these fundamentals

Patterns

Creational

  1. Abstract Factory - Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
  2. Builder - Separates the construction of a complex object from its representation, allowing the same construction process to create different representations.
  3. Factory Method - Creates objects without specifying the exact class of object that will be created.
  4. Protoype - Creates new objects by copying an existing object, known as the prototype.
  5. Singleton - Ensures a class has only one instance and provides a global point of access to it.

Structural

  1. Adapter - Allows incompatible interfaces to work together by converting one interface into another.
  2. Bridge - Decouples an abstraction from its implementation so that the two can vary independently.
  3. Composite - Composes objects into tree structures to represent part-whole hierarchies, allowing clients to treat individual objects and compositions uniformly.
  4. Decorator - Adds behavior or responsibilities to an object dynamically without altering its structure.
  5. Facade - Provides a simplified interface to a larger body of code, making it easier to use.
  6. Flyweight - Reduces memory usage by sharing common parts of the state between multiple objects.
  7. Proxy - Provides a surrogate or placeholder for another object to control access to it.

Behavioral

  1. Chain of Responsibility - Passes a request along a chain of handlers until it is handled or reaches the end of the chain.
  2. Command - Encapsulates a request as an object, thereby allowing for parameterization and queuing of requests.
  3. Interpreter - Defines a representation for a language's grammar and provides an interpreter to process sentences in that language.
  4. Iterator - Provides a way to access elements of a collection sequentially without exposing its underlying representation.
  5. Mediator - Defines an object that encapsulates how a set of objects interact, promoting loose coupling.
  6. Memento - Captures and restores an object's internal state without violating encapsulation.
  7. Observer - Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
  8. State - Allows an object to alter its behavior when its internal state changes.
  9. Strategy - Defines a family of algorithms, encapsulates each one, and makes them interchangeable.
  10. Template Method - Defines the skeleton of an algorithm in a method, deferring some steps to subclasses.
  11. Visitor - Separates an algorithm from the objects it operates on by allowing new operations to be added without modifying the objects.