- package cn.lxl.observe;
- /**
- * Created by IntelliJ IDEA.
- * User: Administrator
- * Date: 2011-11-11
- * Time: 15:30:25
- * To change this template use File | Settings | File Templates.
- */
- //抽象主题
- public interface AbstractMotif {
- public void addAbstractMotif(AbstractWatcher abstractWatcher);
- public void removeAbstractMotif(AbstractWatcher abstractWatcher);
- public void notifyAbstractMotif(String message);
- }
- package cn.lxl.observe;
- /**
- * Created by IntelliJ IDEA.
- * User: Administrator
- * Date: 2011-11-11
- * Time: 15:33:19
- * To change this template use File | Settings | File Templates.
- */
- public interface AbstractWatcher {
- public void update(String message);
- }
- package cn.lxl.observe;
- /**
- * Created by IntelliJ IDEA.
- * User: Administrator
- * Date: 2011-11-11
- * Time: 15:46:47
- * To change this template use File | Settings | File Templates.
- */
- public class ConWatcher implements AbstractWatcher {
- // 定义每一个观察者名字
- private String name;
- public ConWatcher(String name) {
- this.name = name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return this.name;
- }
- //接受信息
- public void update(String message) {
- System.out.println(message);
- }
- }
- package cn.lxl.observe;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by IntelliJ IDEA.
- * User: Administrator
- * Date: 2011-11-11
- * Time: 15:31:26
- * To change this template use File | Settings | File Templates.
- */
- //具体主题
- public class ConMotif implements AbstractMotif {
- //观察都列表
- List<AbstractWatcher> list = new ArrayList<AbstractWatcher>();
- // 增加观察者
- @Override
- public void addAbstractMotif(AbstractWatcher abstractWatcher) {
- list.add(abstractWatcher);
- ConWatcher con = (ConWatcher) abstractWatcher;
- String conName = con.getName();
- System.out.println("增加了一个新观察者..." + conName);
- }
- //删除观察者
- @Override
- public void removeAbstractMotif(AbstractWatcher abstractWatcher) {
- list.remove(abstractWatcher);
- ConWatcher con = (ConWatcher) abstractWatcher;
- String conName = con.getName();
- System.out.println("删除了一个新观察者..." + conName);
- }
- //观察者发送信息
- @Override
- public void notifyAbstractMotif(String message) {
- for (AbstractWatcher abstractWatcher : list) {
- ConWatcher con = (ConWatcher) abstractWatcher;
- String conName = con.getName();
- abstractWatcher.update("现在还有:" + conName + "在" + message);
- }
- }
- }
- package cn.lxl.observe;
- /**
- * Created by IntelliJ IDEA.
- * User: Administrator
- * Date: 2011-11-11
- * Time: 15:48:29
- * To change this template use File | Settings | File Templates.
- */
- public class Test {
- public static void main(String[] args) {
- //生成一个具体的被观察对象
- ConMotif conMotif = new ConMotif();
- //生成观察者张三
- ConWatcher zsConWatcher = new ConWatcher("张三");
- //生成观察者李四
- ConWatcher lsConWatcher = new ConWatcher("李四");
- //生成观察者王五
- ConWatcher wwConWatcher = new ConWatcher("王五");
- //观察者观察具休珠对象
- conMotif.addAbstractMotif(zsConWatcher);
- conMotif.addAbstractMotif(lsConWatcher);
- conMotif.addAbstractMotif(wwConWatcher);
- conMotif.removeAbstractMotif(wwConWatcher);
- conMotif.notifyAbstractMotif("观察我");
- }
- }