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
Ïà¹ØÎĵµ£º
µÚÒ»²½£º²é¿´Linux×Ô´øµÄJDKÊÇ·ñÒÑ°²×° £¨Ð¶ÔØcentOSÒÑ°²×°µÄ1.4£©
<1># rpm -qa|grep jdk ← ²é¿´jdkµÄÐÅÏ¢»òÖ±½ÓÖ´ÐÐ
»ò
# rpm -q jdk
»ò
# java -version
<2># rpm -qa | grep gcj ← È·ÈÏgcjµÄ°æ±¾ºÅ
<3># yum -y remove java-1.4.2-gcj-compat ← жÔØgcj
µÚ¶þ²½£º°²×°JDK
< ......
ÉùÃ÷£º
ÒÔϵÄÀý×Ó²»Ò»¶¨ÕýÈ·£¬Ö»ÊÇΪÁËÑÝʾ´ó¸ÅµÄÁ÷³Ì¡£
Ò»£ºÎÞ·µ»ØÖµµÄ´æ´¢¹ý³Ì
´æ´¢¹ý³ÌΪ£º
CREATE OR REPLACE PROCEDURE TESTA(PARA1 IN VARCHAR2,PARA2 IN VARCHAR2) AS
BEGIN
INSERT INTO HYQ.B_ID (I_ID,I_NAME) VALUES (PARA1, PARA2);
END TESTA;
È»ºóÄØ£¬ÔÚjavaÀïµ÷ÓÃʱ¾ÍÓÃÏÂÃæµÄ´ ......
ÓÐÁ½ÖÖ·½·¨£º
·½·¨Ò»£ºÓÃjava.util.DateÀàÀ´ÊµÏÖ£¬²¢½áºÏjava.text.DateFormatÀàÀ´ÊµÏÖʱ¼äµÄ¸ñʽ»¯£¬¿´ÏÂÃæ´úÂ룺
import java.util.*;
import java.text.*;
//ÒÔÏÂĬÈÏʱ¼äÈÕÆÚÏÔʾ·½Ê½¶¼ÊǺºÓïÓïÑÔ·½Ê½
//Ò»°ãÓïÑÔ¾ÍĬÈϺºÓï¾Í¿ÉÒÔÁË£¬Ê±¼äÈÕÆڵĸñʽĬÈÏΪMEDIUM·ç¸ñ£¬±ÈÈ磺2008-6-16 20:54:53
//ÒÔÏÂÏÔʾµÄÈ ......
java´ò°ü-exeÎļþ-×îÖÕÒÔsetupÐÎʽ·¢²¼µÄ½â¾öÖ®µÀ
java´ò°ü-exeÎļþ-×îÖÕÒÔsetupÐÎʽ·¢²¼µÄ½â¾öÖ®µÀ
Ê×ÏÈ£¬½«±àÒëºÃµÄ³ÌÐò´ò°ü³ÉjarÎļþ£¬È»ºó×ö³öexe£¬ÕâÑù´úÂë¾Í²»¿É¼ûÁË£»µ«ÊÇexeÎļþÔÚûÓа²×°jreµÄµçÄÔÉϲ»ÄÜÔËÐУ¬Èç¹ûÒªÇó¿Í»§ÔÙÈ¥°²×°jre ÉèÖû·¾³±äÁ¿ ¾Í²»ÑÅÁË¡£ÎÒÃÇÐèÒª½«jre´ò°ü¡£
Õ⼸²½²Ù×÷Í ......