返回 Skill 列表
extension
分类: 开发与工程无需 API Key

js-design-patterns

应用经典的JavaScript设计模式(单例、观察者、工厂、代理、原型、模块、混入、中介者、享元)来解决纯JS和Node.js中的结构和行为问题。每当用户按名称询问设计模式、如何构建对象或类、需要在没有继承的情况下共享行为、想要解耦组件、管理单一共享资源、拦截对象访问或构建事件/发布-订阅系统时,使用此技能。当用户描述一个已知模式可以解决的问题时也触发该技能——即使他们没有提到模式的名称——例如“我如何确保只有一个实例存在?”、“当我应用程序中的某些内容发生变化时,我如何通知多个部分?”或“我如何在不知道其类型的情况下创建对象?”。基于patterns.dev。

person作者: jakexiaohubgithub

JavaScript Design Patterns Skill

Classic GoF-inspired design patterns implemented in modern JavaScript (ES2017+). Based on patterns.dev/vanilla.


Quick Pattern Selector

| Problem | Pattern | | ------------------------------------------------------- | ------------- | | Need exactly one shared instance (DB, config, logger) | Singleton | | Intercept, validate, or log object property access | Proxy | | Share methods across many instances without duplication | Prototype | | Notify multiple parts of the app when state changes | Observer | | Encapsulate private state; expose a clean public API | Module | | Add reusable behavior to classes without inheritance | Mixin | | Decouple components via a central communication hub | Mediator | | Reuse instances to reduce memory (thousands of objects) | Flyweight | | Create objects without specifying their exact type | Factory |


Response Format

For every pattern question, provide:

  1. What it is — one sentence
  2. When to use / when NOT to use — concrete conditions
  3. Code example — minimal, modern JS (ES6+), runnable
  4. Trade-offs — what you gain and what you give up

Pattern Summaries

Singleton

One instance, globally accessible. Use for shared resources. Avoid when you need testability or multiple independent instances.

Proxy (ES6 Proxy)

Wrap an object to intercept get/set/apply traps. Use for validation, logging, caching, reactive systems. Has runtime overhead — avoid on hot paths.

Prototype

Add shared methods to .prototype rather than each instance. Memory-efficient. Mutating the prototype affects all instances.

Observer / EventEmitter

Subscribers register for events; publisher emits without knowing who's listening. Great for decoupling. Risk: memory leaks if listeners aren't removed.

Module (ES Modules / IIFE)

Expose only a public API; keep internals private. Prefer ES import/export for tree-shaking. Use IIFE only in non-bundled environments.

Mixin

Copy methods from plain objects onto a class prototype with Object.assign. Enables multiple-behavior composition. Risk: name collisions, hard to trace origins.

Mediator / Middleware

Components communicate through a central hub, not directly. Pipeline variant (Express-style) chains async handlers. Decouples senders from receivers.

Flyweight

Share a common "intrinsic state" object across many instances; each instance only stores unique "extrinsic state". Best for large numbers of similar objects.

Factory

A function or class that creates and returns objects based on input, hiding instantiation details. Useful for polymorphism and plugin architectures.


Reference File

For full examples with runnable code and detailed trade-offs, read: → references/design-patterns.md