赞
踩
源码地址:https://github.com/ldy1993/ToolforAS.git
8.3.1我们已经创建了一个简单的mvc框架。实现了在界面中执行获取sdcard中的某个选中文件名称。
假如我们有新增一个需求,想要把选中的文件上送到后台。
我们可以将选取文件,和上送文件两个功能变成action或者是function或者是server,然后合并到Controller中执行。
我们重新改一下目录结构,将原来单一功能设置成一个一个action,然后由一个大的功能去管理
还是一样的构建一个开始-执行-结束
- /**
- * Action 抽象类定义
- *
- * @author ldy
- *
- */
- public abstract class AAction {
- /**
- * 子类构造方法必须调用super设置ActionStartListener
- * @param listener {@link ActionStartListener}
- */
- public AAction(ActionStartListener listener) {
- this.startListener = listener;
- }
-
- /**
- * Action结束监听器
- * @author ldy
- */
- private ActionStartListener startListener;
- public interface ActionStartListener {
- void onStart(AAction Action);
- }
- /**
- * Action结束监听器
- * @author ldy
- */
- private ActionEndListener endListener;
- public interface ActionEndListener {
- void onEnd(AAction Action, ActionResult result);
- }
- public void setEndListener(ActionEndListener listener) {
- this.endListener = listener;
- }
-
-
- /**
- * Action的具体处理方法
- */
- protected abstract void process();
-
- /**
- * 执行Action之前需要先设置{@link ActionEndListener},
- * 此接口内部先调用{@link ActionStartListener#onStart(AAction)} ,
- * 再调用{@link AAction#process}
- */
- public void execute() {
- if (startListener != null) {
- startListener.onStart(this);
- }
- process();
- }
-
-
- /**
- * 设置Action结果, 次接口内部调用{@link ActionEndListener#onEnd(AAction, ActionResult)}方法
- * @param result {@link ActionResult}
- */
- public void setResult(ActionResult result) {
- if (endListener != null) {
- endListener.onEnd(this, result);
- }
- }
- }
然后分别实例两个action功能
然后编写Controller,用于按顺序启动各种action
- /**
- * 事物控制器
- * @author ldy
- */
- public abstract class AController {
- private static final String TAG = "AController";
- /**
- * state和action的绑定关系表
- */
- private Map<String, AAction> actionMap;
-
- /**
- * Action结束监听器
- * @author ldy
- */
- private ControllerEndListener endListener;
- public interface ControllerEndListener {
- void onEnd(AController controller, ControllerResult result);
- }
- public void setEndListener(ControllerEndListener listener) {
- this.endListener = listener;
- }
-
-
- /**
- * 单个state绑定action
- * @param state
- * @param action
- */
- protected void bind(final String state, AAction action) {
- if (actionMap == null) {
- actionMap = new HashMap<String, AAction>();
- }
- actionMap.put(state, action);
- action.setEndListener(new AAction.ActionEndListener() {
- @Override
- public void onEnd(AAction action, ActionResult result) {
- onActionResult(state, result);
- }
- });
- }
-
- /**
- * 执行state状态绑定的action
- *
- * @param state
- */
- public void gotoState(String state) {
- AAction action = actionMap.get(state);
- if (action != null) {
- action.execute();
- } else {
- Log.e(TAG, "无效State:" + state);
- }
- }
-
- /**
- * 执行交易
- */
- public void execute() {
- bindStateOnAction();
- onStart();
- }
-
- /**
- * state绑定action抽象方法,在此实现中调用{@link #bind(String, AAction)}方法, 并在最后调用{@link #gotoState(String)}方法,执行第一个state
- */
- protected abstract void bindStateOnAction();
- /**
- * 调用前初始化
- */
- protected abstract void onStart();
- /**
- * action结果处理
- *
- * @param currentState :当前State
- * @param result :当前Action执行的结果
- */
- protected abstract void onActionResult(String currentState, ActionResult result);
-
- }
实力的代码如下
- public class UpFileController extends AController {
- private static final String TAG = "UpFileController";
-
- enum State {
- 选择文件,
- 网络通讯;
- }
-
- @Override
- protected void bindStateOnAction() {
- ActionListSdcard actionListSdcard = new ActionListSdcard(new AAction.ActionStartListener() {
- @Override
- public void onStart(AAction Action) {
-
- }
- });
- bind(State.选择文件.toString(), actionListSdcard);
- ActionCommunication actionCommunication = new ActionCommunication(new AAction.ActionStartListener() {
- @Override
- public void onStart(AAction Action) {
-
- }
- });
- bind(State.网络通讯.toString(), actionCommunication);
- }
-
- @Override
- protected void onStart() {
- gotoState(State.选择文件.toString());
- }
-
- @Override
- protected void onActionResult(String currentState, ActionResult result) {
-
- if (result.getRet() != ActionResult.SUCCESS) {
- return;
- }
- State state = State.valueOf(currentState);
- switch (state) {
- case 选择文件:
- Log.e(TAG,"获取到的文件名称"+result.getDetailMessage());
- gotoState(State.网络通讯.toString());
- break;
- case 网络通讯:
- Log.e(TAG,"网络通讯"+result.getDetailMessage());
- break;
- }
- }
- }
最后在view中进行调用即可
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。