backgroundLayer 1Navigate back to the homepage

The Facade Design Pattern

Patryk Jeziorowski
December 30th, 2019 · 1 min read

Facade Introduction

In this post, we’ll quickly go through a structural design pattern - Facade.

According to Gang of Four definition, facade provides a simplified interface for a set of interfaces of a subsystem. It defines a convenient higher-level interface to interact with.

The facade decouples clients from subinterfaces of our modules. The client uses just one object - the facade, to delegate work to many separated components. The client doesn’t have to know the details of our architecture - it is all hidden behind a clean interface of the facade.

Without Facade

Facade Pattern

The client knows the elements of module and orchestrates the work.


With Facade

Facade Pattern

The client doesn’t know anything about subsystem, it communicates with the facade only.

When to use?

  • when you want to provide an easy interface to interact with a more complicated system
  • to decouple the clients from implementation details
  • to use as an entry point to some submodule

Elements

  • Facade - knows subsystem, orchestrates work, delegates requests to module components
  • Submodule Class - elements of our module
  • Client - uses facade to interact with the system

Example implementation

Let’s pretend that we’re working on an e-commerce application. We want to implement product ordering. We already got a few services in our app, but they are not integrated.

1public interface InventoryService {
2 boolean isAvailable(int productId);
3 int order(int productId);
4}
5
6public interface ShipmentService {
7 void shipTo(int userId, int productId);
8}
9
10public interface NotificationService {
11 void notifyAboutOrder(int userId, int orderId);
12 void notifyAboutIssue(int userId);
13}

(For simplicity, we use ints to represent users, orders etc.)

With those building blocks, we want to provide an API to order a product.

1public interface OrderFacade {
2 void order(int productId, int userId);
3}

We don’t want the client code to interact with all services just to make an order, so we provide nice and clean API with Facade.

Here’s its implementation:

1public class DefaultOrderFacade implements OrderFacade {
2 // Dependencies could be injected using Dependency Injection tool
3 public DefaultOrderFacade(final InventoryService inventory,
4 final NotificationService notifications,
5 final ShipmentService shipments) {
6 this.inventory = inventory;
7 this.notifications = notifications;
8 this.shipments = shipments;
9 }
10
11 private final InventoryService inventory;
12 private final NotificationService notifications;
13 private final ShipmentService shipments;
14
15 @Override
16 public void order(int productId, int userId) {
17 if (inventory.isAvailable(productId)) {
18 final int orderId = inventory.order(productId);
19 shipments.shipTo(userId, productId);
20 notifications.notifyAboutOrder(userId, orderId);
21 } else {
22 notifications.notifyAboutIssue(userId);
23 }
24 }
25}

In this way, instead of using three different services to make an order, we are using just one - the Facade. All services methods are hidden behind the facade - making an order is much simpler now.

1public class SomeClientCode {
2 // This could be injected by some DI framework
3 private OrderFacade orderfacde;
4
5 public static void main(String[] args) {
6 final int someProductId;
7 final int forSomeUserId;
8
9 // ordering product is much easier now with Facade
10 orderFacade.order(someProductId, forSomeUserId);
11 }
12}

In real apps the services would have even more methods - we could hide them all and provide higher-level APIs by adding more methods to Facades.

Conclusions

The facade implementation is responsible for distributing the work among elements of our application. The client is decoupled from specific implementations and have a nice entry point to the system.

In my opinion, this pattern is very nice, as it’s very easy to implement and appears somewhat naturally in projects. It’s one of the most common patterns I’ve seen in production apps that I’ve worked with.

Join our email list and get notified about new content

Be the first to receive our latest content with the ability to opt-out at anytime. We promise to not spam your inbox or share your email with any third parties.

More articles from talkoverflow

React & GraphQL blog Done & Deployed in 10 minutes

Hello! Do you know Dan Abramov ? He's the creator of popular Javascript react-redux library and a member of the core React team. In this…

December 28th, 2019 · 2 min read

Adapter pattern

Introduction In this post, we'll introduce a popular design pattern - Adapter . Adapters are common in everyday life, so you should have an…

December 25th, 2019 · 2 min read
© 2019–2020 talkoverflow
Link to $https://facebook.com/talkoverflowsoftware/Link to $https://twitter.com/pjeziorowskiLink to $https://github.com/pjeziorowskiLink to $https://www.linkedin.com/in/patryk-jeziorowski-81587414a/