Facade in Flutter

Alvaro Armijos
4 min readJan 8, 2023

It’s common that in our projects we have several complex systems. The Facade design pattern solves this, by creating a Class that encapsulates each of those systems and provides them in a very simple way, either in that Class, in a method or simply with an instance.

This pattern is commonly used in very large applications like Facebook, Instagram, YouTube, which handle logging, security and analytics subsystems.

Benefits

  • Structuring a system into subsystems helps reduce complexity. A common design goal is to minimize the communication and dependencies between subsystems. One way to achieve this goal is to introduce a Facade that provides a single, simplified interface to the more general facilities of a subsystem.
  • It shields clients from subsystem components, thereby reducing the number of objects that clients deal with and making the subsystem easier to use.
  • It promotes weak coupling between the subsystem and its clients. Often the components in a subsystem are strongly coupled. Weak coupling lets you vary the components of the subsystem without affecting its clients.
  • It doesn’t prevent applications from using subsystem classes if they need to. Thus you can choose between ease of use and generality.
Design Patterns, 2009. Erich Gamma, Richard Helm, Ralph Johnson & John Vlissides

When to use it?

Use the Facade pattern when:

  • you want to provide a simple interface to a complex subsystem. Subsystems often get more complex as they evolve. Most patterns, when applied, result in more and smaller classes. This makes the subsystem more reusable and easier to customize, but it also becomes harder to use for clients that don’t need to customize it. A Facade can provide a simple default view of the subsystem that is good enough for most clients. Only clients needing more customizability will need to look beyond the Facade.
  • there are many dependencies between clients and the implementation classes of an abstraction. Introduce a facade to decouple the subsystem from clients and other subsystems, thereby promoting subsystem independence and portability.
  • you want to layer your subsystems. Use a facade to define an entry point to each subsystem level. If subsystems are dependent, then you can simplify the dependencies between them by making them communicate with each other solely through their facades.

Facade in Flutter

In this example, we are going to assume that we have an application to make transfers. First we are going to create a Class called TransferFacade that will be in charge of encapsulating all the subsystems.

The TransferFacade has the instance of all the subsystems. Then we create the function transfer. First we get the token, then we ask Analytics to record the transfer and finally the TransferManager to make the transfer.

With this, the TransferFacade is encapsulating the complexity of 3 subsystems in just one transfer method. If there is a need to add or remove any of these subsystems, the responsibility rests solely with the TransferFacade.

Let’s assume that we have an Analytics subsystem called AnalyticsManager and another subsystem called SecurityManager. The security will simply return a token with the getToken function. And the Analytics system will register when the user makes the transfer with the registerTransfer function.

The Transfer system is in charge of executing the transaction with a transfer function. This is just one example, in real life these subsystems are very complex.

If you like it, you can Buy Me A Coffee!

--

--

Alvaro Armijos

Electronic and Telecommunications Engineer | #Flutter Developer 💙 | Always eager to learn | https://www.linkedin.com/in/alvaro-armijos-sarango/