• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • Junit代碼分析之觀察者模式(2)

    發表于:2013-03-06來源:新浪博客作者:土司點擊數: 標簽:junit
    } public void endTest(Test test) { } public void startTest(Test test) { getWriter().print(.); if (fColumn++ = 40) { getWriter().println(); fColumn= 0; } } Junit中的說被觀察者就是TestResult對象

      }

      public void endTest(Test test) {

      }

      public void startTest(Test test) {

      getWriter().print(".");

      if (fColumn++ >= 40) {

      getWriter().println();

      fColumn= 0;

      }

      }

      Junit中的說被觀察者就是TestResult對象,它有添加觀察者的方法:

      public synchronized voidaddListener(TestListener listener) {

      fListeners.addElement(listener);

      }

      在Junit中被觀察者是如何通知觀察者呢?請看這幾個方法,在TestResult中下面幾個方法都是循環遍歷觀察者列表,并調用相應的更新方法:

      public synchronized void addError(Test test, Throwable t) {

      fErrors.addElement(new TestFailure(test, t));

      for (Enumeration e= cloneListeners().elements(); e.hasMoreElements(); ) {

      ((TestListener)e.nextElement()).addError(test, t);

      }

      }

      public synchronized void addFailure(Test test, AssertionFailedError t) {

      fFailures.addElement(new TestFailure(test, t));

      for (Enumeration e= cloneListeners().elements(); e.hasMoreElements(); ) {

      ((TestListener)e.nextElement()).addFailure(test, t);

      }

      }

      public synchronized voidaddListener(TestListener listener) {

      fListeners.addElement(listener);

      }

      public synchronized voidremoveListener(TestListener listener) {

      fListeners.removeElement(listener);

      }

      private synchronized Vector cloneListeners() {

      return (Vector)fListeners.clone();

      }

      在Junit中使用觀察者模式帶來的好處:

      1)上面提到的Subject與Observer的抽象耦合,使JUnit可以支持不同的使用方式,為Junit執行平臺(如textui、awtextui等)的擴展非常容易

      2)支持了廣播通信,目標對象不關心有多少對象對自己注冊,它只是通知注冊的觀察者

    原文轉自:http://blog.sina.com.cn/s/blog_6e0d94750101640x.html

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>