一个简单的观察者模式例子
来源:互联网 发布:打考勤软件 编辑:程序博客网 时间:2024/05/22 05:29
一:下面的这个类是核心类
public class NotificationCenter {
//static reference for singletonprivate static NotificationCenter _instance;private static NotificationCenter _instance2;private HashMap<String, ArrayList<NotificationDelegate>> registredObjects;//default c'tor for singletonprivate NotificationCenter(){ registredObjects = new HashMap<>();}//returning the referencepublic static synchronized NotificationCenter defaultCenter(){ if(_instance == null) _instance = new NotificationCenter(); return _instance;}//returning the referencepublic 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);
}
}
- 一个简单的观察者模式例子
- 观察者模式简单例子
- 很简单的的观察者模式例子
- 学习观察者模式的一个例子
- Java 观察者模式 简单例子
- 观察者模式简单例子(1)
- Notification简单观察者模式例子
- 观察者模式--一个简单的模拟
- 一个简单观察者模式的实现 cocos2dx
- 设计模式之观察者模式 简单易懂的小例子
- 观察者模式的实现的一个小例子
- JAVA设计模式-观察者模式简单例子
- C#设计模式---观察者模式简单例子
- 一个用观察者模式解决实际问题的例子
- 关于复合模式、策略模式,工厂模式、观察者模式、装饰模式的一个好例子
- 观察者模式的小例子
- Observer模式 的一个简单的例子
- 一个单例模式的简单例子
- JavaMail(2)邮件发送和接收
- 【数据结构与算法】复杂度度量与分析
- Java爬虫框架
- android拍照获得图片及获得图片后并裁剪
- Bitwise AND of Numbers Range
- 一个简单的观察者模式例子
- poj 1068
- ArrayList、Vector、HashMap、HashTable是如何扩容
- Unity3d 5.3.5使用sqlite3
- UVa 1252该类的子集的二进制表示方法还需要学习
- java 反射工具
- hdu 5752 Sqrt Bo (水题)
- hdu 5752 Sqrt Bo(2016 Multi-University Training Contest 3——水题)
- xml的解析