Android业务中间层该如何设计?

1,281 阅读2分钟

如果一个产品需要有多个业务线,各业务线之间如何协作才是最高效的?

上图是比较常见的业务组装方式,如果需要添加某个业务,把相应的View直接写在Layout里,然后处理业务逻辑。但是如果业务模块多达几十个,散落的逻辑有几千行,这时该如何设计才能保证各业务的稳定和可扩展性?

公共业务应该是各个业务积木堆积组成,各个积木之间是黑盒状态,只能通过“窗口”向外提供服务,以及发布需求。中间层委托、代理信息的传递。

中间层在Android平台该如何设计?

  • Android平台起点及终点都是和界面的生命周期息息相关,中间层作为业务/界面的承载模型,所以应该继承自View。
  • 各个业务积木之间是独立、隔离、和动态的,业务积木通过中间承载模型加载/卸载也应该是动态的。
  • 中间层作为界面承载模型,所以也是有生命周期的,且依赖于外部。
  • 中间层除了承载、通信职责,也应该随着外部环境变化,去影响业务积木的改变。

对外协议

/**
 * 委托协议
 *
 * @author jacky
 * @version v1.0
 * @description 对外暴露的协议
 * @since 2017/9/14
 */
public interface IBusinessDelegate {
    IBusinessDelegate setup();
    IBusinessDelegate setupBusiness1();
    IBusinessDelegate setupBusiness2();
    IBusinessDelegate setupBusiness3();
    IBusinessDelegate setupBusiness4();
    void event1();
    void event2();
    void onCreate();
    void onResume();
    void onPause();
    void onDestroy();
}

中间层实现

/**
 * 委托
 *
 * @author jacky
 * @version v1.0
 * @description 委托,隔离各业务间的耦合
 * @since 2017/9/14
 */
public class BusinessDelegate extends RelativeLayout implements IBusinessDelegate {
    private WeakReference mContextReference;
    private TimingChestContract.Presenter mTimingChestPresenter;
    public BusinessDelegate(Context context) {
        super(context);
    }
    public BusinessDelegate(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public BusinessDelegate(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    public IBusinessDelegate setup() {
        mContextReference = new WeakReference<>(getContext());
        removeAllViews();
        return this;
    }
    // ---- 动态加载挂件 Start ----
    @Override
    public IBusinessDelegate setupBusiness1() {
        TimingChestView chestView = new TimingChestView(mContextReference.get());
        mTimingChestPresenter = new TimingChestPresenter(chestView, new TimingChestModel());
        chestView.setPresenter(mTimingChestPresenter);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        addView(chestView, params);
        return this;
    }
    @Override
    public IBusinessDelegate setupBusiness2() {
        return this;
    }
    @Override
    public IBusinessDelegate setupBusiness3() {
        return this;
    }
    @Override
    public IBusinessDelegate setupBusiness4() {
        return this;
    }
    // ---- 动态加载挂件 End ----
    // ---- 接收事件 Start ----
    @Override
    public void event1() {
        if (mTimingChestPresenter != null) {
            mTimingChestPresenter.notify();
        }
    }
    @Override
    public void event2() {
    }
    // ---- 接收事件 End ----
    // ---- 生命周期 Start ----
    @Override
    public void onCreate() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        if (mTimingChestPresenter != null) {
            mTimingChestPresenter.onCreate();
        }
    }
    @Override
    public void onResume() {
        if (mTimingChestPresenter != null) {
            mTimingChestPresenter.onResume();
        }
    }
    @Override
    public void onPause() {
        if (mTimingChestPresenter != null) {
            mTimingChestPresenter.onPause();
        }
    }
    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        if (mTimingChestPresenter != null) {
            mTimingChestPresenter.onDestroy();
        }
    }
    // ---- 生命周期 End ----
}

委托作为中间层的呈现方式,动态加载业务积木,并感应外部环境变化反应到业务积木。通过这种方式,业务积木就有了很好的稳定性和扩展性。并辅以辅助手段,使委托变得异常强大。

用心做好的内容 赏 Jacky WeChat Pay

微信打赏

Jacky Alipay

支付宝打赏