一起来写一个类似于EventBus的东西

这边文章写在睡觉前,所以我会写的很快。
首先我没用过EventBus,但是我看过它的使用介绍。
大概是这样用的:

EventBus.getDefault().register(this);
EventBus.getDefault().post(messageEvent);
EventBus.getDefault().unregister(this);

好的,一个非常经典的观察者模式。
注册,发送事件,取消注册。

然后我们自己来实现一下。

首先我们写一个自己的MyEventBus:

public class MyEventBus {
    static MyEventBus myEventBus;
    static List<EventListener> listeners = new ArrayList<>();

    private MyEventBus(){

    }

    public static MyEventBus getDefault(){
        if(myEventBus==null){
            return new MyEventBus();
        }else {
            return myEventBus;
        }
    }

    public void register(EventListener eventListener){
        listeners.add(eventListener);
    }

    public void unregister(EventListener eventListener){
        listeners.remove(eventListener);
    }

    public void post(String message){
        for(EventListener eventListener:listeners){
            eventListener.getMessage(message);
        }
    }
}

MyEventBus是个单例,有个静态的List<EventListener>管理注册的类。还有个post方法用来发送消息。

EventListener如下:

public interface EventListener {
    void getMessage(String message);
}

一个接口,有个getMessage方法用来接收post的信息。

MainActivity:

public class MainActivity extends AppCompatActivity implements EventListener{

    TextView textView;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.text_view);
        button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);
            }
        });
        MyEventBus.getDefault().register(this);
    }

    @Override
    public void getMessage(String message) {
        textView.setText(message);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyEventBus.getDefault().unregister(this);
    }
}

SecondActivity:

public class SecondActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        Button button = findViewById(R.id.send_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyEventBus.getDefault().post("你好呀,MainActivity");
                finish();
            }
        });
    }
}

MainActivity注册到了MyEventBus的List中,一旦调用了post方法,MainActivity的getMessage(String message)就会收到post过来的方法。

SecondActivity中调用了post方法,它会把信息发给所有注册者,在这里就是我们的MainActivity。

效果如下:


blogtest.gif

总结:

以上就是EventBus的超超超简化版,很简单对不对,不过EventBus的源码当然不会这么简单,但是我觉得核心思想就是观察者模式。

对一个开源框架,我们要了解它的内涵,而不是拿来用,觉得哇好帅啊,就完事了。我们可以看看它的介绍,然后想想如果是自己去写一个类似的框架,你会怎么写。

就是这样,晚安~

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,868评论 18 139
  • 项目到了一定阶段会出现一种甜蜜的负担:业务的不断发展与人员的流动性越来越大,代码维护与测试回归流程越来越繁琐。这个...
    fdacc6a1e764阅读 3,209评论 0 6
  • 目录 1.概述 2.实战 1.基本框架搭建 2.新建一个类FirstEvent 3.在要接收消息的页面注册Even...
    慕涵盛华阅读 10,526评论 2 16
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,024评论 25 708
  • 七律/大花芦莉 作者:心博、图片:网络 此君实谓品尤佳,一年四季显其华。 椭圆绿叶颜如玉,五瓣桃红色比霞。 单朵芬...
    心博1阅读 446评论 0 3