你的位置:首页 > 软件开发 > 操作系统 > 一张图看Goodle Clean设计架构

一张图看Goodle Clean设计架构

发布时间:2016-06-14 20:00:04
之前用一张图分析了Google给出的MVP架构,但是在Google给出的所有案例里面除了基本的MVP架构还有其它几种架构,今天就来分析其中的Clean架构。同样的,网上介绍Clean架构的文章很多,我也就不用文字过多叙述了,还是用一张类图来分析一下Clean架构的这个案例吧。好了 ...

一张图看Goodle Clean设计架构

  之前用一张图分析了Google给出的MVP架构,但是在Google给出的所有案例里面除了基本的MVP架构还有其它几种架构,今天就来分析其中的Clean架构。同样的,网上介绍Clean架构的文章很多,我也就不用文字过多叙述了,还是用一张类图来分析一下Clean架构的这个案例吧。好了,先直接上图!

  一张图看Goodle Clean设计架构

  上完图,再说一说我对Clean架构的一个理解吧。对比前一篇文章的MVP架构图可以看出,clean在一定程度上继承了mvp的设计思想,但是其抽象程度比mvp更高。初次看这个demo的时候,确实被震撼了一下——原来用Java可以这样写代码!!!跟之前用的一些项目框架和我自己平时写的一些代码对比一下,只能感叹clean的这种设计思想真不是一般的程序员可以想出来的。它对接口、抽象类和实现类之间的实现、继承、调用关系发挥到了一个比较高的层次,它并不是像我们平时写代码那样很直白地写下来,而是充分利用了面向对象的封装性、继承性和多态性,是对面向对象思想的一个高度理解。其实,要说clean复杂,它确实有些难理解,可是如果你真的理解了面向对象思想,那么又会觉得这样的设计完全在情理之中。

  举个例子,在这个案例里面,对实体类的设计就进行了高度的抽象与封装。首先,为所有的实体类设计了基类——UseCase,UseCase的代码如下:

 1 /* 2  * Copyright 2016, The Android Open Source Project 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); 5  * you may not use this file except in compliance with the License. 6  * You may obtain a copy of the License at 7  * 8  *   http://www.apache.org/licenses/LICENSE-2.0 9  *10  * Unless required by applicable law or agreed to in writing, software11  * distributed under the License is distributed on an "AS IS" BASIS,12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13  * See the License for the specific language governing permissions and14  * limitations under the License.15 */16 17 package com.example.android.architecture.blueprints.todoapp;18 19 /**20  * Use cases are the entry points to the domain layer.21  *22  * @param <Q> the request type23  * @param <P> the response type24 */25 public abstract class UseCase<Q extends UseCase.RequestValues, P extends UseCase.ResponseValue> {26 27   private Q mRequestValues;28 29   private UseCaseCallback<P> mUseCaseCallback;30 31   public void setRequestValues(Q requestValues) {32     mRequestValues = requestValues;33   }34 35   public Q getRequestValues() {36     return mRequestValues;37   }38 39   public UseCaseCallback<P> getUseCaseCallback() {40     return mUseCaseCallback;41   }42 43   public void setUseCaseCallback(UseCaseCallback<P> useCaseCallback) {44     mUseCaseCallback = useCaseCallback;45   }46 47   void run() {48     executeUseCase(mRequestValues);49   }50 51   protected abstract void executeUseCase(Q requestValues);52 53   /**54    * Data passed to a request.55   */56   public interface RequestValues {57   }58 59   /**60    * Data received from a request.61   */62   public interface ResponseValue {63   }64 65   public interface UseCaseCallback<R> {66     void onSuccess(R response);67     void onError();68   }69 }

原标题:一张图看Goodle Clean设计架构

关键词:架构

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。