Skip to content
OpenAgentSkill
Browsing is open · Catalog captured 2026-11-21 · Automatic updates and account services are paused.
Design & UI

implementation-functional-patterns

TypeScript's functional answers to the 22 Gang of Four classes — factory functions (Factory Method, Abstract Factory, Prototype, Memento), module-scope singletons, fluent immutable builders, wrapper functions (Adapter, Facade), native Proxy, WeakMap caches (Flyweight), discriminated unions with exhaustive match (State, Visitor, Composite), event emitters and signals (Mediator, Observer), pipelines and composition (CoR, Decorator), stream methods (Iterator), closures-as-commands, higher-order strategies, lambda placement. Use when reviewing TypeScript that has a class-shaped problem the GoF catalog solves with a hierarchy but where idiomatic TS reaches for a function, a tagged union, or a data structure. Each rule names the GoF pattern(s) it replaces and when the class form still wins. Trigger on "factory class", "singleton getInstance", "state machine class", "observer pattern", "AST visitor", "where do I put this lambda". Sibling to implementation-design-patterns.

pproenca · AI review on file

Installation

Read the source and dependencies before running a command. Copying does not install or execute anything.

npx skills add pproenca/dot-skills --skill implementation-functional-patterns

About this skill


name: implementation-functional-patterns description: TypeScript's functional answers to the 22 Gang of Four classes — factory functions (Factory Method, Abstract Factory, Prototype, Memento), module-scope singletons, fluent immutable builders, wrapper functions (Adapter, Facade), native Proxy, WeakMap caches (Flyweight), discriminated unions with exhaustive match (State, Visitor, Composite), event emitters and signals (Mediator, Observer), pipelines and composition (CoR, Decorator), stream methods (Iterator), closures-as-commands, higher-order strategies, lambda placement. Use when reviewing TypeScript that has a class-shaped problem the GoF catalog solves with a hierarchy but where idiomatic TS reaches for a function, a tagged union, or a data structure. Each rule names the GoF pattern(s) it replaces and when the class form still wins. Trigger on "factory class", "singleton getInstance", "state machine class", "observer pattern", "AST visitor", "where do I put this lambda". Sibling to implementation-design-patterns.

TypeScript Functional Patterns

Implementation reference for the functional shapes that supersede the Gang of Four catalog in idiomatic TypeScript. Sibling to implementation-design-patterns: read that one when the answer is a class, this one when the answer is a function, a tagged union, or a small data structure.

TypeScript has first-class functions, discriminated unions, structural typing, and zero ceremony around closures. That means the 22 GoF patterns — written in the catalog as class hierarchies because the source material targets Java/C# — collapse to far fewer functional shapes in real TS code. Several patterns share a functional answer: tagged unions cover State, Visitor, and Composite; factory functions cover Factory Method, Abstract Factory, Prototype, and Memento; event emitters cover Mediator and Observer; wrapper functions cover Adapter and Facade. This skill names those collapses, the placement rules they imply, and the performance trade-offs.

When to Apply

  • Refactoring a Factory class hierarchy, an AbstractFactory returning families of products, or a class with clone() / Memento save-restore
  • Refactoring a Singleton class with private constructor + static getInstance
  • Refactoring a mutable Builder class for a configuration object with many optional fields
  • Refactoring a Strategy / Template Method / Bridge class hierarchy where variation is a single method
  • Replacing a Chain of Responsibility class chain or Decorator wrapper-class stack
  • Replacing a custom Iterator class with stream methods, generators, or lazy iterator helpers
  • Replacing a Command class with a closure stored in a queue (when undo/serialization is not required)
  • Replacing an Adapter / Facade class that exists only to forward calls or hide subsystem orchestration
  • Replacing a Proxy class with native JS Proxy (for transparent interception) or an HOF wrapper (for selective wrapping)
  • Replacing a Flyweight factory class with a Map/WeakMap cache + factory function
  • Replacing a State / Visitor / Composite class hierarchy with a discriminated union + exhaustive match function
  • Replacing a Mediator / Observer class with an event emitter or reactive signal
  • Reviewing TSX where lambdas appear inline in JSX, hook deps, or memo'd child props — placement controls identity
  • Recognizing imperative for loops with mutated accumulators that would read more honestly as reduce, Object.groupBy, or flatMap

Rule Categories

#CategoryImpactRulesTheme
1Creational alternatives (create)HIGH3Factory functions, module-scope singletons, fluent immutable builders
2Higher-order functions (hof)HIGH1Pass a function instead of a class
3Pipelines & composition (pipe)HIGH2Compose small functions: pipe (data flow), compose (wrapper layering)
4Stream methods (stream)HIGH4map/filter/flatMap/reduce, lazy iteration, single-pass chains
5Wrappers (wrap)HIGH2Wrapper functions for Adapter/Facade; native Proxy or HOF for Proxy
6Caching & sharing (cache)HIGH1Map/WeakMap + factory function over Flyweight class
7Pattern matching (match)HIGH1Discriminated unions + exhaustive match for State/Visitor/Composite
8Signals & event emitters (signal)HIGH1Event emitter / signal for Mediator/Observer
9Placement & identity (place)HIGH1Where the lambda lives controls behavior
10Closures as data (closure)MEDIUM1Functions that carry their state

17 rules across 10 categories, covering all 22 GoF patterns (some patterns share a functional answer — see the GoF → Functional Map below).

GoF → Functional Map

The full mapping from each Gang of Four pattern to its functional answer in idiomatic TS. Read this table to find the rule for a specific pattern; read the categories above to find rules by functional technique.

GoF GroupGoF PatternFunctional EquivalentRule
CreationalFactory MethodFunction returning tagged objectcreate-factory-function-over-factory-classes
Abstract FactoryFunction returning record of constructorscovered by factory-function rule
BuilderObject literal + Partial<T>, or fluent immutablecreate-fluent-immutable-builder
PrototypestructuredClone / spread / Immer producecovered by factory-function rule
SingletonModule-scope const, lazy ??= memocreate-module-scope-over-singleton
StructuralAdapterWrapper function translatin
Read the complete source ↗