上一步是使用了 Dagger2,这一步使用 RxJava,为啥不用2,还没学。
RxJava 我刚看了一点,大致就是查询数据,回调,处理数据,可能的分发,最终在 subscriber 中处理。
这里就是把 Presenter 作为最终响应的地方,其他层都是回调再回调。
而 RxJava 的回调比自己用 Callback 实现要清晰很多。
Data 层
/**
* The interface User repository.
*/
public interface UserRepository {
/**
* Gets user.
*
* @return the user
*/
Observable<User> getUser();
}
Callback 被移除了,能少一个参数也是值得高兴的。
/**
* The type User repository.
*/
public class UserRepositoryImpl implements UserRepository {
/**
* Instantiates a new User repository.
*/
@Inject
public UserRepositoryImpl() {
}
@Override
public Observable<User> getUser() {
User user = new User();
user.setName("Clean");
return Observable.just(user);
}
}
实际操作中是在 call 中获取数据,这里简单返回 User 对象。
domain 层
/**
* The type Use case.
*/
public abstract class UseCase {
/**
* Build use case observable observable.
*
* @return the observable
*/
protected abstract Observable buildUseCaseObservable();
private Subscription subscription = Subscriptions.empty();
/**
* Execute.
*
* @param UseCaseSubscriber the use case subscriber
*/
@SuppressWarnings("unchecked")
public void execute(Subscriber UseCaseSubscriber) {
this.subscription = this.buildUseCaseObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(UseCaseSubscriber);
}
/**
* Unsubscribe.
*/
public void unsubscribe() {
if (!subscription.isUnsubscribed()) {
subscription.unsubscribe();
}
}
}
依旧负责屏蔽线程差异。
/**
* The type Get user use case.
*/
public class GetUserUseCase extends UseCase {
private final UserRepository userRepository;
/**
* Instantiates a new Get user use case.
*
* @param userRepository the user repository
*/
@Inject
public GetUserUseCase(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override protected Observable buildUseCaseObservable() {
return this.userRepository.getUser();
}
}
Presenter
/**
* The type User presenter.
*/
public class UserPresenter extends Subscriber<User> {
public GetUserUseCase getUserUseCase;
/**
* Instantiates a new User details presenter.
*
* @param getUserUseCase the get user use case
*/
@Inject
public UserPresenter(GetUserUseCase getUserUseCase) {
this.getUserUseCase = getUserUseCase;
}
private UserView userView;
/**
* Sets user view.
*
* @param userView the user view
*/
public void setUserView(@NonNull UserView userView) {
this.userView = userView;
}
/**
* Initialize.
*/
public void initialize() {
if (userView != null) {
loadUserDetails();
}
}
private void loadUserDetails() {
showViewLoading();
getUser();
}
private void getUser() {
getUserUseCase.execute(this);
}
@Override
public void onCompleted() {
hideViewLoading();
}
@Override
public void onError(Throwable e) {
// Not cope error for simple.
}
@Override
public void onNext(User user) {
showUser(user);
}
private void showUser(User user) {
userView.renderUser(user);
}
private void showViewLoading() {
userView.showLoading();
}
private void hideViewLoading() {
userView.hideLoading();
}
}
推荐文章:
http://www.infoq.com/cn/articles/clean-architecture-model-to-develop-android-application