decorator
装饰器模式 - 动态地给对象添加额外的职责。
函数签名
typescript
function decorate<T>(fn: T, decorators: Decorator<T>[]): T
function withLogging(logger): Decorator
function withTiming(logger): Decorator
function withCache(): Decorator
function withErrorHandling(handler): Decorator
function withRetry(maxRetries, delay, setTimeout): Decorator使用示例
typescript
import { decorate, withLogging, withCache } from 'zcw-shared/functions/patterns/decorator'
function add(a: number, b: number) {
return a + b
}
const enhanced = decorate(add, [
withLogging(console.log),
withCache()
])
enhanced(1, 2) // 记录日志并缓存结果装饰器模式 - 动态添加功能!