Currently, I'm working on my academic research where I design an android app by implementing Gang of Four design patterns as possible (note: GoF pattern is a must, it is part of the research that I conduct).
Now I've come to the part where I fetch data from remote/Rest API via Repository. I don't want the app to always fetch the very same data from the remote because that is very expensive (consumes time, resource, bandwidth, etc), as most apps would do, I decide to add caching mechanism.
In my previous android app project, I usually "cache" the data using a database (Room) combined with a dedicated field to keep the cache object stays on the memory (see the snippet code below).
But, I almost run out of time (due to research deadline), I want to focus more on the implementation of GoF on the app that I design and prefer to avoid database related stuff.
The code below is the snippet of my Repository clas. When I (let's say a ViewModel) need the user data it will call getUser(
) method from the Repository.
Inside
getUser()
method I will check if the shouldRefresh is false and the cache object is not null. Once true, it returns thecacheUser
, but if false then thefetchUser()
method (an interface provided by the retrofit library) will be called. I would consider that fetchUser() performs a very heavy and expensive task (since it requires the internet and the process depends on the network quality).
The snippet of Repository.java
class.
public class AccountRepository {
private AccountWebservice webservice;
private User cacheUser;
public User getUser(String accessToken, int userId, boolean shouldRefresh) {
if (!shouldRefresh && cacheUser != null) {
return cacheUser;
}
Call<SinglePayloadResponse<User>> call = webservice.fetchUser(accessToken, userId);
call.enqueue(new Callback<SinglePayloadResponse<User>>() {
@Override
public void onResponse(Call<SinglePayloadResponse<User>> call, Response<SinglePayloadResponse<User>> response) {
assert response.body() != null;
cacheUser = response.body().getPayload(); // currently I'm *caching* the response by assign it to cacheUser
return cacheUser;
}
@Override
public void onFailure(Call<SinglePayloadResponse<User>> call, Throwable t) {
}
});
}
}
I found out that Proxy pattern (Smart Proxy Pattern) is used to handle the creation of expensive objects like in this example (Usage of Proxy design pattern), as we can see, the ProxyImage was introduced to wrap the real expensive object (RealImage) to not reload again if it is already loaded.
I see the smart proxy pattern has the behavior (like caching) that I like to accomplish in this app. I wonder if I can implement this pattern to cache response/s (like user data, recently viewed products, recent search results, and other relevant stuff that need to access ASAP) from the Webservice in the Repository class.
Now is the real question:
Is my case is reasonable enough to implement the Smart Proxy Pattern? If so, how to apply it? (Let's take the snippet of the repository class above is my current solution to access user data, and I need to apply smart proxy pattern)
I still try to learn the 23 catalogs of the GoF pattern, since I have limited time, limited knowledge, limited perspective, it is very easy for me to miss something and even important things about the pattern. So, any answer, recommendation, and clarification of 'my' or 'your' misunderstanding are appreciated, thanks.
Aucun commentaire:
Enregistrer un commentaire