独立于外部依赖
仓储模式(Repository Pattern)是存在于业务和数据库之间单独分离出来的一层,是对数据访问的封装。
public interface TasksDataSource { void getTask(@NonNull String taskId, @NonNull GetTaskCallback callback); void saveTask(@NonNull Task task); void completeTask(@NonNull Task task); void deleteTask(@NonNull String taskId); }
public final class Task { private final String mId; private final String mTitle; private final String mDescription; private final boolean mCompleted; // ... ... }
public class CompleteTask extends UseCase<CompleteTask.RequestValues, CompleteTask.ResponseValue> { private final TasksRepository mTasksRepository; @Override protected void executeUseCase(final RequestValues values) { String completedTask = values.getCompletedTask(); mTasksRepository.completeTask(completedTask); getUseCaseCallback().onSuccess(new ResponseValue()); } }
public class Statistics { private final int completedTasks; private final int activeTasks; //... ... }
public class GetStatistics extends UseCase<GetStatistics.RequestValues, GetStatistics.ResponseValue> { private final TasksRepository mTasksRepository; @Override protected void executeUseCase(RequestValues requestValues) { mTasksRepository.getTasks(new TasksDataSource.LoadTasksCallback() { @Override public void onTasksLoaded(List<Task> tasks) { int activeTasks = 0; int completedTasks = 0; // We calculate number of active and completed tasks } }); }
public class TasksPresenter implements TasksContract.Presenter { private final TasksContract.View mTasksView; private final GetTasks mGetTasks; private final CompleteTask mCompleteTask; private void loadTasks(boolean forceUpdate, final boolean showLoadingUI) { GetTasks.RequestValues requestValue = new GetTasks.RequestValues(forceUpdate, mCurrentFiltering); mUseCaseHandler.execute(mGetTasks, requestValue, new UseCase.UseCaseCallback<GetTasks.ResponseValue>() { @Override public void onSuccess(GetTasks.ResponseValue response) { // ... ... } @Override public void onError() { // The view may not be able to handle UI updates anymore if (!mTasksView.isActive()) { return; } mTasksView.showLoadingTasksError(); } }); } }
@RunWith(AndroidJUnit4.class) public class TasksScreenTest { private void createTask(String title, String description) { // Click on the add task button onView(withId(R.id.fab_add_task)).perform(click()); // Add task title and description onView(withId(R.id.add_task_title)).perform(typeText(title), closeSoftKeyboard()); // Type new task title onView(withId(R.id.add_task_description)).perform(typeText(description), closeSoftKeyboard()); // Type new task description and close the keyboard // Save the task onView(withId(R.id.fab_edit_task_done)).perform(click()); } // ... ... }
public class TasksPresenterTest { @Test public void completeTask_ShowsTaskMarkedComplete() { // Given a stubbed task Task task = new Task("Details Requested", "For this task"); // When task is marked as complete mTasksPresenter.completeTask(task); // Then repository is called and task marked complete UI is shown verify(mTasksRepository).completeTask(eq(task.getId())); verify(mTasksView).showTaskMarkedComplete(); } //... ... }
android/architecture-samples
https://github.com/android/architecture-samples/tree/todo-mvp-clean
android10/Android-CleanArchitecture-Kotlin
https://github.com/android10/Android-CleanArchitecture-Kotlin
微信搜索: 一码一浮生 ,或者搜索公众号ID: life2code