Skip to content

lucas-negrello/typescript-design-patterns

Repository files navigation

Design Patterns - E-commerce Examples (Typescript)

Purpose

  • A teaching/reference repository that holds one module per design pattern (GoF) implemented as examples in the context of an e-commerce application.
  • Each pattern folder contains a lightweight TypeScript stub and metadata linking the pattern to a concrete e-commerce responsibility. Implement the pattern inside the stub when you want to create the example.

Why this topic

  • E‑commerce systems contain many interacting parts — orders, payment, cart, inventory, shipping, promotions, notifications — which make it possible to illustrate all creational, structural and behavioral patterns naturally.

Repository Layout

  • src/patterns/<pattern>/index.ts — stub for the pattern, exports metadata and a placeholder implementation entry.
  • src/patterns/index.ts — exports a map of all pattern modules (used by tests/example).
  • src/index.ts — small demo that prints pattern mapping (after build or with ts-node).
  • tests/patterns.test.ts — simple test asserting all pattern modules exist.

Desing Patterns Mapping (GoF) -> E-commerce example

  • Creational
    • Singleton: ConfigurationManager (global config, DB connection single instance)
    • Factory Method: PaymentFactory (create payment method handlers)
    • Abstract Factory: ShippingFactory (factories for domestic/international shipping)
    • Builder: OrderBuilder (construct complex Order objects)
    • Prototype: CartItem.clone (clone items with modifiers)
  • Structural
    • Adapter: PaymentGatewayAdapter (adapts external gateway API)
    • Bridge: Notification (abstraction) & NotificationSender (implementations)
    • Composite: ProductCategory / ProductBundle (tree of product items)
    • Decorator: PriceDecorator (add taxes, discounts)
    • Facade: CheckoutFacade (simplify checkout subsystem)
    • Flyweight: ProductImageFlyweight (share image data across products)
    • Proxy: InventoryProxy (lazy/remote inventory checks)
  • Behavioral
    • Chain of Responsibility: OrderProcessingPipeline (validation -> payment -> shipping)
    • Command: CartCommands (add/remove/undo)
    • Interpreter: PromoCodeInterpreter (interpret discount expressions)
    • Iterator: CatalogIterator (iterate over catalogs)
    • Mediator: CheckoutMediator (coordinates cart, payment, shipping)
    • Memento: OrderMemento (save/restore order state)
    • Observer: InventoryObserver / Notification subscribers
    • State: OrderState (Pending -> Paid -> Shipped -> Cancelled)
    • Strategy: PricingStrategy (different pricing algorithms)
    • Template Method: PaymentProcessingTemplate (common workflow with hooks)
    • Visitor: ReportingVisitor (traverse products/orders to build reports)

How to run locally

  1. Clone or create repository:

    • Create locally and commit these files, or use GitHub CLI:
      • gh repo create / --public --source=. --remote=origin --push
  2. Install dependencies:

    • npm install
  3. Build:

    • npm run build
  4. Run demo:

    • npm start
    • or in development: npm run dev
  5. Run tests:

    • npm test

Notes

  • Patterns are intentionally left as stubs. Each stub exports name and description metadata plus a placeholder example() function you can implement.
  • Tests validate that all pattern modules exist. Add more unit tests as you implement each pattern.

Contributing

  • Follow the folder naming convention: src/patterns/<lowercase-name-with-hyphens>.
  • Add pattern-specific README and tests inside each pattern folder as you implement.

License

  • Add your preferred license (MIT recommended). This repo is a template.

About

Repository for Design Pattern Testing and Exemplifying

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published