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

Adapter, Facade, Decorator and Proxy all wrap other objects. How do they differ?

They differ in their goal and in the interface they show to the caller.

Adapter converts one interface into another interface that the client expects. Example: StripePaymentGateway implements your PaymentGateway interface. It translates each call into a call to the Stripe SDK.

Facade gives one simpler interface over a whole subsystem. Example: CheckoutFacade.placeOrder() hides the calls to inventory, pricing, payment and email.

Decorator keeps the same interface and adds behaviour. You can stack decorators inside each other: new RetryingNotifier(new LoggingNotifier(email)).

Proxy also keeps the same interface. But its job is to control access to the real object. It can be used for:

  • lazy creation (create the real object only when first needed)
  • caching
  • access checks
  • standing in for a remote object

In short:

  • Adapter changes the interface.
  • Facade turns many interfaces into one simple one.
  • Decorator adds features and keeps the same interface.
  • Proxy controls access and keeps the same interface.
  • Adapter translates an existing interface into the one the client needs.
  • Facade is one simple entry point over a subsystem.
  • Decorator keeps the same interface, adds behaviour, and can be stacked.
  • Proxy keeps the same interface and controls access, for lazy creation, caching, auth or remote objects.