簡單的說,當Observable(被觀察者)發生變化的時候,會通知Observer(觀察者)。
以生活例子來說,你追蹤了其中一個粉絲頁面,你就是觀察者,而粉絲頁面是被觀察者,
當粉絲頁(被觀察者)新增了文章,也就是發生變化時,會通知你(觀察者)。
大概知道概念後,就可以來試著實作出來了。
首先先新增一個Singleton Class,名字隨便取一個。
然後這個Class要繼承Observable,此時你的程式碼應該會像下面這樣。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyObservable extends Observable{ | |
private static MyObservable ourInstance = new MyObservable(); | |
public static MyObservable getInstance() { | |
return ourInstance; | |
} | |
private MyObservable() { | |
} | |
} |
以剛才生活例子來講,你追隨很多個同一個粉絲團(?,聽起來就不太對。
接著我們加入一些簡短的程式碼,有一個屬性可以讓其他人設定,當值改變的時候做一些事情。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyObservable extends Observable{ | |
private static MyObservable ourInstance = new MyObservable(); | |
public static MyObservable getInstance() { | |
return ourInstance; | |
} | |
private MyObservable() { | |
} | |
private int data = 0; | |
public void setData(int data){ | |
if(this.data != data){ | |
//假如資料有變動 | |
this.data = data; | |
setChanged(); | |
//設定有改變 | |
} | |
notifyObservers(); | |
//發送給觀察者 | |
} | |
} |
setChanged,簡單的說就是產生變化,上面有提到,當被觀察者產生變化時,要通知觀察者,當你呼叫這個方法的時候,就是產生變化了。
notifyObservers,通知觀察者。
你可能會想,我直接用notifyObservers通知觀察者就好了,為啥還要多設定一個產生變化,此時我們可以來看一下觀察者的程式碼該如何實現。
我們先在MainActivity之中implements Observer。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends Activity implements Observer |
這麼一來我們多了一個方法,update,也就是更新,當被觀察者產生變化時,會在這邊收到訊息,因此上面必須先setChanged,這邊才會收到訊息。
接著我們試著把觀察這個被觀察者吧。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MyObservable.getInstance().addObserver(this); | |
//加入觀察者 | |
MyObservable.getInstance().setData(1); | |
MyObservable.getInstance().setData(1); | |
MyObservable.getInstance().setData(2); | |
//設定資料 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void update(Observable observable, Object o) { | |
Log.d("tag" , "MyObservable產生變化"); | |
} |
然後試著輸入了三個值,根據判斷,當值改變的時候會呼叫setChanged,
最後看看update是否印出與想像中相同的Log。
事實上它會印出兩次Log,因為setChanged只被呼叫了兩次。
如此一來你應該對觀察者與被觀察者有基本的認識了。
沒有留言:
張貼留言