观察器(Observer)

 
Observable类用于跟踪那些当发生一个改变时希望收到通知的所有个体——无论“状态”是否改变。如果有人说“好了,所有人都要检查自己,并可能要进行更新”,那么Observable类会执行这个任务——为列表中的每个“人”都调用notifyObservers()方法。notifyObservers()方法属于基础类Observable的一部分。
 
Observable对象会自动调用每个Observer对象的update()方法。
 
为真正产生效果,必须从Observable继承,并在衍生类代码的某个地方调用setChanged()。这个方法需要设置“changed”(已改变)标志,它意味着当调用notifyObservers()的时候,所有观察器事实上都会收到通知。
 
通过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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值