android - model architecture

23
Android - Model Architecture Indonesia Mar 2016 Fandy Gotama

Upload: fandy-gotama

Post on 16-Apr-2017

476 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Android - Model Architecture

Android - Model Architecture

Indonesia

Mar 2016

Fandy Gotama

Page 2: Android - Model Architecture

2

Security

Page 3: Android - Model Architecture

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.

Page 4: Android - Model Architecture

4

Security: Securing your API

- OAuth,- HTTP Basic Authentication,- API Key.

Page 5: Android - Model Architecture

5

Networking best practices

Page 6: Android - Model Architecture

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.

Page 7: Android - Model Architecture

7

MVPModel Architecture

Page 8: Android - Model Architecture

Model Diagrams

OlxService

CommunicationImpl

CommunicationInterface

APIDataSource StorageDataSource

Repositories Pattern

Storage

DataSource Interface

DataStoreFactory

Page 9: Android - Model Architecture

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.

Page 10: Android - Model Architecture

10

Model Breakdown

Page 11: Android - Model Architecture

11

Repository Implementation

- In charge for getting data requested by presenter.

Page 12: Android - Model Architecture

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); }}

Page 13: Android - Model Architecture

13

DataStoreFactory

- An abstract class, has a job to get data from cache or from API,

- Repository will call getDataStore() method get the data.

Page 14: Android - Model Architecture

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

Page 15: Android - Model Architecture

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); }

}

Page 16: Android - Model Architecture

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.

Page 17: Android - Model Architecture

17

ShoppingApiDataStore Classpublic 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; }}

Page 18: Android - Model Architecture

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).

Page 19: Android - Model Architecture

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); }}

Page 20: Android - Model Architecture

20

In Summary

- Repository pattern is abstraction, reduce complexity and make the rest of the code persistent ignorant,

- Easy to write unit test.

Page 21: Android - Model Architecture

21

Source Code

https://github.com/CommunityShareCode/MVP

Page 23: Android - Model Architecture

23

Q&A