统计

留言簿(1)

DB

Others

QA

Tech Website

阅读排行榜

评论排行榜

【JUnit源码解读】--观察者模式

一、观察者模式定义
         观察者模式(Observer): 在对象之间定义一对多的依赖,这样一来,当一个对象改变状态,依赖它的对象都会收到通知,并自动更新

二、在JUnit中的体现




TestResult中用Vector保存各个监听器(文本界面、图形界面和Eclipse插件)
protected Vector fListeners;  //监听器集合

在测试运行阶段,如果出现Error 或者 Failure,TestResult 则会通知各个监听器
public synchronized void addFailure(Test test, AssertionFailedError t) {
        fFailures.addElement(
new TestFailure(test, t));
        
//观察者模式在这里体现出来了,当fFailures有变化时,马上通知其它Listeners
        for (Enumeration e= cloneListeners().elements(); e.hasMoreElements(); ) {
            ((TestListener)e.nextElement()).addFailure(test, t);
        }

    }


posted on 2011-02-15 10:33 XXXXXX 阅读(211) 评论(0)  编辑  收藏 所属分类: Programing


只有注册用户登录后才能发表评论。


网站导航: