观察器(Observer)
来源:互联网 发布:窥一叶而知秋 编辑:程序博客网 时间:2024/04/30 06:59
1 Observable类用于跟踪那些当发生一个改变时希望收到通知的所有个体——无论“状态”是否改变。如果有人说“好了,所有人都要检查自己,并可能要进行更新”,那么Observable类会执行这个任务——为列表中的每个“人”都调用notifyObservers()方法。notifyObservers()方法属于基础类Observable的一部分。
2 Observable对象会自动调用每个Observer对象的update()方法。
3 为真正产生效果,必须从Observable继承,并在衍生类代码的某个地方调用setChanged()。这个方法需要设置“changed”(已改变)标志,它意味着当调用notifyObservers()的时候,所有观察器事实上都会收到通知。
4 通过notifyObservers()和update()中的代码的结合,可以应付一些非常复杂的局面。
import java.awt.*;import java.awt.event.*;import java.util.*;// You must inherit a new type of Observable:class BoxObservable extends Observable { public void notifyObservers(Object b) { // Otherwise it won't propagate changes: setChanged(); super.notifyObservers(b); }}public class BoxObserver extends Frame { Observable notifier = new BoxObservable(); public BoxObserver(int grid) { setTitle("Demonstrates Observer pattern"); setLayout(new GridLayout(grid, grid)); for(int x = 0; x < grid; x++) for(int y = 0; y < grid; y++) add(new OCBox(x, y, notifier)); } public static void main(String[] args) { int grid = 8; if(args.length > 0) grid = Integer.parseInt(args[0]); Frame f = new BoxObserver(grid); f.setSize(500, 400); f.setVisible(true); f.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); }}class OCBox extends Canvas implements Observer { Observable notifier; int x, y; // Locations in grid Color cColor = newColor(); static final Color[] colors = { Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green, Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow }; static final Color newColor() { return colors[ (int)(Math.random() * colors.length) ]; } OCBox(int x, int y, Observable notifier) { this.x = x; this.y = y; notifier.addObserver(this); this.notifier = notifier; addMouseListener(new ML()); } public void paint(Graphics g) { g.setColor(cColor); Dimension s = getSize(); g.fillRect(0, 0, s.width, s.height); } class ML extends MouseAdapter { public void mousePressed(MouseEvent e) { notifier.notifyObservers(OCBox.this); } } public void update(Observable o, Object arg) { OCBox clicked = (OCBox)arg; if(nextTo(clicked)) { cColor = clicked.cColor; repaint(); } } private final boolean nextTo(OCBox b) { return Math.abs(x - b.x) <= 1 && Math.abs(y - b.y) <= 1; }}
原文地址: http://blog.sina.com.cn/s/blog_3f4dc73b01009mls.html
- 观察器(Observer)
- MVC-Observer(观察器)
- Observer(观察器)模式
- 设计模式:Observer(观察器)
- 观察器
- iOS开发7-KVO(Key-Value Observer)键值观察
- java观察着observer模式---商品价格变动通知客户
- 观察
- 观察
- 思考设计模式在自助终端软件上的应用 ——Observer(观察着)模式
- 设计技巧24:观察 Observer 一对多通知状态的变换
- 观察都模式Observable与Observer的运用(单指拖放)
- Observer
- observer
- Observer
- Observer
- Observer
- Observer
- 为什么VB.net的Shared(共享)方法在C#中叫Static(静态)?(http://zhenyulu.cnblogs.com/articles/385047.html)
- SOA性能消耗
- 打字测试程序代码(C语言)
- .config文件加密解密
- Life with qmail中文版 PDF格式
- 观察器(Observer)
- ASP.NET AJAX 和客户端模板
- 关于java使用javacomm20-win32实践总结
- 网络通讯更自由 谈Java串行端口技术协议
- 单页界面和 AJAX 模式
- 在Java中操作串口实现短信收发
- 疯狂的程序员66
- Ext2.0含有checkbox的树状菜单
- 命名规则