CodePark/ questions / Structural patterns
START PRACTICINGPRACTICE
Backend question · Software design & patterns › Structural patterns

Which pattern is shown here?

class TimingRepository implements OrderRepository {
  constructor(private inner: OrderRepository, private metrics: Metrics) {}
  async findById(id: string) {
    const start = Date.now();
    try { return await this.inner.findById(id); }
    finally { this.metrics.timing("orders.findById", Date.now() - start); }
  }
}
const repo = new TimingRepository(new CachingRepository(new PgOrderRepository()), metrics);
Decorator

This is a decorator. Each wrapper implements the same interface. Each one adds one behaviour and then passes the call to the inner object. The wrappers are stacked inside each other. "Adapter" is wrong, because an adapter changes the interface, and these wrappers don't.