Search code examples
androidretrofitretrofit2facade

What design pattern retrofit uses and how does it use it in Android?


I think Retrofit uses Facade design pattern

  • Is it true, If so how does it use it ( Since Facade gives a common interface to set of subsystems - Thus client can interact with this interface )
  • Does retrofit uses any other design pattern, if so how does it use it

Solution

  • This is by no means a comprehensive answer, but too long for a comment.

    I wouldn't say it has a single pattern. You can definitely find several there, i.e., Builder for when you are building the retrofit instance.

    I guess one could say the main pattern is the Proxy Pattern. To the best of my knowledge Retrofit uses this to implement the api interface we define.

    It creates a proxy object for the interface and every time the methods get called, it will go through the annotations and build the correct http request. From our perspective it feels like we're calling an implementation, but it's actually a proxy.

    I suppose one could say that for the interceptors it uses Chain of Responsibility. However, the interceptors are part of a dependency - OkHttp - rather than retrofit itself.

    As for the question "How does it use it in Android?" - I'd say no different than in any other place. Retrofit's design patterns are not specific for Android and would work everywhere the same.