早泻 发表于 2009-2-12 09:00:45

观察器(Observer)的用法以及其他

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

    view plaincopy to clipboardprint?
    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);
      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(Graphicsg) {
      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;
      }
    }
    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);
      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(Graphicsg) {
      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
   上一页
页: [1]
查看完整版本: 观察器(Observer)的用法以及其他