- 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.
- 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.
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.
- 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)
-
Clone or create repository:
- Create locally and commit these files, or use GitHub CLI:
- gh repo create / --public --source=. --remote=origin --push
- Create locally and commit these files, or use GitHub CLI:
-
Install dependencies:
- npm install
-
Build:
- npm run build
-
Run demo:
- npm start
- or in development: npm run dev
-
Run tests:
- npm test
- Patterns are intentionally left as stubs. Each stub exports
name
anddescription
metadata plus a placeholderexample()
function you can implement. - Tests validate that all pattern modules exist. Add more unit tests as you implement each pattern.
- Follow the folder naming convention:
src/patterns/<lowercase-name-with-hyphens>
. - Add pattern-specific README and tests inside each pattern folder as you implement.
- Add your preferred license (MIT recommended). This repo is a template.