System ArchitecturePoC Developer/Architect

What is the fundamental difference between the Proxy and Facade patterns at the architectural level, and when to apply them?

Pass interviews with Hintsage AI assistant

Answer.

Proxy and Facade are structural design patterns that solve different tasks in application architecture organization.

Proxy acts as a surrogate or "substitute" for an object. Its purpose is to control access, cache, implement lazy loading, regulate security, or distribute requests (for example, network proxy, security proxy). Proxy implements the same contract (interface) as the real object.

Example in Java:

public interface Service { void operation(); } public class RealService implements Service { public void operation() { // ... } } public class ProxyService implements Service { private RealService realService = new RealService(); public void operation() { // access check realService.operation(); } }

Facade is a pattern that hides the complexity of a subsystem, providing a single simplified interface. Facade does not deal with access control or additional logic; it only aggregates calls.

Example in Java:

public class PaymentFacade { private CardService card; private SmsService sms; public void pay() { card.check(); card.charge(); sms.notify(); } }

When to use:

  • Proxy — if access control, caching, security logic, or lazy loading are required
  • Facade — when it is necessary to simplify the client's work with a set of subsystems and reduce coupling
  • They can complement each other, but the tasks are different

Key features:

  • Proxy implements the target interface, Facade has its own
  • Proxy is transparent to the client, Facade is an additional abstraction
  • Proxy adds behavior, Facade only simplifies work

Tricky questions.

Is it allowed to add functionality beyond the original subsystem classes with the Facade pattern?

No, Facade only aggregates and simplifies calls; new functionality is implemented with other patterns or in new services.

Is it mandatory for Proxy to be implemented through inheritance from the target object?

No, Proxy implements the target interface, but composition (encapsulation of the real object) is often used instead of inheritance.

Can Facade be used for access control or error logging?

It is better to use Proxy for that. Facade is not meant for built-in support for caching, logging, or permission checks.