Strategy, Decorator and Adapter

Strategy, Decorator and Adapter An architecture diagram generated by Archify. Checkout · _cost.PriceFor(order) · Architecture component Checkout _cost.PriceFor(order) IShippingCost · decimal PriceFor(Order o) · Architecture component · the whole contract IShippingCost decimal PriceFor(Order o) the whole contract FlatRate · reads nothing, 4.95 · Strategy: one of these is registered FlatRate reads nothing, 4.95 WeightBanded · reads o.WeightKg, 6.50 · Strategy: one of these is registered WeightBanded reads o.WeightKg, 6.50 FreeOverFifty · reads o.Subtotal, 5.95 · Strategy: one of these is registered FreeOverFifty reads o.Subtotal, 5.95 LoggingShippingCost · ref and a stopwatch · Decorator: wrap it and pass it on LoggingShippingCost ref and a stopwatch CachingShippingCost · answers, or passes on · Decorator: wrap it and pass it on CachingShippingCost answers, or passes on IMemoryCache · keyed on the order · Architecture component IMemoryCache keyed on the order RetryingShippingCost · twice at most · Decorator: wrap it and pass it on RetryingShippingCost twice at most KestrelShippingCost · innermost layer, and the adapter · Adapter: translate it across · adapter KestrelShippingCost innermost layer, and the adapter adapter Kestrel.Logistics.Sdk · QuoteRequest / QuoteResponse · Adapter: translate it across Kestrel.Logistics.Sdk QuoteRequest / QuoteResponse Kestrel API · the call that leaves the process · Architecture component Kestrel API the call that leaves the process one unchanged call hit answers here miss only kg to grams HTTP Strategy: one of these is registered Decorator: wrap it and pass it on Adapter: translate it across Legend Call site Your implementation In-process store The interface Third party

Strategy

  • • Three bodies, one interface, one unchanged call site
  • • The caller hands over the whole order, because it cannot know which fields matter
  • • services.AddScoped<IShippingCost, WeightBanded>()

Decorator

  • • Each layer holds another IShippingCost and passes the call on
  • • A cache hit returns from two layers in, so the courier is never called
  • • Registration order is the stack: logging wraps caching wraps retrying wraps the courier

Adapter

  • • Units: 2.4 kg becomes 2400 grams on the way in
  • • Shape: RateInPence 650 becomes a decimal 6.50 on the way out
  • • Failure: KestrelApiException becomes ShippingUnavailableException, and nothing of theirs gets past