一个简单的观察者模式例子

一:下面的这个类是核心类
public class NotificationCenter {

//static reference for singleton
private static NotificationCenter _instance;
private static NotificationCenter _instance2;
private HashMap<String, ArrayList<NotificationDelegate>> registredObjects;

//default c'tor for singleton
private NotificationCenter(){
    registredObjects = new HashMap<>();
}
//returning the reference
public static synchronized NotificationCenter defaultCenter(){
    if(_instance == null)
        _instance = new NotificationCenter();
    return _instance;
}
//returning the reference
public static synchronized NotificationCenter defaultCenterNew(){
    if(_instance2 == null)
        _instance2 = new NotificationCenter();
    return _instance2;
}

public synchronized void addFucntionForNotification(String notificationName, NotificationDelegate delegate){
    ArrayList<NotificationDelegate> list = registredObjects.get(notificationName);
    if(list == null) {
        list = new ArrayList<>();
        registredObjects.put(notificationName, list);
    }
    list.add(delegate);
}

public synchronized void removeFucntionForNotification(String notificationName, NotificationDelegate r){
    if(registredObjects == null){
        return ;
    }
    ArrayList<NotificationDelegate> list = registredObjects.get(notificationName);
    if(list != null) {
        list.remove(r);
    }
}

public synchronized void postNotificationName(String notificationName, Object obj){
    ArrayList<NotificationDelegate> list = registredObjects.get(notificationName);

// LogUtils.showLog(“==========”,” ” + list);
if(list != null) {
for(NotificationDelegate r: list){
r.update(notificationName,obj);
// LogUtils.showLog(“==========”,” upadate “);
}

    }
}

// public synchronized void postNotification(Object obj){
// for(String name: registredObjects.keySet()){
// ArrayList list = registredObjects.get(name);
// if(list!= null){
// for(NotificationDelegate r: list)
// r.update(“”,obj);
// }
// }
// }

}
二:
/**
* Created by dongwanlin on 2016/6/2.
*/
public interface NotificationDelegate {
public void update(String name, Object obj);
}

三:
( 1 )发送
SmmApplication.center.postNotificationName(FinalConstant.AutoQuotation_Finish, categoryId);
(2)接收
private void registerListener() {
final Gson gson = new Gson();
// 实时更新监听器
delegate = new NotificationDelegate() {
@Override
public void update(String name, Object obj) {
if (name.equals(FinalConstant.AutoQuotation_Finish)) {
s_page = 1;
getData();
}
}
};
if (SmmApplication.center != null) { SmmApplication.center.addFucntionForNotification(FinalConstant.AutoQuotation_Finish,delegate);
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (SmmApplication.center != null) {
SmmApplication.center.removeFucntionForNotification(FinalConstant.AutoQuotation_Finish, delegate);
}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值