Retrofit uses about 12 design patterns :
1- Builder Pattern: to separate the construction of a complex object from its representation.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.build();
2- Façade Pattern: Facade mode requires that the external and internal communication of a subsystem must be carried out through a unified object, and this unified object in retrofit is the Retrofit object we created. We only configure Retrofit, and then obtain the interface object request data, the rest are Retrofit Things inside the framework, we don’t need to care.
interface BooksApi {
@GET("books")
fun listBooks(): Call<List<Book>>
}
3- Proxy Pattern: is used in the create method to create the interface object, as follows
public <T> T create(final Class<T> service) {
validateServiceInterface(service);
return (T)
Proxy.newProxyInstance(
service.getClassLoader(),
new Class<?>[] {service},
new InvocationHandler() {
private final Platform platform = Platform.get();
private final Object[] emptyArgs = new Object[0];
@Override
public @Nullable Object invoke(Object proxy, Method method, @Nullable Object[] args)
throws Throwable {
// If the method is a method from Object then defer to normal invocation.
if (method.getDeclaringClass() == Object.class) {
return method.invoke(this, args);
}
args = args != null ? args : emptyArgs;
return platform.isDefaultMethod(method)
? platform.invokeDefaultMethod(method, service, proxy, args)
: loadServiceMethod(method).invoke(args);
}
});
}
4- Adapter Pattern: is used by CallAdapter to transform the interface of one class into another interface that the client expects.
5- Abstract Factory Pattern: CallAdapter.Factory is an abstract factory, CallAdapter is the product interface of the factory.
Retrofit.Builder()
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.build()
6- Factory Pattern: The CallAdapter interface is also this interface, and its adapt method is this method of creating objects. For example, the adapt method of RxJava2CallAdapter will create an RxJava event stream object, and the anonymous inner class CallAdapter under ExecutorCallAdapterFactory will create an ExecutorCallbackCall object.
7- Decorator Pattern: ExecutorCallbackCall uses this mode in retrofit. For example, ExecutorCallbackCall enhances the function of OkHttpCall. The enqueue method mode of OkHttpCall is asynchronous. When the child thread is called, the return will be in the child thread. Switch to the main thread. It withProxy modevery similar.
8- Strategy Pattern: Retrofit addCallAdapterFactory() with addConverterFactory() Used to configure policies. In addCallAdapterFactory() Taking an example, the configured interface adaptation plant object is the policy context Context, CallAdapter Corresponding to strategy abstraction, different interface adapters are implemented CallAdapter.adapt() Methods, different factories will produce different strategies to achieve classes, and different strategies implemented ADAPT () behavior.
9- Observer Pattern: RXJAVA supported by Retrofit uses the observer mode, not only that, call the OkhttpCall object enqueue() When the method, the callback is used. The callback is also an observer mode.
10- Prototype Pattern: It is mainly used to copy an object. Creating an object using prototype mode is much better than direct New an object, because Object's Clone () is a local method, you can directly operate memory. Another benefit of prototype mode is to simplify object creation, if you need to create an object multiple times in a piece of code, you may wish to use prototype mode. Prototype mode usually needs to be realized Cloneable interface Indicates that the object is copied.
11- Flyweight Pattern: to reuse existing similar objects, if matching objects are not found, create new objects to reduce the number of creating objects to reduce memory, and improve performance. This type of design pattern belongs to structure mode.
12- Singleton Pattern: to ensure that there is only the unique objects of such classes in memory, belonging to the creation mode.
Check this article and this for more details.