观察者模式(Observer)(行为模式)

xiaoxiao2021-02-28  22

参考文档:

1.设计模式-可复用面向对象软件的基础

2.http://blog.csdn.net/zhshulin/article/details/38708351(设计模式——观察者模式(Observer) 

观察者模式(Observer)(行为模式) 

详细的理论方面的知识,可以参考  参考文档。

看一下observer的结构:

协作方式:

如果,在实现是,需要封装复杂的更新语义,可以使用Mediator(5.5)模式,ChangeManager是一个Mediator(5.5)模式的实例。

代码实现,案例为书中讲的时钟的案例。

Subject.java: package com.rick.designpattern.observer; import java.util.ArrayList; import java.util.List; /** * Created by MyPC on 2017/6/19. */ public abstract class Subject { private List<Observer> observers = new ArrayList<Observer>(); public void attach(Observer observer) { observers.add(observer); } public void detach(Observer observer) { observers.remove(observer); } public void Notify() { for (Observer os : observers) { os.update(this); } } } ClockTimer.java: package com.rick.designpattern.observer; import javax.xml.crypto.Data; import java.text.SimpleDateFormat; import java.util.Date; /** * Created by MyPC on 2017/6/19. */ public class ClockTimer extends Subject { private int hour, minute, second; public ClockTimer() { } public int getHour() { return hour; } public int getMinute() { return minute; } public int getSecond() { return second; } public void tick() { new Thread(new Runnable() { @Override public void run() { while (true) { Date date = new Date(System.currentTimeMillis()); hour = date.getHours(); minute = date.getMinutes(); second = date.getSeconds(); ClockTimer.this.Notify(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }).run(); } } Observer.java: package com.rick.designpattern.observer; /** * Created by MyPC on 2017/6/19. */ public abstract class Observer { public abstract void update(Subject subject); } AnalogClock.java: package com.rick.designpattern.observer; /** * Created by MyPC on 2017/6/19. */ public class AnalogClock extends Observer implements Widget { private ClockTimer subject; public AnalogClock(ClockTimer subject) { this.subject = subject; subject.attach(this); } public void detachSubject() { subject.detach(this); } @Override public void draw() { System.out.println("AnalogClock:当前时间:" + subject.getHour() + ":" + subject.getMinute() + ":" + subject.getSecond()); } @Override public void update(Subject subject) { if (subject instanceof ClockTimer) { draw(); } } } DigitalClock.java: package com.rick.designpattern.observer; /** * Created by MyPC on 2017/6/19. */ public class DigitalClock extends Observer implements Widget { private ClockTimer subject; public DigitalClock(ClockTimer subject) { this.subject = subject; subject.attach(this); } public void detachSubject() { subject.detach(this); } @Override public void draw() { System.out.println("DigitalClock:当前时间:" + subject.getHour() + ":" + subject.getMinute() + ":" + subject.getSecond()); } @Override public void update(Subject subject) { if (subject instanceof ClockTimer) { draw(); } } } Widget.java: package com.rick.designpattern.observer; /** * Created by MyPC on 2017/6/19. */ public interface Widget { public void draw(); } Client.java: package com.rick.designpattern.observer; /** * Created by MyPC on 2017/6/19. */ public class Client { public static void main(String[] args) { ClockTimer clockTimer = new ClockTimer(); DigitalClock digitalClock = new DigitalClock(clockTimer); AnalogClock analogClock = new AnalogClock(clockTimer); clockTimer.tick(); } }

转载请注明原文地址: https://www.6miu.com/read-450198.html

最新回复(0)