观察者模式是一种一对多的对象行为模式,当被观察者改变时,所有依赖于他的观察者都会自动更新。常见例子比如Android中的button.setOnclickListener(),broadcastReceiver, rxJava等等。这里通过一个自定义观察者模式来看:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 interface Observer { fun update (observable: Observable , objects: Any ) }open class Observable { private var list: ArrayList<Observer> = ArrayList<Observer>() fun addObserver (observe: Observer ) { observe?.apply { list.add(observe) } } fun deleteObserver (observe: Observer ) { list?.apply { if (list.size > 0 ) { list.remove(observe) } } } fun notifyObservers (obj: Any ) { for (element in list) { element.update(this , obj) } } }class FishMan (var name: String) : Observer { override fun update (observable: Observable , objects: Any ) { println("$name observed this fish is eating $objects " ) } }class Shark (var name: String) : Observer { override fun update (observable: Observable , objects: Any ) { println("$name observed this fish is eating $objects " ) } }class Fish (var name: String) : Observable() { fun eating (food: String ) { notifyObservers(food) } fun toStrings () : String { return "Fish" } }fun main () { println("hi is me" ) var fish = Fish("smallFish" ) var fishManJack = FishMan("Jack" ) var fishManTonny = FishMan("Tonny" ) var fishManMash = FishMan("Mash" ) var shark = Shark("Onil" ) fish.addObserver(fishManJack) fish.addObserver(fishManTonny) fish.addObserver(fishManMash) fish.addObserver(shark) fish.deleteObserver(fishManJack) fish.eating("smallFish" ) }
1、抽象的observer接口,方便多个观察者调用;
2、class observable存储所有的观察者对象,有list存储观察者,addObserver()添加观察者,deleteObservable()删除观察者,notifyObserver()将list中的observers.notify(), notify()是抽象后的接口, 实现的方法体在每个具体的观察者如fishMan、shark等类中。
3、init Observable Fish, init Observers Fishman and Shark, 将观察者add进去被观察者父类中,然后调用被观察者eating方法,到Observable中的notifyObservers(), 再到Observer中的update(), 最后到达FishMan和Shark中的update()。