Sobes.tech
Junior — Middle

Explain the differences between adapter, decorator, and proxy patterns in programming.

sobes.tech AI

Answer from AI

The adapter, decorator, and proxy patterns have different purposes, although they all wrap objects:

  • Adapter — transforms the interface of a class into the interface expected by the client. Used when incompatible interfaces need to work together.

  • Decorator — dynamically adds responsibilities to objects, extending their functionality without modifying the original class.

  • Proxy — controls access to another object, potentially performing additional actions (e.g., lazy initialization, caching, access control).

Example:

  • Adapter: you have a class with a method specificRequest(), and the client expects request(). The adapter implements request(), calling specificRequest().
  • Decorator: wraps an object and adds behavior, such as logging calls.
  • Proxy: acts as an intermediary, for example, checking access rights before calling the real object's method.