Android设计模式实战---责任链模式
责任链模式实战
在Android中,可以借鉴责任链模式的思想来优化BroadcastReceiver使之成为一个全局的责任链处理者。在BroadcastReceiver的分类中,有序广播是根据优先级依次传播的,指导有接受者将其终止或者所有接受者都不终止它,这与责任链模式很相近。通过它可以轻松实现一种全局的责任链事件处理,这里我们创建了3个BroadcastReceiver:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class FirstReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int limit = intent.getIntExtra( "limit" , - 1001 ); //如果限制值为1000,则处理,否则交给下一个receiver处理 if (limit == 1000 ) { String msg = intent.getStringExtra( "msg" ); Toast.makeText(context,msg,Toast.LENGTH_SHORT).show(); abortBroadcast(); } else { //添加信息发送给下一个Receiver Bundle b = new Bundle(); b.putString( "new" , "Message From FisrtReceiver" ); setResultExtras(b); } } } |
一个接受者能否处理本次广播的唯一条件是看广播中附加权限值是否与自身相等,只有满足limit == 1000 这个条件,才会处理广播并终止广播的继续传递,否则将在广播的intent中在附加一条信息并传播给下一个接收者:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class SecondReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int limit = intent.getIntExtra( "limit" , - 1001 ); //如果限制值为100,则处理,否则交给下一个receiver处理 if (limit == 100 ) { String msg = intent.getStringExtra( "msg" ); Bundle b = getResultExtra( true ); String str = b.getString( "new" ); Toast.makeText(context,msg + "-----" ,Toast.LENGTH_SHORT).show(); abortBroadcast(); } else { //添加信息发送给下一个Receiver Bundle b = new Bundle(); b.putString( "new" , "Message From SecondReceiver" ); setResultExtras(b); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
//第三个Receiver public class ThirdReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int limit = intent.getIntExtra( "limit" , - 1001 ); //如果限制值为10,则处理,否则交给下一个receiver处理 if (limit == 10 ) { String msg = intent.getStringExtra( "msg" ); Bundle b = getResultExtra( true ); String str = b.getString( "new" ); Toast.makeText(context,msg + "-----" ,Toast.LENGTH_SHORT).show(); abortBroadcast(); } else { //添加信息发送给下一个Receiver Bundle b = new Bundle(); b.putString( "new" , "Message From SecondReceiver" ); setResultExtras(b); } } } |
还需要在AndroidManifest中声明这三个Receiver:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<receiver android:name= ".broadcast.order.FirstReceiver" > <intent-filter android:priority= "1000" > </action></intent-filter> </receiver> <receiver android:name= ".broadcast.order.SecondReceiver" > <intent-filter android:priority= "100" > </action></intent-filter> </receiver> <receiver android:name= ".broadcast.order.ThirdReceiver" > <intent-filter android:priority= "10" > </action></intent-filter> </receiver> |
在Activity中发送一个广播:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class OrderActivity extends Activity { @Override protected void onCreate(Bundle saveInstanceState) { super .onCreate(saveInstanceState); setContentView(R.layout.activity_main); Button button = findViewById(R.id.order_send_btn); button.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(); i.setAction( "com.test.action.ORDER_BROADCAST" ); i.putExtra( "limit" , 100 ); i.putExtra( "msg" , "Message from OrderActivity" ); sendOrderdBroadcast(i, null ); } }); } } |
上例中只有SecondReceiver能处理该广播。
总结
责任链模式的优点显而易见,就是可以对请求者和处理者解耦,提高代码的灵活性。
缺点是对链中请求处理者的遍历,如果处理者太多那么遍历必定会影响性能,特别是在一些递归调用中,要慎重。