Android前端架构设计:基于观察者模式的架构设计
今天有个想法,但是没有实际实践过,欢迎各路大哥批评指点。基于“凡事皆事件”的原理,我把所有的事件都统一起来,并且给每个事件一个名称,通过java观察者模式的方式监听事件,从而把业务逻辑集中起来进行处理。
先来看看我的观察者类
public class ActionBuilding {
private static ActionBuilding building;
public static ActionBuilding getInstance(){
if (building==null)building=new ActionBuilding();
return building;
}
private ActionBuilding() { }
private List<ActionDo> actionDos =new ArrayList<>();
public void register(ActionDo actionDo){
if (!actionDos.contains(actionDo)){
actionDos.add(actionDo);
}
}
public void unRegister(ActionDo actionDo){
if (actionDos.contains(actionDo)){
actionDos.remove(actionDo);
}
}
public void setAction(String actionStr,Object o){
for (ActionDo actionDo : actionDos){
actionDo.toDo(actionStr,o);
}
}
}
提供注册监听、注销监听和发出Action的方法,其中ActionDo是一个接口,代码如下:
public interface ActionDo {
void toDo(String actionStr,Object object);
}
基于观察者类,activity就这么写
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MyButton my_button;
private ThisAction allAction;
private ThisAction myButtonAction;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBuilding.getInstance().setAction(Action.main.MAIN_ACTIVITY_ON_CREATE,savedInstanceState);
initView();
}
private void initView() {
my_button=findViewById(R.id.my_button);
my_button.setOnClickListener(this::onClick);
myButtonAction=new ThisAction();
allAction=new ThisAction();
my_button.getAction(myButtonAction);
ActionBuilding.getInstance().register(allAction);
}
@Override
protected void onResume() {
super.onResume();
ActionBuilding.getInstance().setAction(Action.main.MAIN_ACTIVITY_ON_RESUME,null);
}
@Override
protected void onRestart() {
super.onRestart();
ActionBuilding.getInstance().setAction(Action.main.MAIN_ACTIVITY_ON_RESTART,null);
}
@Override
protected void onPause() {
super.onPause();
ActionBuilding.getInstance().setAction(Action.main.MAIN_ACTIVITY_ON_PAUSE,null);
}
@Override
protected void onDestroy() {
super.onDestroy();
ActionBuilding.getInstance().setAction(Action.main.MAIN_ACTIVITY_ON_DESTROY,null);
}
@Override
protected void onStop() {
super.onStop();
ActionBuilding.getInstance().setAction(Action.main.MAIN_ACTIVITY_ON_STOP,null);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.my_button:
ActionBuilding.getInstance().setAction(Action.main.MAIN_ACTIVITY_MY_BUTTON_ON_CLICK,null);
break;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK){
ActionBuilding.getInstance().setAction(Action.main.MAIN_ACTIVITY_KEY_BACK,null);
return false;
}
if (keyCode==KeyEvent.KEYCODE_HOME){
ActionBuilding.getInstance().setAction(Action.main.MAIN_ACTIVITY_KEY_HOME,null);
return false;
}
if (keyCode==KeyEvent.KEYCODE_MENU){
ActionBuilding.getInstance().setAction(Action.main.MAIN_ACTIVITY_KEY_MENU,null);
return false;
}
return super.onKeyDown(keyCode, event);
}
private class ThisAction implements ActionDo {
@Override
public void toDo(String actionStr, Object object) {
switch (actionStr){
case Action.main.UPDATE_MY_BUTTON_VIEW:
my_button.setData((List<String>) object);
break;
case Action.myButtonView.my_button_view_do:
break;
}
}
}
}
所有事件都不在Activity中进行处理,而是通过ActionBuilding(观察者类)把事件都分发出去,包括按钮的点击事件等等。同时Activity中也通过ThisAction 监听ActionBuilding的事件,实现外部操控这个Activity,主要是数据传输给UI刷新这样的逻辑。
然后加一个控制类,实现业务逻辑,代码如下:
public class MainController {
public MainController() {
ActionBuilding.getInstance().register(new ThisAction());
}
private class ThisAction implements ActionDo {
@Override
public void toDo(String actionStr, Object o) {
switch (actionStr){
case Action.main.MAIN_ACTIVITY_ON_CREATE:
CallbackData.getData(new ActionDo() {
@Override
public void toDo(String actionStr, Object object) {
List<String> list=(List<String>) object;
ActionBuilding.getInstance().setAction(Action.main.UPDATE_MY_BUTTON_VIEW,list);
}
});
break;
case Action.main.MAIN_ACTIVITY_ON_RESUME:
break;
case Action.main.MAIN_ACTIVITY_ON_PAUSE:
break;
case Action.main.MAIN_ACTIVITY_ON_RESTART:
break;
case Action.main.MAIN_ACTIVITY_ON_DESTROY:
break;
case Action.main.MAIN_ACTIVITY_ON_STOP:
break;
case Action.main.MAIN_ACTIVITY_KEY_BACK:
break;
case Action.main.MAIN_ACTIVITY_KEY_HOME:
break;
case Action.main.MAIN_ACTIVITY_KEY_MENU:
break;
}
}
}
}
通过这个类拿到事件,并进行各种业务逻辑,例如代码中MAIN_ACTIVITY_ON_CREATE这个事件,这个事件中通过CallbackData.getData获取到一个结果List, 然后这个结果要给谁去处理? 依然是通过ActionBuilding.getInstance().setAction把结果丢出去,谁需要这个结果自己去监听这个事件即可,这里发出的是Action.main.UPDATE_MY_BUTTON_VIEW,也就是被上面的MainActivity领取了,并把这个结果给了my_button这个控件。
......这里省略了MainActivity的其他内容
private class ThisAction implements ActionDo {
@Override
public void toDo(String actionStr, Object object) {
switch (actionStr){
case Action.main.UPDATE_MY_BUTTON_VIEW:
my_button.setData((List<String>) object);
break;
case Action.myButtonView.my_button_view_do:
break;
}
}
}
这样实现我们的代码结构就呈现出这样的效果: aController:接收Action,实现各种业务逻辑,同时把执行结果也当做Action发出去。 aData: 负责获取数据,http请求或者别的数据源。 aView: 就负责把各种触摸事件上报,以及监听刷新指令进行UI渲染。
优点:
- 随时随地轻松获取各种你需要的事件
- 结构分成3层,数据层——控制层——View层,三层之间的交互都依赖“事件”,实现解耦。利于合作开发。
缺点:还请各位大哥指出来。
|