Java Éè¼Æģʽ(12)
Observer
Intent
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
How to
Subject
knows its observers. Any number of Observer objects may observe a subject.
provides an interface for attaching and detaching Observer objects.
Observer
defines an updating interface for objects that should be notified of changes in a subject.
Known cases
Listener
MVC
Publish-Subscribe
UML
´úÂëÑùÀý£º
package org.haha.DesignPatterns.observer;
import java.util.ArrayList;
import java.util.List;
public class Observable {
private List<Observer> observers;
public Observable() {
observers = new ArrayList<Observer>();
}
public void register(Observer obs) {
observers.add(obs);
}
public void unregister(Observer obs) {
observers.remove(obs);
}
public void remind() {
for (Observer obs : observers) {
obs.update(this);
}
}
}
package org.haha.DesignPatterns.observer;
public abstract class Observer {
public Observer() {
}
abstract public void update(Observable subject);
}
package org.haha.DesignPatterns.observer;
import java.util.List;
import java.util.ArrayList;
public class Subject extends Observable {
private List<String> datas = new ArrayList<String>();
public void addData(String data) {
datas.add(data);
remind();
}
public List<String> getDatas() {
return datas;
}
}
package org.haha.DesignPatterns.observer;
import java.util.List;
public class Subscriber extends Observer {
private String name;
public Subscriber(String name) {
super();
this.name = name;
}
public void update(Observable sub) {
if (sub instanceof Subject) {
List<String> datas = ((Subject)sub).getDatas();
for (String str : datas) {
System.out.println("Name=" +name + " " + str);
}
}
}
}
p
Ïà¹ØÎĵµ£º
XMLÏÖÔÚÒѾ³ÉΪһÖÖͨÓõÄÊý¾Ý½»»»¸ñʽ,ËüµÄƽ̨ÎÞ¹ØÐÔ,ÓïÑÔÎÞ¹ØÐÔ,ϵͳÎÞ¹ØÐÔ,¸øÊý¾Ý¼¯³ÉÓë½»»¥´øÀ´Á˼«´óµÄ·½±ã¡£¶ÔÓÚXML±¾ÉíµÄÓ﷨֪ʶÓë¼¼Êõϸ½Ú,ÐèÒªÔĶÁÏà¹ØµÄ¼¼ÊõÎÄÏ×,ÕâÀïÃæ°üÀ¨µÄÄÚÈÝÓÐDOM(Document Object Model),DTD(Document Type Definition),SAX(Simple API for XML),XSD(Xml Schema Definition),XSLT(Exten ......
ÓÐÁ½ÖÖ·½·¨£º
·½·¨Ò»£ºÓÃjava.util.DateÀàÀ´ÊµÏÖ£¬²¢½áºÏjava.text.DateFormatÀàÀ´ÊµÏÖʱ¼äµÄ¸ñʽ»¯£¬¿´ÏÂÃæ´úÂ룺
import java.util.*;
import java.text.*;
//ÒÔÏÂĬÈÏʱ¼äÈÕÆÚÏÔʾ·½Ê½¶¼ÊǺºÓïÓïÑÔ·½Ê½
//Ò»°ãÓïÑÔ¾ÍĬÈϺºÓï¾Í¿ÉÒÔÁË£¬Ê±¼äÈÕÆڵĸñʽĬÈÏΪMEDIUM·ç¸ñ£¬±ÈÈ磺2008-6-16 20:54:53
//ÒÔÏÂÏÔʾµÄÈ ......
<!--
/* Font Definitions */
@font-face
{font-family:SimSun;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:ËÎÌå;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"\@SimSun&qu ......
import java.util.LinkedList;
public abstract class Manager {
private String mThreadPoolName = null;
......