otto框架使多个类之间解耦
最近发现了一个很有趣的框架,可以用来使得多个类之间解耦。看起来十分玄乎,其实举个例子就明白了。比如,一个聊天应用,服务器推送来了一条信息,信息通知栏会出现未读通知,但是接收信息的类和显示通知的类是不同的类,如果显示通知的类使用接受信息的类的方法,那么这两个类就耦合了,显然这是不好的,我们应该避免这种耦合,这时候,otto框架就派上用场了!咦,怎么感觉broadcastreceiver也是做这种事的呢!是的,broadcastreceiver也可以做到,但是很多场合,从开发效率,设计思路上来说,otto都要优于broadcastreceiver,所以otto是非常值得学习的,最近看了一下关于otto的资料,发现好多看不懂,后来自己看看官方的文档和其他人的学习资料,然后自己总结一下,然后自己写一个demo。
导入方法:eclipse下载jar包,然后导入。
android studio在build.gradle中加上compile 'com.squareup:otto:+'
一,Bus建立单例
官方文档这么说:如果Bus是被分享的,它才能表现出它的效率,我们推荐通过注入或者其他合适的机制获得Bus的实例。
Bus类就是这个框架主要的类,用于发布消息,注册订阅,注销订阅等,官方文档这么说,也就是推荐我们把Bus做出单例。代码如下:
public class BusSingle {
private static final Bus bus = new Bus(ThreadEnforcer.MAIN);
public static Bus getInstance(){
return bus;
}
private BusSingle(){}
}
单例,谁都看得懂,唯一疑惑的是ThreadEnforce.Main到底是什么呢?其实看字面意思也可以看出,就是这个单例只能在主线程上,ThreadEnforce还有另一个参数Any,这个参数允许任意线程。二,订阅事件
首先是布局文件,很简单,一个按钮就可以了,代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next" />
</RelativeLayout>
这个activity主要是订阅事件,接收到另外一个activity的事件,它就响应,并打印log,按钮一按,就跳到发布事件的activity。代码如下:
package com.example.admin.studypercent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;
public class MainActivity extends ActionBarActivity {
private Bus bus = BusSingle.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bus.register(this);
findViewById(R.id.textview).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,SecondActivity.class));
}
});
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
bus.unregister(this);
}
@Subscribe
public void sayEventOccur(String event) {
Log.e("event", "mainactivity" + event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
bus实例一定要注册,activity被销毁的时候要注销bus,代码
@Subscribe
public void sayEventOccur(String event) {
Log.e("event", "mainactivity" + event);
}
就是收到事件作出响应的函数,如果控制台打印log说明成功了。三,发布事件
发布事件的activity布局和第一个activity基本一样,代码如下:、<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="occur" />
</RelativeLayout>
然后就可以看看发布的方法了,很简单,直接获得bus实例,然后使用post方法就可以了,代码如下:package com.example.admin.studypercent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.squareup.otto.Bus;
public class SecondActivity extends ActionBarActivity {
private Bus bus = BusSingle.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
findViewById(R.id.textview).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bus.post("hello");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
四,运作流程
首先mainactivity注册bus,并有一个注解函数,用于响应事件发生,然后mainactivity跳转到secondactivity,按钮点击触发事件,失去焦点的mainactivity响应,调用注解函数,打印日志,关闭程序,mainactivity销毁的之前bus注销,程序结束。
五,效果
六,总结
这是个不错的框架,使得类之间解耦,值得学习,是个不错的框架。
demo下载地址:http://download.youkuaiyun.com/detail/u013013970/8894557