1.前言
我们来看一个场景,某android客户端打开后需要登录,登录后进入主界面,此界面顶部显示用户头像和昵称,某客户需求在点击用户头像后可编辑用户资料。于是,小编就开始着手实施,在实施前小编开始仔细思考了一下,在更新完资料后返回主界面,此时主界面信息没有刷新,如何在资料更新完成后刷新主界面呢?小编毫无疑问,立马想到了用广播通知刷新,但是广播这个东西成本太高,需要在AndroidManifest.xml注册才可以使用,此外小编还想到在主界面的onResume方法中去请求后台数据更新主界面用户信息,但网络操作势必浪费时间,所以,灵机一动,此时此刻这个地方不正是用观察者模式的好地方吗?
2.实现方法
首先我们定义观察者接口BusinessObserver
public abstract interface BusinessObserver {
public abstract void onUpdate(int what, Object o);
}
然后创建账号观察者AccountObserver并实现接口
public class AccountObserver implements BusinessObserver {
private static final String TAG = "AccountObserver";
@Override
public void onUpdate(int what,Object o) {
switch (what) {
case 1:
onUpdateAccountName((Account)o);
break;
default:
break;
}
}
protected void onUpdateAccountName(Account account) {}
}
其中what我们用来判断是修改姓名呢,还是修改头像,并定义onUpdateAccountName方法,但此方法是空方法,后续我们会创建一个类去继承AccountObserver ,并实现onUpdateAccountName方法
接着我们创建BusinessHandler抽象类,用它去通知所有的观察者对象
public abstract class BusinessHandler {
private Operator mOperator;
public BusinessHandler(Operator operator){
mOperator=operator;
}
public void notifyObservers(int what, Object o) {
synchronized (mOperator.mObservers) {
Iterator iterator = mOperator.mObservers.iterator();
while (iterator.hasNext()) {
BusinessObserver observer = iterator.next();
if (observer != null) {
observer.onUpdate(what, o);
}
}
}
}
}
其中Operator类是全局类,用于全局管理所有的观察者,此外为了进一步降低耦合,我们继续在Operator类中添加BusinessHandler集合,此集合除了能够添加AccountHandler外还可以添加FriendListHandler等等。
public class Operator {
private static final String TAG="Operator";
public static final int ACCOUNT_HANDLER = 1;
List mObservers = new ArrayList();
private SparseArray mHandlers = new SparseArray();
public Operator(){
}
public void addObserver(BusinessObserver observer){
if (!mObservers.contains(observer)) {
mObservers.add(observer);
}
}
public void removeObserver(BusinessObserver observer){
mObservers.remove(observer);
}
public BusinessHandler getHandler(int type){
synchronized (mHandlers) {
BusinessHandler handler = createHandler(type);
if (handler != null) {
mHandlers.put(type, handler);
}
return handler;
}
}
public BusinessHandler createHandler(int type){
Log.e(TAG,"begin createHandler " + type);
BusinessHandler handler=null;
switch (type) {
case ACCOUNT_HANDLER:
handler=new AccountHandler(this);
break;
default:
break;
}
Log.e(TAG,"end createHandler " + type);
return handler;
}
}
接下来我们继续创建AccountHandler类继承BusinessHandler
public class AccountHandler extends BusinessHandler {
private static final String TAG = "FriendListHandler";
public AccountHandler(Operator appInterface) {
super(appInterface);
}
public void updateAccountName(String name) {
Log.d(TAG,"更新账号信息");
Account friend=new Account();
friend.name=name;
notifyObservers(1,friend);
}
}
public class MainActivity extends BaseActivity {
private static final String TAG="MainActivity";
private AccountObserver mObserver;
private TextView mTxtName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mObserver=new AccountObserverImp();
mOperator.addObserver(mObserver);
((Button)findViewById(R.id.btn_jump)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,UpdateUserActivity.class);
startActivity(intent);
}
});
mTxtName=(TextView)findViewById(R.id.txt_name);
}
@Override
protected void onDestroy() {
super.onDestroy();
mOperator.removeObserver(mObserver);
}
public class AccountObserverImp extends AccountObserver{
@Override
protected void onUpdateAccountName(Account account) {
Log.d(TAG,"通知:账号信息更新了,姓名: "+account.name+" threadName:"+Thread.currentThread().getName());
mTxtName.setText(account.name);
}
}
}
public class UpdateUserActivity extends BaseActivity{
private String TAG="AddFrinedActivity";
private AccountObserver mObserver;
private TextView mTxtName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_user);
mObserver=new AccountObserverImp();
mOperator.addObserver(mObserver);
mTxtName=(TextView)findViewById(R.id.txt_name);
((Button)findViewById(R.id.btn_test)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
updateAccountName("张三");
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
mOperator.removeObserver(mObserver);
}
private void updateAccountName(String name){
AccountHandler handler = (AccountHandler)mOperator.getHandler(Operator.ACCOUNT_HANDLER);
handler.updateAccountName(name);
}
public class AccountObserverImp extends AccountObserver{
@Override
protected void onUpdateAccountName(Account account) {
Log.d(TAG,"通知:账号信息更新了,姓名: "+account.name+" threadName:"+Thread.currentThread().getName());
mTxtName.setText(account.name);
}
}
}
在UpdateUserActivity中,我们来更新用户名,此时MainActivty虽然已经处于OnStop状态,但是我们同样能够即时更新到UI界面,可以极大的提升用户体验,降低程序耦合
1.MainActivty初始界面如下
2.当我们点击跳转到UpdateUserActivtiy中点击测试按钮后,效果如下图所示,此时账号名称被修改为“张三”
3.此时我们返回MainActivity中发现名字已变为"张三"
4.日志打印结果如下
PS:所有的设计模式有利有弊,此模式的优点就是降低代码耦合,并且可以提高用户体验,缺点就是需要建很多类,使用前需要根据实际的项目需要来衡量,当然,如上需求使用观察者模式是最好的选择。
3.下载
http://download.csdn.net/detail/cc_want/9497109
下载需要消耗1积分,不多不少,评论一下立马赚回,谢谢支持!