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

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

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

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

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

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

对外协议

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* 委托协议
*
* @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();
}

中间层实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* 委托
*
* @author jacky
* @version v1.0
* @description 委托,隔离各业务间的耦合
* @since 2017/9/14
*/

public class BusinessDelegate extends RelativeLayout implements IBusinessDelegate {

private WeakReference<Context> 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 ----
}

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

打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2017-2023 Jacky

所有的相遇,都是久别重逢

支付宝
微信