If a product requires multiple lines of business, how can the lines of business collaborate most efficiently?
The figure above is a common way of business assembly. If a business needs to be added, the corresponding View is directly written in Layout, and then the business logic is processed. However, if there are dozens of business modules and thousands of lines of scattered logic, how can the design ensure the stability and scalability of each business?
The common business should be composed of various business building blocks with black boxes between them, which can only provide services and publish requirements through “Windows”. Transfer of information of the middle layer.
How should the middle tier be designed on Android?
- The Android platform starts and ends with the lifecycle of the interface. The middle layer is the business/interface bearer model, so it should be inherited from View.
- Business building blocks are independent, isolated, and dynamic, and business building blocks should also be dynamic when loaded/unloaded through an intermediate hosting model.
- As the interface bearer model, the middle tier also has a life cycle and is externally dependent.
- In addition to the responsibilities of carrying and communication, the middle layer should also influence the change of business building blocks as the external environment changes.
External contract
/** ** delegate protocol ** @author Jacky * @version v1.0 * @description External protocol * @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(); }Copy the code
Mid-tier implementation
/** * delegate ** @author Jacky * @version v1.0 * @description delegate, * @since 2017/9/14 */ public Class 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 = 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 onCreate() {// ---- End ---- // ---- lifecycle 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(); }} // ---- lifecycle End ----}Copy the code
As the presentation mode of the middle layer, delegation dynamically loads the business building blocks and responds to the changes of the external environment. In this way, business building blocks are stable and scalable. And then there are ancillary tools that make delegation incredibly powerful.
WeChat exceptional
Alipay rewards