易截截图软件、单文件、免安装、纯绿色、仅160KB

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 异常Exception 集


<!--
/* 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 ......

java中汉字转拼音

代码如下:
  以下为引用的内容:
  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新手的一封信(bx软件小组版)

下面这篇文章是台湾著名Java论坛“Javaworld”的一个资深版主写的一封对Java新手的信,希望小组成员仔细看下 你会学到很多的、、、、
 親愛的 java 新手們 :
很高興你們願意來這裡共同學習,
Java 這個語言, 已&# ......

java程序实现线程连接池功能

 import   java.util.LinkedList;  
   
  public   abstract   class   Manager   {  
   
          private   String   mThreadPoolName   =   null;  
   
    ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号