每个人都会死去,但不是每个人都曾经真正活过
Ever man dies,Not every man really lives
BlogJava
首页
新随笔
新文章
联系
聚合
管理
posts - 7,comments - 0,trackbacks - 0
<
2024年11月
>
日
一
二
三
四
五
六
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
some one once said, if you want something very badly, set it free. if it comes back to you, it's yours forever. if it doesn't, it was not yours to begin with.
常用链接
我的随笔
我的评论
我的参与
留言簿
(1)
给我留言
查看公开留言
查看私人留言
随笔分类
java(2)
杂记(2)
生活(1)
随笔档案
2006年10月 (1)
2006年9月 (6)
文章分类
java
收藏(3)
设计模式(3)
文章档案
2006年9月 (3)
搜索
最新评论
阅读排行榜
1. 近期工作学习计划(199)
2. 学习计划(194)
3. 每日流水帐之-------用户配置的生成(183)
4. 每日流水帐之-------初识spring(157)
5. 每日流水帐之-------对myblog的几点新想法(156)
评论排行榜
1. 最近比较烦(0)
2. 每日流水帐之-------用户配置的生成(0)
3. 每日流水帐之-------对myblog的几点新想法(0)
4. 学习计划(0)
5. 每日流水帐之-------初识spring(0)
设计模式学习之(Head First Design Patterns)------观察者模式
OBSERVER
The Observer Pattern defines a one-to-many dependency between object so that when one object changes state ,all
of its dependents are notified and update automatically.
观察者模式在对象间定义了一个一对多的关系,当一个对象(Subject 主题对象)发生改变时,所有依赖于该对象的对象都会被通知并且自动更新。
The Observer Pattern provides an object design where subject and observer are loosely coupled:
1> The only thing the subject knows about an observer is that it implement a certain interface.
2> We can add new observer at any time.
3> We never need to modify the subject to add new type of observers.
4> Changes to either the subject or an observer will not affect other.
以下是测试代码
ProductObservable
/** */
/**
*
@author
Wangzg
* Time: 2006-9-12
* FileName: ProductObservable.java
* PackageName: src.observerPattern
* Description: Observable class
*/
package
src.observerPattern;
import
java.util.Observable;
public
class
ProductObservable
extends
Observable
{
private
String name;
private
double
weigh;
public
String getName()
{
return
name;
}
public
void
setName(String name)
{
this
.name
=
name;
setChanged();
notifyObservers(name);
}
public
double
getWeigh()
{
return
weigh;
}
public
void
setWeigh(
double
weigh)
{
this
.weigh
=
weigh;
setChanged();
notifyObservers(
new
Double(weigh));
}
}
ProductNameobserver
/** */
/**
*
@author
Wangzg
* Time: 2006-9-12
* FileName: ProductNameobserver.java
* PackageName: src.observerPattern
* Description: Observer class
*/
package
src.observerPattern;
import
java.util.Observable;
import
java.util.Observer;
public
class
ProductNameobserver
implements
Observer
{
private
String name
=
null
;
/**/
/*
* @author Wangzg
* Time: 2006-9-12
* MethodsName:
* variable in :
* variable out :
* Description:
*/
public
void
update(Observable o, Object arg)
{
//
TODO Auto-generated method stub
if
( arg
instanceof
String)
{
name
=
(String)arg;
System.out.println(
"
name change to
"
+
name);
}
System.out.println(
"
ProductNameobserver nothing happen
"
);
}
}
ProductWeighObserver
/** */
/**
*
@author
Wangzg
* Time: 2006-9-12
* FileName: ProductWeighObserver.java
* PackageName: src.observerPattern
* Description: Observer class
*/
package
src.observerPattern;
import
java.util.Observable;
import
java.util.Observer;
public
class
ProductWeighObserver
implements
Observer
{
private
dou
Double
ble
weigh
=
0.0
;
/**/
/*
* @author Wangzg Time: 2006-9-12 MethodsName: variable in : variable out :
* Description:
*/
public
void
update(Observable o, Object arg)
{
//
TODO Auto-generated method stub
if
( arg
instanceof
ProductObservable )
{
weigh
=
((Double)arg).doubleValue();
System.out.println(
"
weigh change to
"
+
weigh);
}
System.out.println(
"
ProductWeighObserver nothing happen
"
);
}
}
posted on 2006-09-12 19:56
saftyfirst
阅读(149)
评论(0)
编辑
收藏
所属分类:
设计模式