算法艺术创作
algorithmic-art
通过代码和可调参数创作生成艺术与交互式视觉效果。
design-patterns【设计与创意】Use this skill when selecting or reviewing any design pattern — GoF creational/structural/behavioral, enterprise, integration, concurrency, architectural, or anti-pattern avoidance. This skill enforces: pattern selection decision framework, trade-off analysis, anti-pattern rejection, explicit elimination criteria for rejected candidates, and pattern …
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Guide pattern selection across all categories — GoF, enterprise, integration, concurrency, architectural — with decision trees, applicability rules, trade-off analysis, and composition guidance.
User request includes: design pattern, gof, gang of four, pattern catalog, creational, structural, behavioral, pattern selection, enterprise pattern, integration pattern, concurrency pattern, architectural pattern, anti-pattern, pattern composition, CQRS, event sourcing, saga, repository, unit of work, messaging pattern, circuit breaker, strangler fig, saga pattern, hexagonal architecture, clean architecture.
Selected pattern(s) with rationale, rejected candidates with explicit elimination reasons, implementation sketch, trade-off table, and composition rules if multiple patterns are needed.
No preamble. No postamble. No explanations. Strip articles a/an/the where unambiguous. Compress output. If no pattern fits, output No matching pattern. Consider: [alternative approach]. and stop.
What kind of problem are you solving?
├── Object creation, instance management → Creational (Singleton, Factory, Builder, Prototype)
├── Class/object composition, interfaces → Structural (Adapter, Decorator, Proxy, Facade)
├── Object communication, algorithms → Behavioral (Observer, Strategy, Command, State)
├── Business logic organization, persistence → Enterprise (Repository, Unit of Work, Aggregate)
├── Service-to-service communication → Integration (Message Queue, Event Bus, API Gateway)
├── Thread safety, async coordination → Concurrency (Mutex, Semaphore, Actor, Fan-out/Fan-in)
├── High-level system structure → Architectural (Clean Architecture, Hexagonal, CQRS, Event Sourcing)
└── Code smells affecting maintainability → Anti-Pattern to refactor (God Class, Spaghetti Code)
What is changing?
├── How objects are created → Creational
│ ├── Need to ensure single instance → Singleton
│ ├── Need to create families of related objects → Abstract Factory
│ ├── Need to construct complex objects step by step → Builder
│ ├── Need to create copies without knowing concrete types → Prototype
│ └── Need to delegate creation to subclasses → Factory Method
├── How objects are composed → Structural
│ ├── Need to make incompatible interfaces compatible → Adapter
│ ├── Need to add responsibilities dynamically → Decorator
│ ├── Need to control access to an object → Proxy
│ ├── Need to simplify complex subsystem → Facade
│ ├── Need to compose objects into tree structures → Composite
│ └── Need to share fine-grained objects → Flyweight (only if profiler confirms memory pressure)
└── How objects communicate and distribute work → Behavioral
├── Need to notify multiple objects of state changes → Observer
├── Need to select algorithm at runtime → Strategy
├── Need to encapsulate a request as an object → Command
├── Need to define skeleton of an algorithm → Template Method
├── Need to traverse a collection without exposing internals → Iterator
├── Need to represent a grammar → Interpreter
├── Need to capture and restore object state → Memento
├── Need to dispatch operations based on type → Visitor
└── Need to manage state-dependent behavior → State
What enterprise problem are you solving?
├── Data access abstraction → Repository
├── Transaction coordination → Unit of Work
├── Business logic as transaction script → Transaction Script
├── Business logic as domain model → Domain Model
├── Object identity and caching → Identity Map
├── Lazy loading for related objects → Lazy Load
├── Mapping objects to database → Data Mapper
└── Separating read/write models → CQRS
How do services communicate?
├── One-to-one, request-reply → RPC/HTTP + Circuit Breaker
├── One-to-many, fire-and-forget → Message Queue (Event Bus)
├── Guaranteed delivery, durability → Durable Message Queue
├── Bidirectional, low latency → WebSocket
├── Asynchronous, eventual consistency → Event-Driven + Transactional Outbox
├── Coordinating multiple services → Saga (Choreography or Orchestration)
├── Gradually migrating a monolith → Strangler Fig
└── Protecting downstream services → Circuit Breaker + Retry + Bulkhead
What concurrency problem?
├── Protect shared resource → Mutex / Read-Write Lock
├── Coordinate multiple workers → Semaphore / Barrier
├── Pipeline processing → Pipeline / Fan-out/Fan-in
├── Thread-safe state management → Actor Model
└── Parallel independent tasks → Fork-Join / Parallel Streams
| Problem | Category | Reference |
|---|---|---|
| Object creation, instance management | Creational | gof-patterns.md |
| Class/object composition, interfaces | Structural | gof-patterns.md |
| Object communication, algorithms | Behavioral | gof-patterns.md |
| Business logic organization, persistence | Enterprise | enterprise-patterns.md |
| Service-to-service communication | Integration | integration-patterns.md |
| Thread safety, async coordination | Concurrency | concurrency-patterns.md |
| High-level system structure | Architectural | enterprise-patterns.md |
| Code smells needing refactoring | Anti-patterns | anti-patterns.md |
For each candidate pattern, evaluate:
interface PaymentGateway {
charge(amount: Money): Promise<PaymentResult>;
}
class StripeGateway implements PaymentGateway { ... }
class PayPalGateway implements PaymentGateway { ... }
abstract class PaymentGatewayFactory {
abstract createGateway(): PaymentGateway;
async processPayment(amount: Money): Promise<PaymentResult> {
const gateway = this.createGateway();
return gateway.charge(amount);
}
}
class StripeFactory extends PaymentGatewayFactory {
createGateway(): PaymentGateway {
return new StripeGateway(process.env.STRIPE_KEY);
}
}
from abc import ABC, abstractmethod
class ShippingStrategy(ABC):
@abstractmethod
def calculate(self, weight: float, distance: float) -> float: ...
class FlatRateShipping(ShippingStrategy):
def calculate(self, weight: float, distance: float) -> float:
return 5.99
class WeightBasedShipping(ShippingStrategy):
def calculate(self, weight: float, distance: float) -> float:
return weight * 0.5 + distance * 0.01
class Order:
def __init__(self, shipping_strategy: ShippingStrategy):
self._shipping = shipping_strategy
@property
def shipping_cost(self) -> float:
return self._shipping.calculate(self.weight, self.distance)
interface Observer<T> {
update(event: T): Promise<void>;
}
class EventEmitter<T> {
private observers: Set<Observer<T>> = new Set();
subscribe(observer: Observer<T>): void {
this.observers.add(observer);
}
unsubscribe(observer: Observer<T>): void {
this.observers.delete(observer);
}
async emit(event: T): Promise<void> {
await Promise.allSettled(
Array.from(this.observers).map(o => o.update(event))
);
}
}
interface Notifier {
send(message: string): Promise<void>;
}
class EmailNotifier implements Notifier {
async send(message: string): Promise<void> {
// Send via SMTP
}
}
class SlackNotifierDecorator implements Notifier {
constructor(private wrapped: Notifier) {}
async send(message: string): Promise<void> {
await this.wrapped.send(message);
await this.sendToSlack(message); // additional behavior
}
private async sendToSlack(message: string): Promise<void> {
// Post to Slack channel
}
}
// Third-party SDK (incompatible interface)
class StripeSDK {
createPayment(amountCents: number, currency: string, token: string): Promise<any> { }
}
// Target interface
interface PaymentProcessor {
charge(amount: Money): Promise<PaymentResult>;
}
// Adapter
class StripeAdapter implements PaymentProcessor {
constructor(private sdk: StripeSDK) {}
async charge(amount: Money): Promise<PaymentResult> {
const response = await this.sdk.createPayment(
amount.toCents(),
amount.currency,
amount.sourceToken
);
return { transactionId: response.id, status: response.status };
}
}
interface Command<T = void> {
execute(): Promise<T>;
}
class PlaceOrderCommand implements Command<Order> {
constructor(
private orderRepo: OrderRepository,
private eventBus: EventBus,
private items: OrderItem[],
) {}
async execute(): Promise<Order> {
const order = Order.create(this.items);
await this.orderRepo.save(order);
await this.eventBus.publish(new OrderPlacedEvent(order.id));
return order;
}
}
// Usage
const command = new PlaceOrderCommand(repo, bus, items);
await commandBus.dispatch(command);
interface OrderState {
addItem(item: OrderItem): void;
pay(amount: Money): void;
ship(): void;
}
class PendingState implements OrderState {
constructor(private order: Order) {}
addItem(item: OrderItem) { this.order.items.push(item); }
pay(amount: Money) { this.order.setState(new PaidState(this.order)); }
ship() { throw new Error('Cannot ship pending order'); }
}
class PaidState implements OrderState {
constructor(private order: Order) {}
addItem() { throw new Error('Cannot modify paid order'); }
pay() { throw new Error('Already paid'); }
ship() { this.order.setState(new ShippedState(this.order)); }
}
class Order {
state: OrderState = new PendingState(this);
setState(state: OrderState) { this.state = state; }
}
| Anti-Pattern | Problem | Better Alternative |
|---|---|---|
| Singleton | Global state, hidden dependencies | DI-managed single instance |
| God Class | Too many responsibilities | Decompose into smaller classes |
| Spaghetti Code | Unstructured flow | Clean Architecture layers |
| Golden Hammer | Applying familiar pattern everywhere | Match pattern to problem |
| Premature Optimization | Optimizing without profiling | Measure first, optimize later |
| Service Locator | Hidden dependencies | Constructor injection |
| Composition | Patterns | Use Case |
|---|---|---|
| Repository + Unit of Work | Enterprise | Data access with transaction coordination |
| Command + Observer + Mediator | Behavioral + Behavioral | CQRS with event bus |
| Strategy + Factory | Behavioral + Creational | Pluggable algorithms with auto-selection |
| Decorator + Proxy | Structural + Structural | Layered cross-cutting concerns |
| Adapter + Factory | Structural + Creational | Third-party integration abstraction |
| Saga + CQRS + Event Sourcing | Architectural + Architectural | Distributed transactions with audit |
| Chain of Responsibility + Composite | Behavioral + Structural | Request processing pipeline |
Hand off to backend/universal/microservices/SKILL.md if distributed system patterns need implementation details. Hand off to backend/universal/clean-architecture/SKILL.md if structuring the entire application. Hand off to backend/universal/event-driven/SKILL.md if event-driven patterns need elaboration.
` interface EventObserver { onEvent(event: T): Promise; }
class EventBus { private observers: Set<EventObserver> = new Set(); subscribe(observer: EventObserver): void { this.observers.add(observer); } unsubscribe(observer: EventObserver): void { this.observers.delete(observer); } async emit(event: T): Promise { const results = Array.from(this.observers).map(o => o.onEvent(event)); await Promise.allSettled(results); } } `
config: defaults: timeout: 30s retryCount: 3 overrides: production: timeout: 60s retryCount: 5 development: timeout: 300s retryCount: 1
| Metric | Threshold | Severity | Action |
|---|---|---|---|
| Error rate | > 1% over 5min | Critical | Page on-call |
| p99 latency | > 2s over 5min | Warning | Investigate |
| Throughput drop | > 50% over 1min | Critical | Check upstream |
| Queue depth | > 1000 over 1min | Warning | Scale consumers |
| Disk usage | > 85% | Warning | Clean or expand |
| Memory usage | > 90% heap | Critical | Restart or scale |
| Anti-Pattern | Symptom | Root Cause | Solution |
|---|---|---|---|
| Premature optimization | Complex code for no measured benefit | Guessing instead of profiling | Measure first, optimize based on data |
| Copy-paste reuse | Duplicate code across codebase | Lack of abstraction | Extract shared logic into libraries |
| Gold-plating | Features with no current requirement | Over-engineering | YAGNI — build what's needed now |
| Magical thinking | Assumptions without validation | Skipping error handling | Handle all failure modes explicitly |
Cache hierarchy: L1 (in-memory local) → L2 (distributed Redis/Memcached) → L3 (CDN/Edge). Cache invalidation: TTL-based (simple, stale), event-based (complex, fresh), write-through (consistent, higher write latency), write-behind (fast writes, eventual consistency).