Proxy in Flutter

Alvaro Armijos
3 min readJan 6, 2023

The Proxy pattern is responsible for controlling access and providing a security layer in the communication between two Classes.

To review this pattern, we are going to suppose that we have an application that allows us to make money transfers. If we want to apply certain business rules when we make a transfer, the Proxy is the ideal one and it solves this problem.

Benefits

  • One reason to control access to a Class is to delay all the cost of creating and initializing it until it is actually needed to be used or all business rules are met.

When to use it?

If we want to know when to use this pattern, we must analyze whether we need more business rules or a security layer. So, the Proxy pattern should be used:

  • If we need a class to have restricted access, or simply have prior validations, with the proxy we can expose the necessary methods and do the validations before it reaches the final object.

Proxy in Flutter

In our example, we have an application to make transfers. The button is in charge of making the transfer and executing the event. This is where the Proxy pattern does its magic.

We are going to create the TransferProxy Class that helps us to intercept this event. We assume that we have 2 business rules at the time of making the transfer. A maximum and a minimum amount to transfer. We create a function checkTransfer to evaluate all the business rules. We are simply going to return a message when it complies or does not comply with the business rules.

When it complies with the business rules, we execute the event or function to make the transfer, for this we have the _doTransfer method. Within this method we can already call another system or Class to make the transfer. With this, the Proxy has already taken care of giving us a level of security and the other system that makes the transfer will have the reliability that the rules have been complied with.

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/