1:类图的实现
2:观察者模式的分析
针对,上面的类图我们详细讲解一下观察者模式的概念:观察者模式是一种一对多的依赖关系,让多个观察者对象能够监听同一个对象,只要该对象发生变化后,所有观察者都可以知道,使观察者能够更新自己的状态。
Subject类:该类型为通知者,把所有观察者都装进自己的内部的容器里,这个容器可以有一定数量的观察者,允许一定的删除和添加,该通知者发生变化时,Subject类型中有notifly方法来同时观察者。
Observer类:该类为观察者,即每一个需要被通知者通知的对象都要实现该方法,同时实现接口里的Update方法,供Subject通知类使用来通知观察者。
3:代码实现
package observerPattern; /** * @author :dazhu * @date :Created in 2020/4/4 7:49 * @description: * @modified By: * @version: $ */ public class Main { public static void main(String[]args){ Observer o1 = new Observer1("张三"); Observer o2 = new Observer2("李四"); Observer o3 = new Observer2("王二"); Subject s1 = new Subject1("小梅"); Subject s2 = new Subject2("小红"); s1.addObserver(o1); s1.addObserver(o2); s2.addObserver(o3); s2.addObserver(o2); s1.notiflyObserver(); o1.showMe(); o2.showMe(); o3.showMe(); s2.notiflyObserver(); o1.showMe(); o2.showMe(); o3.showMe(); } } package observerPattern; public interface Observer { public void update(int number); public void showMe(); } package observerPattern; import java.util.ArrayList; import java.util.List; public interface Subject { //提供添加观察者 public void addObserver(Observer observer); //删除观察者 public void delObserver(Observer observer); //通知观察者 public void notiflyObserver(); } package observerPattern; import java.util.ArrayList; import java.util.List; /** * @author :dazhu * @date :Created in 2020/4/4 7:51 * @description:1号通信员 * @modified By: * @version: $ */ public class Subject1 implements Subject{ private String name = null; private List<Observer> list = new ArrayList<>(); public Subject1(String name){ this.name = name; } @Override public void addObserver(Observer observer) { list.add(observer); } @Override public void delObserver(Observer observer) { list.remove(observer); } @Override public void notiflyObserver() { for(Observer o:list){ o.update(100); } } } package observerPattern; import java.util.ArrayList; import java.util.List; /** * @author :dazhu * @date :Created in 2020/4/4 7:53 * @description:2号通信员 * @modified By: * @version: $ */ public class Subject2 implements Subject { private String name = null; private List<Observer> list = new ArrayList<>(); public Subject2(String name){ this.name = name; } @Override public void addObserver(Observer observer) { list.add(observer); } @Override public void delObserver(Observer observer) { list.remove(observer); } @Override public void notiflyObserver() { for(Observer o:list){ o.update(200); } } } package observerPattern; /** * @author :dazhu * @date :Created in 2020/4/4 7:50 * @description:1号观察者 * @modified By: * @version: $ */ public class Observer1 implements Observer{ private String name = null; public int number = 0; public Observer1(String name){ this.name = name; } @Override public void update(int number) { this.number = number; } @Override public void showMe() { System.out.println(number); } } package observerPattern; /** * @author :dazhu * @date :Created in 2020/4/4 7:51 * @description:2号观察者 * @modified By: * @version: $ */ public class Observer2 implements Observer{ private String name = null; public int number = 0; public Observer2(String name){ this.name = name; } @Override public void update(int number) { this.number = number; } @Override public void showMe() { System.out.println(number); } } package observerPattern; /** * @author :dazhu * @date :Created in 2020/4/4 7:51 * @description:3号观察者 * @modified By: * @version: $ */ public class Observer3 implements Observer { private String name = null; public int number = 0; public Observer3(String name){ this.name = name; } @Override public void update(int number) { this.number = number; } @Override public void showMe() { System.out.println(number); } }
来源:https://www.cnblogs.com/dazhu123/p/12630328.html