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
相关文档:
Java开发中,最终的程序发布一般是要打成jar包形式的,而有一些配置文件是放在jar里面的,这样在做File形式的读取时,就会发现读不到相应的文件。下面提供一种解决方式:String fileName = "/config/abcd.config";
InputStream in = getClass().getResourceAsStream(fileName);
BufferedReader br = null;
try{
br = n ......
先看一段代码
public class Hello{
public static void main(String[] args){
int i = 5 , j = 2;
System.out.println(i+j);
......
关于flex处理java 实体bean,我也查了好多资料,现在终于搞定,写一个简单的例子,分享给大家
高手就不用看了
/*student实体类=java*/
public class Student implements java.io.Serializable{
private String sNo;
private String sName;
get..
set..
}
/*flex as类 Student.as*/
packa ......
代码如下:
以下为引用的内容:
package com.example.pinyin.demo2;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.form ......
下面这篇文章是台湾著名Java论坛“Javaworld”的一个资深版主写的一封对Java新手的信,希望小组成员仔细看下 你会学到很多的、、、、
親愛的 java 新手們 :
很高興你們願意來這裡共同學習,
Java 這個語言, 已 ......