当对象间存在一对多关系时,则使用观察者模式(Observer Pattern)。比如,当一个对象被修改时,则会自动通知它的依赖对象。 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。
1. 观察者模式
package com.andrew.pattern0303.observer.model01; import java.util.ArrayList; import java.util.List; public class Subject { private Listobservers = new ArrayList (); private int state; public int getState() { return state; } public void setState(int state) { this.state = state; notifyAllObservers(); } public void attach(Observer observer) { observers.add(observer); } public void notifyAllObservers() { for (Observer observer : observers) { observer.update(); } } }
package com.andrew.pattern0303.observer.model01; public abstract class Observer { protected Subject subject; public abstract void update(); }
package com.andrew.pattern0303.observer.model01; public class BinaryObserver extends Observer { public BinaryObserver(Subject subject) { this.subject = subject; this.subject.attach(this); } @Override public void update() { System.out.println("Binary String: " + Integer.toBinaryString(subject.getState())); } } package com.andrew.pattern0303.observer.model01; public class OctalObserver extends Observer { public OctalObserver(Subject subject) { this.subject = subject; this.subject.attach(this); } @Override public void update() { System.out.println("Octal String: " + Integer.toOctalString(subject.getState())); } } package com.andrew.pattern0303.observer.model01; public class HexaObserver extends Observer { public HexaObserver(Subject subject) { this.subject = subject; this.subject.attach(this); } @Override public void update() { System.out.println("Hex String: " + Integer.toHexString(subject.getState())); } }
package com.andrew.pattern0303.observer.model01; /** * 1. 观察者模式 * * @author andrew * @date 2018/08/18 */ public class Client { public static void main(String[] args) { Subject subject = new Subject(); new HexaObserver(subject); new OctalObserver(subject); new BinaryObserver(subject); System.out.println("First state change: 15"); subject.setState(15); System.out.println("Second state change: 10"); subject.setState(10); } } 运行结果: First state change: 15 Hex String: f Octal String: 17 Binary String: 1111 Second state change: 10 Hex String: a Octal String: 12 Binary String: 1010
2. 观察者模式
package com.andrew.pattern0303.observer.model02; public interface Watched { public void addWatcher(Watcher watcher); public void removeWatcher(Watcher watcher); public void notifyWatchers(String str); } package com.andrew.pattern0303.observer.model02; import java.util.ArrayList; import java.util.List; public class ConcreteWatched implements Watched { private Listlist = new ArrayList (); @Override public void addWatcher(Watcher watcher) { list.add(watcher); } @Override public void removeWatcher(Watcher watcher) { list.remove(watcher); } @Override public void notifyWatchers(String str) { for (Watcher watcher : list) { watcher.update(str); } } }
package com.andrew.pattern0303.observer.model02; public interface Watcher { public void update(String str); } package com.andrew.pattern0303.observer.model02; public class ConcreteWatcher implements Watcher { @Override public void update(String str) { System.out.println(str); } }
package com.andrew.pattern0303.observer.model02; /** * 2. 观察者模式 * * @author andrew * @date 2018/08/18 */ public class Client { public static void main(String[] args) { Watched girl = new ConcreteWatched(); Watcher watcher1 = new ConcreteWatcher(); Watcher watcher2 = new ConcreteWatcher(); Watcher watcher3 = new ConcreteWatcher(); girl.addWatcher(watcher1); girl.addWatcher(watcher2); girl.addWatcher(watcher3); girl.notifyWatchers("开心,爽"); girl.removeWatcher(watcher2); girl.notifyWatchers("不爽"); } } 运行结果: 开心,爽 开心,爽 开心,爽 不爽 不爽