πŸ§”πŸ» Michael Abadi S. | 🍎 iOS Engineer | 🏠 Bangkok, πŸ‡ΉπŸ‡­ Thailand | πŸ‡²πŸ‡¨ Indonesian

Welcome to the official web of Michael

Understanding Proxy Pattern in Swift within 5 minutes

Medium : Understanding Proxy Pattern in Swift within 5 minutes


DEFINITION


The proxy pattern is one of the few underrated patterns that nobody really talks about in iOS. A proxy pattern is one of the structural design patterns that lets you provide a substitute or placeholder for another object. A proxy has total control and access to the original object, and let the proxy object do some magical stuff with the original object and restrict the access to it, however, the one who uses a proxy object, won’t have any knowledge behind the scene.


Proxy

Proxy pattern normally being used outside of mobile development, when some request wants to map the request to a specific object without exposing underlying knowledge about that object, so it can be interchangeable with any object that conforms to the original object. However, proxy patterns can be also used in mobile development.

The benefit of the proxy, it can be disguised as a real object, because the interface is similar to the real client object, however, we don’t interrupt the original object that will do the process under the hood, which means we can interpolate or dislodge something in the proxy without we harm the original object. Proxy is having similar behavior as a gateway to the original process of the operation.

INITIAL CODE


Let’s take real use case here. A repository object wants to call a network request to an API, however since the company has moved forward to the new system, they want to migrate gradually from the old REST API to the GraphQL endpoint. So in order for the repository to still able to work with the existing interface without breaking change, we will make a Proxy object that has a similar interface as the original object does


Proxy1

FINAL CODE


So the code is quite simple, define your interface of the service. Then make your real implementation class of the service for both classes that serving the old system with REST API and the new system with GraphQL. This how the caller looks like. Pretty simple, instantiate your proxy class and just call the proxy method.


Proxy2

CONCLUSION

There is some confusion related to the difference between an adapter and a proxy pattern. This Stack overflow answer has a very good answer about that. Basically Proxy is only a surrogate and it has a similar interface with the original object, however the opposite with the adapter. The adapter pattern's intention is to help the accessor able to work with the old system that can’t work together with the new implementation, hence the adapter object and the adaptee object has a different interface.



Published on 2021-01-26 14:00:00 +0000

Tagged with: