3. 3
Security: HTTPS
- Always use HTTPS,
- Pay attention to TLS version supported by Android API,
- Do coordinate with the Phone Provider,
- Small user might experience slowness, expect bad
review.
6. 6
Networking best practices
- Don’t build your own custom http, use libraries,
- Always use timeout,
- Use Jackson library to parse JSON data to POJO,
- Always cache your data (including images),
- Pay attention to your activity / fragment lifecycle.
9. 9
Model Architecture
- Using repository pattern,
- Repository encapsulated set of objects in data store,
- Clean separation, and one way dependency between
presenter and model layer.
12. 12
Repository Class
public class ShoppingRepositoryImpl {
private final ShoppingDataStoreFactory mFactory;
public ShoppingRepositoryImpl(@NonNull ShoppingDataStoreFactory factory) {
mFactory = factory;
}
public Shopping getShopping(ShoppingRequest request) {
return mFactory.getDataStore().getShopping(request);
}
}
13. 13
DataStoreFactory
- An abstract class, has a job to get data from cache or
from API,
- Repository will call getDataStore() method get the data.
14. 14
public abstract class DataStoreFactory<T extends DataStore> {
public T getDataStore() {
DataStore dataStore;
if (isCacheAvailable()) {
dataStore = createDiskDataStore(mResponseCache);
} else {
dataStore = createApiDataStore(mResponseCache);
}
return (T) dataStore;
}
protected abstract T createDiskDataStore(ResponseCache responseCache);
protected abstract T createApiDataStore(ResponseCache responseCache);
DataStoreFactory: Abstract Class
15. 15
DataStoreFactory: Implementation Class
public class ShoppingDataStoreFactory extends
DataStoreFactory<ShoppingDataStore> {
@Override
protected ShoppingDataStore createDiskDataStore(ResponseCache
responseCache) {
return new ShoppingDiskDataStore(responseCache);
}
@Override
protected ShoppingDataStore createApiDataStore(ResponseCache
responseCache) {
return new ShoppingApiDataStore(mService, responseCache);
}
}
16. 16
ShoppingApiDataStore
- Called by DataStoreFactory to get the data from API,
- This class will connect to API using retrofit, and insert
the data into cache.
17. 17
ShoppingApiDataStore Class
public class ShoppingApiDataStore implements ShoppingDataStore {
@Override
public Shopping getShopping(ShoppingRequest request) {
Shopping shopping = mService.shopping(
request.callname,
RESONSE_ENCODING,
APP_ID,
SITE_ID,
request.keywords,
VERSION);
mResponseCache.putList(shopping);
return shopping;
}
}
18. 18
ShoppingDiskDataStore
- Called by DataStoreFactory to get the data from disk,
- This class will get the data from disk (can be mysql, file
system or memory).
19. 19
ShoppingDiskDataStore Class
public class ShoppingDiskDataStore implements ShoppingDataStore {
private final ResponseCache<Shopping, ShoppingRequest> mResponseCache;
public ShoppingDiskDataStore(@NonNull ResponseCache responseCache) {
mResponseCache = responseCache;
}
@Override
public Shopping getShopping(@NonNull ShoppingRequest request) {
return mResponseCache.getList(request);
}
}
20. 20
In Summary
- Repository pattern is abstraction, reduce complexity and
make the rest of the code persistent ignorant,
- Easy to write unit test.