Share
Embed
ROSIE: CLEAN USE CASE FRAMEWORK
During the past years we have been discussing and iterating over our mobile application architecture at Karumi, and the result of all that work is called Rosie.
Rosie is a framework that helps application development by focusing on three pillars that we consider very important: testability, code scalability y code readability.
This talk is not about promoting Rosie but rather the creation process of this framework, the needs that we needed to cover and how we materialized all the literature present in tons of books and internet articles. This is about problems and solutions
Karumi
August 11, 2016
Transcript
86 slides
-
ROSIE: CLEAN USE CASE FRAMEWORK Jorge Juan Barroso Carmona [email protected]
@flipper83 +JorgeJBarroso Pedro Gomez Sergio Gutierrez -
Basho. Poet Karumi is the beauty of ordinary things spoken
of in a simple way. -
Sergio Gutierrez Senior Mobile Engineer Pedro Gomez Senior Mobile Engineer
Alberto Gragera Technical Director Davide Mendolia Senior Full Stack Engineer -
Adam Tornhill “If Clean Architecture is about running away from
frameworks, does a clean architecture framework make sense?” -
Activity Fragment Service View … Presenter View Use Case Rich
Model Repository DataSource Api Retrofit SharedPref Api Sync Execution -
Adam Tornhill public class SampleActivity extends RosieActivity implements SamplePresenter.View {
@Inject @Presenter SamplePresenter presenter; @Override protected void onPreparePresenter() {/*...*/} } -
Adam Tornhill public class SampleActivity extends RosieActivity implements SamplePresenter.View {
@Inject @Presenter SamplePresenter presenter; @Override protected void onPreparePresenter() {/*...*/} } -
Adam Tornhill public class SampleActivity extends RosieActivity implements SamplePresenter.View {
@Inject @Presenter SamplePresenter presenter; @Override protected void onPreparePresenter() {/*...*/} } -
Adam Tornhill public class SamplePresenter extends RosiePresenter<SamplePresenter.View> { protected void
initialize() {/*...*/} protected void update() {/*...*/} protected void pause() {/*...*/} protected void destroy() {/*...*/} private void sampleMethod() { View view = getView(); // Get the view linked to the presenter view.foo(); } public interface View extends RosiePresenter.View { void foo(); } -
Adam Tornhill public class SamplePresenter extends RosiePresenter<SamplePresenter.View> { protected void
initialize() {/*...*/} protected void update() {/*...*/} protected void pause() {/*...*/} protected void destroy() {/*...*/} private void sampleMethod() { View view = getView(); // Get the view linked to the presenter view.foo(); } public interface View extends RosiePresenter.View { void foo(); } -
Adam Tornhill public class SamplePresenter extends RosiePresenter<SamplePresenter.View> { protected void
initialize() {/*...*/} protected void update() {/*...*/} protected void pause() {/*...*/} protected void destroy() {/*...*/} private void sampleMethod() { View view = getView(); // Get the view linked to the presenter view.foo(); } public interface View extends RosiePresenter.View { void foo(); } -
Adam Tornhill public class SamplePresenter extends RosiePresenter<SamplePresenter.View> { protected void
initialize() {/*...*/} protected void update() {/*...*/} protected void pause() {/*...*/} protected void destroy() {/*...*/} private void sampleMethod() { View view = getView(); // Get the view linked to the presenter view.foo(); } public interface View extends RosiePresenter.View { void foo(); } -
Adam Tornhill public final class PresenterLifeCycleLinker { public void initializeLifeCycle(Object
source, RosiePresenter.View view) { if (source == null) { throw new IllegalArgumentException( "The source instance used to initialize the presenters can't be null"); } if (view == null) { throw new IllegalArgumentException( "The view instance used to initialize the presenters can't be null"); } addAnnotatedPresenter(source); setView(view); initializePresenters(); } -
Adam Tornhill public final class PresenterLifeCycleLinker { public void initializeLifeCycle(Object
source, RosiePresenter.View view) { if (source == null) { throw new IllegalArgumentException( "The source instance used to initialize the presenters can't be null"); } if (view == null) { throw new IllegalArgumentException( "The view instance used to initialize the presenters can't be null"); } addAnnotatedPresenter(source); setView(view); initializePresenters(); } -
Adam Tornhill void resetView() { final Class<?> viewClass = getViewInterfaceClass();
InvocationHandler emptyHandler = new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return null; } }; ClassLoader classLoader = viewClass.getClassLoader(); Class[] interfaces = new Class[1]; interfaces[0] = viewClass; this.view = (T) Proxy.newProxyInstance(classLoader, interfaces, emptyHandler); } -
Adam Tornhill void resetView() { final Class<?> viewClass = getViewInterfaceClass();
InvocationHandler emptyHandler = new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return null; } }; ClassLoader classLoader = viewClass.getClassLoader(); Class[] interfaces = new Class[1]; interfaces[0] = viewClass; this.view = (T) Proxy.newProxyInstance(classLoader, interfaces, emptyHandler); } -
Adam Tornhill void resetView() { final Class<?> viewClass = getViewInterfaceClass();
InvocationHandler emptyHandler = new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return null; } }; ClassLoader classLoader = viewClass.getClassLoader(); Class[] interfaces = new Class[1]; interfaces[0] = viewClass; this.view = (T) Proxy.newProxyInstance(classLoader, interfaces, emptyHandler); } -
Adam Tornhill public class ComicsPresenter extends RosiePresenter<ComicsPresenter.View> { public void
getComics() { createUseCaseCall(getComics) .args(superHeroId, authorId) .onSuccess(new OnSuccessCallback() { /*...*/ }) .onError(new OnErrorCallback() { /*...*/ }) .execute(); } -
Adam Tornhill public class ComicsPresenter extends RosiePresenter<ComicsPresenter.View> { public void
getComics() { createUseCaseCall(getComics) .args(superHeroId, authorId) .name(“sortByDate”) .onSuccess(new OnSuccessCallback() { /*...*/ }) .onError(new OnErrorCallback() { /*...*/ }) .execute(); } -
Adam Tornhill public class ComicsPresenter extends RosiePresenter<ComicsPresenter.View> { public void
getComics() { createUseCaseCall(getComics) .args(superHeroId, authorId) .name(“sortByDate”) .onSuccess(new OnSuccessCallback() { /*...*/ }) .onError(new OnErrorCallback() { /*...*/ }) .execute(); } -
Adam Tornhill public class ComicsPresenter extends RosiePresenter<SamplePresenter.View> { public void
getComics() { createUseCaseCall(getComics) .args(superHeroId, authorId) .name(“sortByDate”) .onSuccess(new OnSuccessCallback() { /*...*/ }) .onError(new OnErrorCallback() { /*...*/ }) .execute(); } -
Adam Tornhill public class GetComics extends RosieUseCase { @UseCase (name
= “sortByDate”) public void getComicsSortedByDate(String superHeroId, String authorId) { try { Comics comics = getComics(superHeroId, authorId); notifySuccess(comics.getSortedByDate()); } catch (DataException e) { notifyError(generateDataError(e)); } } } -
Adam Tornhill public class GetComics extends RosieUseCase { @UseCase (name
= “sortByDate”) public void getComicsSortedByDate(String superHeroId, String authorId) { try { Comics comics = getComics(superHeroId, authorId); notifySuccess(comics.getSortedByDate()); } catch (DataException e) { notifyError(generateDataError(e)); } } } -
Adam Tornhill public class GetComics extends RosieUseCase { @UseCase (name
= “sortByDate”) public void getComicsSortedByDate(String superHeroId, String authorId) { try { Comics comics = getComics(superHeroId, authorId); notifySuccess(comics.getSortedByDate()); } catch (DataException e) { notifyError(generateDataError(e)); } } } -
Adam Tornhill public class GetComics extends RosieUseCase { @UseCase (name
= “sortByDate”) public void getComicsSortedByDate(String superHeroId, String authorId) { try { Comics comics = getComics(superHeroId, authorId); notifySuccess(comics.getSortedByDate()); } catch (DataException e) { notifyError(generateDataError(e)); } } } -
Adam Tornhill public class UseCaseHandler { private final TaskScheduler
taskScheduler; private final ErrorHandler errorHandler; public UseCaseHandler(TaskScheduler taskScheduler, ErrorHandler errorHandler) { this.taskScheduler = taskScheduler; this.errorHandler = errorHandler; } -
Adam Tornhill public class UseCaseHandler { private final TaskScheduler
taskScheduler; private final ErrorHandler errorHandler; public UseCaseHandler(TaskScheduler taskScheduler, ErrorHandler errorHandler) { this.taskScheduler = taskScheduler; this.errorHandler = errorHandler; } -
Adam Tornhill public class UseCaseHandler { private final TaskScheduler
taskScheduler; private final ErrorHandler errorHandler; public UseCaseHandler(TaskScheduler taskScheduler, ErrorHandler errorHandler) { this.taskScheduler = taskScheduler; this.errorHandler = errorHandler; } -
Adam Tornhill public interface TaskScheduler { void execute(UseCaseWrapper useCaseWrapper); }
public class TaskSchedulerJobQueue implements TaskScheduler { private final JobManager jobManager; /**/ @Override public void execute(UseCaseWrapper useCaseWrapper) { UseCaseWrapperJob useCaseWrapperJob = new UseCaseWrapperJob(useCaseWrapper); jobManager.addJobInBackground(useCaseWrapperJob); } } -
Adam Tornhill public interface TaskScheduler { void execute(UseCaseWrapper useCaseWrapper); }
public class TaskSchedulerJobQueue implements TaskScheduler { private final JobManager jobManager; /**/ @Override public void execute(UseCaseWrapper useCaseWrapper) { UseCaseWrapperJob useCaseWrapperJob = new UseCaseWrapperJob(useCaseWrapper); jobManager.addJobInBackground(useCaseWrapperJob); } } -
Adam Tornhill public interface TaskScheduler { void execute(UseCaseWrapper useCaseWrapper); }
public class TaskSchedulerJobQueue implements TaskScheduler { private final JobManager jobManager; /**/ @Override public void execute(UseCaseWrapper useCaseWrapper) { UseCaseWrapperJob useCaseWrapperJob = new UseCaseWrapperJob(useCaseWrapper); jobManager.addJobInBackground(useCaseWrapperJob); } } -
Adam Tornhill protected final UseCaseCall createUseCaseCall(RosieUseCase useCase) { UseCaseCall useCaseCall
= new UseCaseCall(useCase, useCaseHandler); retainUseCaseCall(useCaseCall); return useCaseCall; } -
Adam Tornhill protected final UseCaseCall createUseCaseCall(RosieUseCase useCase) { UseCaseCall useCaseCall
= new UseCaseCall(useCase, useCaseHandler); retainUseCaseCall(useCaseCall); return useCaseCall; } -
Adam Tornhill final class UseCaseParams { private final String
useCaseName; private final Object[] args; private WeakReference<OnSuccessCallback> onSuccessCallback; private WeakReference<OnErrorCallback> onErrorCallback; /**/ } -
Adam Tornhill final class UseCaseParams { private final String
useCaseName; private final Object[] args; private WeakReference<OnSuccessCallback> onSuccessCallback; private WeakReference<OnErrorCallback> onErrorCallback; /**/ } -
Repository DataSource Api Retrofit Api Model Mapper Domain Model Cache
level DataSource Realm Realm Realm Model Mapper Use case/ rich model Populator -
Adam Tornhill public class SampleRepository extends RosieRepository<Key, Value> { @Inject
public SampleRepository(SampleApiDataSource sampleApiDataSource, SampleCacheDataSource sampleCacheDataSource) { addReadableDataSources(sampleApiDataSource); addCacheDataSources(sampleCacheDataSource); } } -
Adam Tornhill public class SampleRepository extends RosieRepository<Key, Value> { @Inject
public SampleRepository(SampleApiDataSource sampleApiDataSource, SampleCacheDataSource sampleCacheDataSource) { addReadableDataSources(sampleApiDataSource); addCacheDataSources(sampleCacheDataSource); } } -
Adam Tornhill public class SampleRepository extends RosieRepository<Key, Value> { @Inject
public SampleRepository(SampleApiDataSource sampleApiDataSource, SampleCacheDataSource sampleCacheDataSource) { addReadableDataSources(sampleApiDataSource); addCacheDataSources(sampleCacheDataSource); } } -
Adam Tornhill public class SampleRepository extends RosieRepository<Key, Value> { @Inject
public SampleRepository() { PaginatedCacheDataSource<Key, Value> inMemoryCacheDataSource = new InMemoryCacheDataSource<>(new TimeProvider(), MINUTES.toMillis(5)); addCacheDataSources(inMemoryCacheDataSource); } } -
Adam Tornhill Key key = /*…*/; Value value; Collection<Value> values;
RosieRepository<Key, Value> repository = /*...*/; value = repository.getByKey(key); value = repository.getByKey(key, ReadPolicy.CACHE_ONLY); values = repository.getAll(); values = repository.getAll(ReadPolicy.READABLE_ONLY); repository.addOrUpdate(value); repository.addOrUpdate(value, WritePolicy.WRITE_ONCE); repository.deleteByKey(key); repository.deleteAll(); -
Adam Tornhill Key key = /*…*/; Value value; Collection<Value> values;
RosieRepository<Key, Value> repository = /*...*/; value = repository.getByKey(key); value = repository.getByKey(key, ReadPolicy.CACHE_ONLY); values = repository.getAll(); values = repository.getAll(ReadPolicy.READABLE_ONLY); repository.addOrUpdate(value); repository.addOrUpdate(value, WritePolicy.WRITE_ONCE); repository.deleteByKey(key); repository.deleteAll(); -
Adam Tornhill Key key = /*…*/; Value value; Collection<Value> values;
RosieRepository<Key, Value> repository = /*...*/; value = repository.getByKey(key); value = repository.getByKey(key, ReadPolicy.CACHE_ONLY); values = repository.getAll(); values = repository.getAll(ReadPolicy.READABLE_ONLY); repository.addOrUpdate(value); repository.addOrUpdate(value, WritePolicy.WRITE_ONCE); repository.deleteByKey(key); repository.deleteAll(); -
Adam Tornhill Key key = /*…*/; Value value; Collection<Value> values;
RosieRepository<Key, Value> repository = /*...*/; value = repository.getByKey(key); value = repository.getByKey(key, ReadPolicy.CACHE_ONLY); values = repository.getAll(); values = repository.getAll(ReadPolicy.READABLE_ONLY); repository.addOrUpdate(value); repository.addOrUpdate(value, WritePolicy.WRITE_ONCE); repository.deleteByKey(key); repository.deleteAll(); -
Repository DataSource Api Retrofit Api Model Mapper Domain Model Cache
level DataSource Realm Realm Realm Model Mapper Use case/ rich model Populator -
Adam Tornhill public class InMemoryCacheDataSource<K, V extends Identifiable<K>> implements CacheDataSource<K,
V> { protected final TimeProvider timeProvider; protected final long ttlInMillis; protected final Map<K, V> items; protected long lastItemsUpdate; public InMemoryCacheDataSource(TimeProvider timeProvider, long ttlInMillis) { this.timeProvider = timeProvider; this.ttlInMillis = ttlInMillis; this.items = new LinkedHashMap<>(); } @Override public synchronized V getByKey(K key) { return items.get(key); } @Override public synchronized Collection<V> getAll() { return new ArrayList<>(items.values()); } -
Adam Tornhill public class InMemoryCacheDataSource<K, V extends Identifiable<K>> implements CacheDataSource<K,
V> { protected final TimeProvider timeProvider; protected final long ttlInMillis; protected final Map<K, V> items; protected long lastItemsUpdate; public InMemoryCacheDataSource(TimeProvider timeProvider, long ttlInMillis) { this.timeProvider = timeProvider; this.ttlInMillis = ttlInMillis; this.items = new LinkedHashMap<>(); } @Override public synchronized V getByKey(K key) { return items.get(key); } @Override public synchronized Collection<V> getAll() { return new ArrayList<>(items.values()); } -
Adam Tornhill public class InMemoryCacheDataSource<K, V extends Identifiable<K>> implements CacheDataSource<K,
V> { protected final TimeProvider timeProvider; protected final long ttlInMillis; protected final Map<K, V> items; protected long lastItemsUpdate; public InMemoryCacheDataSource(TimeProvider timeProvider, long ttlInMillis) { this.timeProvider = timeProvider; this.ttlInMillis = ttlInMillis; this.items = new LinkedHashMap<>(); } @Override public synchronized V getByKey(K key) { return items.get(key); } @Override public synchronized Collection<V> getAll() { return new ArrayList<>(items.values()); } -
Adam Tornhill public Collection<V> getAll(ReadPolicy policy) throws Exception { Collection<V>
values = null; if (policy.useCache()) { values = getValuesFromCaches(); } if (values == null && policy.useReadable()) { values = getValuesFromReadables(); } if (values != null) { populateCaches(values); } return values; } -
Adam Tornhill public Collection<V> getAll(ReadPolicy policy) throws Exception { Collection<V>
values = null; if (policy.useCache()) { values = getValuesFromCaches(); } if (values == null && policy.useReadable()) { values = getValuesFromReadables(); } if (values != null) { populateCaches(values); } return values; } -
Adam Tornhill private Collection<V> getValuesFromCaches() throws Exception { Collection<V> values
= null; for (CacheDataSource<K, V> cacheDataSource : cacheDataSources) { values = cacheDataSource.getAll(); if (values != null) { if (areValidValues(values, cacheDataSource)) { break; } else { cacheDataSource.deleteAll(); values = null; } } } return values; } -
Adam Tornhill private Collection<V> getValuesFromCaches() throws Exception { Collection<V> values
= null; for (CacheDataSource<K, V> cacheDataSource : cacheDataSources) { values = cacheDataSource.getAll(); if (values != null) { if (areValidValues(values, cacheDataSource)) { break; } else { cacheDataSource.deleteAll(); values = null; } } } return values; } -
Adam Tornhill public Collection<V> getAll(ReadPolicy policy) throws Exception { Collection<V>
values = null; if (policy.useCache()) { values = getValuesFromCaches(); } if (values == null && policy.useReadable()) { values = getValuesFromReadables(); } if (values != null) { populateCaches(values); } return values; } -
Adam Tornhill public Collection<V> getAll(ReadPolicy policy) throws Exception { Collection<V>
values = null; if (policy.useCache()) { values = getValuesFromCaches(); } if (values == null && policy.useReadable()) { values = getValuesFromReadables(); } if (values != null) { populateCaches(values); } return values; } -
protected void populateCaches(Collection<V> values) throws Exception { for (CacheDataSource<K, V>
cacheDataSource : cacheDataSources) { cacheDataSource.addOrUpdateAll(values); } }