视觉画布设计
canvas-design
基于设计原则创作静态视觉作品,用于海报与品牌图形。
design-patterns-gof【文档与办公】The 23 Gang of Four object-oriented design patterns (Gamma, Helm, Johnson, Vlissides, 1994) distilled as a practical field guide, not a catalog. Use when naming a shape in code review, choosing between competing designs, deciding whether to introduce indirection, or auditing for over-abstraction. Each pattern documents intent, tradeoffs, modern relev…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Field guide to the 23 patterns from Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, Helm, Johnson, Vlissides, 1994), updated for modern TypeScript/Python/Rust/Swift practice. Each pattern lives in its own reference file — this SKILL.md is the index and the meta-level (principles, categories, decision tree, criticism).
This skill is opinionated. In 2026, a lot of GoF ceremony dissolves into language features (closures, discriminated unions, generators, pattern matching, DI containers). The patterns that survived kept their names because the shape is useful; the class hierarchies often aren't.
Every pattern in the book is a recipe for one or both of these:
When a pattern feels wrong, it's usually because one of these principles was violated (e.g., Template Method forces inheritance; Singleton exposes global state and hides dependencies).
Every pattern documented here has:
| Pattern | One-line intent | Modern relevance |
|---|---|---|
| Abstract Factory | Produce families of related objects through one interface | Low-mid. DI containers subsume most uses. |
| Builder | Construct complex objects step-by-step; avoid telescoping constructors | High (fluent APIs everywhere). Rust-canonical. |
| Factory Method | Subclass (or function) decides which concrete class to instantiate | High. Array.of, Promise.resolve, URL.createObjectURL. |
| Prototype | Clone an existing instance instead of constructing from scratch | Low in app code; high in game engines / scene graphs. |
| Singleton | One instance, global access | Mostly anti-pattern. Use DI or module-level values. |
| Pattern | One-line intent | Modern relevance |
|---|---|---|
| Adapter | Translate one interface to another so incompatible objects can collaborate | Ubiquitous at system boundaries. |
| Bridge | Split abstraction and implementation into two hierarchies linked by composition | Moderate. JDBC, React renderers. |
| Composite | Treat individual objects and groups uniformly via a tree | Ubiquitous. DOM, ASTs, scene graphs. |
| Decorator | Stack behaviors onto an object at runtime | Ubiquitous (middleware under different names). |
| Facade | One narrow API over a complex subsystem | Very high. SDK clients, service classes. |
| Flyweight | Share fine-grained objects to save memory | Niche. Graphics, text rendering, ECS. |
| Proxy | Surrogate that controls access to another object | Enormous. ORMs, gRPC stubs, Vue/MobX reactivity. |
| Pattern | One-line intent | Modern relevance |
|---|---|---|
| Chain of Responsibility | Pass a request through a chain of handlers | Ubiquitous as middleware. |
| Command | Encapsulate a request as a first-class object | High (CQRS, Redux actions, job queues). |
| Interpreter | Represent a grammar and evaluate sentences in it | Low. Use parser combinators / ADTs. |
| Iterator | Traverse a collection without exposing its structure | Built into every modern language. |
| Mediator | Central hub coordinates peers so they don't talk directly | Moderate. Redux/Zustand stores, chat servers. |
| Memento | Capture and restore an object's state without breaking encapsulation | High (undo, time travel, transactions). |
| Observer | Publisher notifies many subscribers of state changes | Ubiquitous (EventEmitter, RxJS, signals). |
| State | Object's behavior changes with internal state via delegation | High. XState, typestate, UI state machines. |
| Strategy | Interchangeable algorithms behind one interface | Ubiquitous — usually just a function. |
| Template Method | Fixed algorithm skeleton; subclasses override steps | Mostly framework internals. Prefer Strategy. |
| Visitor | Add operations to an object structure without modifying it | Niche (compilers). Prefer pattern matching. |
Is the pain about...
├─ CONSTRUCTION? → Creational
│ • Too many constructor args / optional config? → Builder
│ • Need to swap families together? → Abstract Factory
│ • Subclass decides concrete type? → Factory Method
│ • Construction expensive / copy cheaper? → Prototype
│ • (Singleton: almost never — use DI.)
│
├─ SHAPE / WIRING? → Structural
│ • Incompatible interfaces? → Adapter
│ • Add responsibility without subclassing? → Decorator
│ • Tree / part-whole with uniform treatment? → Composite
│ • Simplify access to subsystem? → Facade
│ • Control access / lazy / remote? → Proxy
│ • Decouple two hierarchies (what vs. how)? → Bridge
│ • Share millions of fine-grained objects? → Flyweight
│
└─ BEHAVIOR / COMMS? → Behavioral
• Swap algorithm at runtime? → Strategy (usually just a function)
• Broadcast to many listeners? → Observer
• Encapsulate request (queue / undo / log)? → Command (+ Memento for undo)
• Step through a collection uniformly? → Iterator (usually built-in)
• Walk a heterogeneous tree? → Visitor (prefer pattern matching)
• Object acts differently per internal mode? → State
• Skeleton with customizable steps? → Template Method (prefer Strategy)
• Many-to-many chatter → one hub? → Mediator
• Try handlers until one succeeds? → Chain of Responsibility
• Capture/restore state? → Memento
• Evaluate a domain grammar? → Interpreter (prefer parser combinators)
Before committing to any leaf, run two checks:
for/generator.
Command = closure + data. Observer = pub/sub primitive. Visitor = match on union. If the
language answers, stop.Real systems stack 3–5 patterns. Key relationships from GoF's inter-pattern graph:
Signs you're over-abstracting:
AbstractSingletonProxyFactoryBean — the pattern chain is the identity.Signs you actually need it:
@Observable,
@resultBuilder, actors, property wrappers, Codable, AsyncSequence, @dynamicMemberLookup,
structured concurrency, copy-on-write). Answers "is this pattern still idiomatic in Swift?" per
pattern.some/any confusion) that led Apple to bring classes back via @Observable /
@Model. Pair with swift-idioms.md: that file answers "what's the Swift replacement?"; this
file answers "why does it work, where does it fail, when do I still use a class?"Three long-running critiques have become conventional wisdom; treat them as load-bearing:
Full treatment in references/criticism-and-modernity.md.
Still alive under their GoF names: Adapter, Facade, Proxy, Decorator, Observer, Composite, Iterator (as protocol), Strategy (as function), Builder, Factory Method, State, Command, Chain of Responsibility, Memento.
Mostly anti-patterns or heavily displaced:
eval are better.