一個簡單的觀察者模式例子
一:下面的這個類是核心類
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);
}
}
相關文章
- 最簡單的通知方式 - 觀察者模式模式
- 簡單講解觀察者設計模式設計模式
- 擼一個簡單的MVVM例子MVVM
- 5分鐘通過一個例子理解觀察者模式和釋出訂閱模式的區別模式
- 簡說設計模式——觀察者模式設計模式
- 極簡設計模式-觀察者模式設計模式
- 一個最簡單的 Github workflow 例子Github
- 一個簡單的Ajax請求例子
- 設計模式-單例模式,觀察者模式設計模式單例
- 求教一個觀察者模式應用的問題模式
- JUnit概述及一個簡單例子單例
- WebRTC:一個視訊聊天的簡單例子Web單例
- 一個簡單的例子教會您使用javapJava
- 一個簡單的spring-boot例子Springboot
- 一個簡單的例子帶你理解HashmapHashMap
- 一個閉包函式的簡單例子函式單例
- 【程式碼簡述設計模式】----- 觀察者模式設計模式
- 設計模式之觀察者模式(一)設計模式
- Java中的設計模式(一):觀察者模式Java設計模式
- spring攔截器的一個簡單例子Spring單例
- 一個插排引發的設計思想 (一) 觀察者模式模式
- 觀察者模式模式
- Java多型的一個簡單入門的例子Java多型
- Spring-Context之一:一個簡單的例子SpringContext
- 一個簡單的MVP模式案例MVP模式
- 記一次觀察者模式的使用模式
- php mysql 一個查詢優化的簡單例子PHPMySql優化單例
- linux c 一個autotools的最簡單例子Linux單例
- 一個簡單的oracle函式返回陣列的例子Oracle函式陣列
- 舉兩個例子以快速明白Java中的簡單工廠模式Java模式
- PHP觀察者模式PHP模式
- Unity——觀察者模式Unity模式
- 觀察者模式(2)模式
- Java 觀察者模式Java模式
- JS 觀察者模式JS模式
- 設計模式走一遍---觀察者模式設計模式
- 進擊的觀察者模式模式
- ListView中的觀察者模式View模式