composite合成模式

      composite合成模式可以使用户对象以同样的方式对待单独的对象或复合的对象,合成模式要设计一个公共接口,即可提供给单独的对象使用,也可以供给复合对象使用。
      使得用户类一致地使用单独的对象或者复合的对象,也不必关系要如何处理复合对象,只需要向复合对象添加单独对象即可。
      junit的Test接口的两个子类TestCase和TestSuite就是使用了合成模式。

      其部分关键源代码如下,合成模式的代码结构基本上跟下面代码一样:

package junit.framework;
/** * A <em>Test</em> can be run and collect its results. * * @see TestResult
 */
public interface Test {
 /**
  * Counts the number of test cases that will be run by this test.
  */
 public abstract int countTestCases();
 /**
  * Runs a test and collects its result in a TestResult instance.
  */
 public abstract void run(TestResult result);
}

package junit.framework;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public abstract class TestCase extends Assert implements Test {
 /*......*/
 public int countTestCases() {
  return 1;
 }
 public TestResult run() {
  TestResult result= createResult();
  run(result);
  return result;
 }
 /**
  * Runs the test case and collects the results in TestResult.
  */
 public void run(TestResult result) {
  result.run(this);
 }
 /*......*/
}
package junit.framework;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

public class TestSuite implements Test {
 /*......*/
 private Vector<Test> fTests= new Vector<Test>(10);
 /**
  * Adds a test to the suite.
  */
 public void addTest(Test test) {
  fTests.add(test);
 }
 /**
  * Adds the tests from the given class to the suite
  */
 public void addTestSuite(Class<? extends TestCase> testClass) {
  addTest(new TestSuite(testClass));
 }
 
 /**
  * Counts the number of test cases that will be run by this test.
  */
 public int countTestCases() {
  int count= 0;
  for (Test each : fTests)
   count+=  each.countTestCases();
  return count;
 }

 /**
  * Runs the tests and collects their result in a TestResult.
  */
 public void run(TestResult result) {
  for (Test each : fTests) {
     if (result.shouldStop() )
      break;
   runTest(each, result);
  }
 }
 
 public void runTest(Test test, TestResult result) {
  test.run(result);
 }
 /*......*/
}

 要一起执行若干个TestCase只要向TestSuit的addTest添加,和TestCast一样执行run即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值