组合模式

定义

  将对象组合成树形结构以表示“部分-整体”的层次结构。Composite模式使得用户对单个对象和组合对象的使用具有一致性

构成

  Component:这是一个抽象角色,它给参加组合的对象规定一个接口。这个角色给出共有的接口和默认的行为。其实就我们的Test接口,它定义出run方法
  Composite:实现共有接口并维护一个测试用例的集合,它就是复合测试用例TestSuite
  Leaf:代表参加组合的对象,它没有下级子对象,仅定义出参加组合的原始对象的行为,其实就是单一的测试用例TestCase,它仅实现Test接口的方法

分类

  将管理子元素的方法定义在Composite类中;

  将管理子元素的方法定义在Component接口中;

java代码实现

将管理子元素的方法定义在Composite类中

public interface Component {
public void doSomething();
}
public class Composite implements Component {
private List<Component> list = new ArrayList<Component>(); public void add(Component component) {
list.add(component);
} public void remove(Component component) {
list.remove(component);
} public List<Component> getAll() {
return list;
} @Override
public void doSomething() {
for (Component component : list) {
component.doSomething();
}
}
}
public class Leaf implements Component {
public void doSomething() {
System.out.println("dosomething");
}
}
public class Client {
public static void main(String[] args) {
Component leaf1 = new Leaf();
Component leaf2 = new Leaf(); Composite composite = new Composite();
composite.add(leaf1);
composite.add(leaf2); Composite composite2 = new Composite();
Component leaf3 = new Leaf();
composite2.add(composite);
composite2.add(leaf3); composite2.doSomething();
}
}

将管理子元素的方法定义在Component接口中

public interface Component {
public void doSomething();
public void add(Component component);
public void remove(Component component) ;
public List<Component> getAll() ;
}
public class Composite implements Component {
private List<Component> list = new ArrayList<Component>(); public void add(Component component) {
list.add(component);
} public void remove(Component component) {
list.remove(component);
} public List<Component> getAll() {
return list;
} @Override
public void doSomething() {
for (Component component : list) {
component.doSomething();
}
}
}
public class Leaf implements Component {
public void doSomething() {
System.out.println("dosomething");
} public void add(Component component) {
} public List<Component> getAll() {
return null;
} public void remove(Component component) {
}
}
public class Client {
public static void main(String[] args) {
Component leaf1 = new Leaf();
Component leaf2 = new Leaf(); Composite composite = new Composite();
composite.add(leaf1);
composite.add(leaf2); Composite composite2 = new Composite();
Component leaf3 = new Leaf();
composite2.add(composite);
composite2.add(leaf3); composite2.doSomething();
}
}

组合模式在junit3框架中的应用

public interface Test {
public abstract void run(TestResult result);
} public abstract class TestCase extends Assert implements Test {
public void run(TestResult result) {
result.run(this);
}
} public class TestSuite implements Test {
private Vector fTests= new Vector(10); public void addTest(Test test) {
fTests.addElement(test);
} public void addTestSuite(Class testClass) {
addTest(new TestSuite(testClass));
} public void run(TestResult result) {
for (Enumeration e= tests(); e.hasMoreElements(); ) {
if (result.shouldStop() )
break;
Test test= (Test)e.nextElement();
runTest(test, result);
}
} public Enumeration tests() {
return fTests.elements();
}
}

TestSuit类中有一个属性fTests (Vector类型)中保存了其子测试用例,提供addTest方法来实现增加子对象TestCase,并且还提供testCount 和tests 等方法来操作子对象。最后通过run()方法实现对其子对象进行委托(delegate),最后还提供addTestSuite方法实现递归,构造成树形;

TestCase或者TestSuite都是对Test接口进行实现的。由于TestCase和TestSuit两者都符合Test接口,我们可以通过addTestSuite递归地将TestSuite再组合成TestSuite,这样将构成树形结构。所有开发者都能够创建他们自己的TestSuit。测试人员可创建一个组合了这些测试用例的TestSuit来运行它们所有的TestCase,形如:

public static Test suite() {
TestSuite suite1 = new TestSuite("我的测试TestSuit1");
TestSuite suite2 = new TestSuite("我的测试TestSuit2");
suite1.addTestSuite(xxx.class);
suite2.addTestSuite(xxx.class);
suite1.addTest(suite2);
return suite1;
}

junit3中引入组合模式的好处

1)简化了JUnit的代码 JUnit可以统一处理组合结构TestSuite和单个对象TestCase。使JUnit开发变得简单容易,因为不需要区分部分和整体的区别,不需要写一些充斥着if else的选择语句;

2)定义了TestCase对象和TestSuite的类层次结构基本对象TestCase可以被组合成更复杂的组合对象TestSuite,而这些组合对象又可以被组合,如上个例子,这样不断地递归下去。在程序的代码中,任何使用基本对象的地方都可方便的使用组合对象,大大简化系统维护和开发;

3)使得更容易增加新的类型的TestCase;

junit组合模式应用的更多相关文章

  1. junit设计模式--组合模式

    Composite,英语翻译下,复合,组合. 组合模式有时候又叫做部分-整体模式,它使我们在树形结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以像处理简单元素一样来处理复杂元素,从而使得客户 ...

  2. [19/04/26-星期五] GOF23_结构型模式(桥接模式、组合模式)

    一.桥接模式(bridge) 场景:商城系统中常见的商品分类,以电脑为例,首先想到使用多层继承结构. —— 台式机(联想台式机.戴尔台式机.神舟台式机) 电脑    ——笔记本(联想笔记本.戴尔笔记本 ...

  3. GOF23设计模式之组合模式(composite)

    一.组合模式概述 将对象组合成树状结构以表示“部分和整体”层次结构,使得客户可以统一的调用叶子对象和容器对象. (1)组合模式的使用场景   把部分和整体的关系用树形结构来表示,从而使客户端可以使用统 ...

  4. 设计模式GOF23(结构型模式:代理模式,适配模式,桥接模式,组合模式,装饰模式,外观模式,享元模式)

    结构型模式: – 分类: • 适配器模式.代理模式.桥接模式.装饰模式.组合模式.外观模式.享元模式 – 核心作用:是从程序的结构上实现松耦合,从而可以扩大整体的类结构,用来解决更大的问题.   结构 ...

  5. ComponentPattern (组合模式)

    import java.util.LinkedList; /** * 组合模式 * * @author TMAC-J 主要用于树状结构,用于部分和整体区别无区别的场景 想象一下,假设有一批连锁的理发店 ...

  6. 设计模式(十一):从文Finder中认识"组合模式"(Composite Pattern)

    上一篇博客中我们从从电影院中认识了"迭代器模式"(Iterator Pattern),今天我们就从文件系统中来认识一下“组合模式”(Composite Pattern).说到组合模 ...

  7. 设计模式(十)组合模式(Composite Pattern)

    一.引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象 ...

  8. 设计模式--组合模式Composite(结构型)

    一.概念 组合模式允许你将对象组合成树形结构来表现"整体/部分"层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 二.UML图 1.Component(对象接口),定义 ...

  9. 组合模式/composite模式/对象结构型模式

    组合模式/composite模式/对象结构型 意图 将对象组合成树形结构以表示"整体--部分"的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. 动机 C ...

随机推荐

  1. Request Session生命周期及struts1 中service的编写

    现在接手的项目是一个早期的struts1框架的项目.同时也是刚开始接触web 以及struts1架构. 在处理多个action时,有一个tab子页面需要每5s自动刷新一次. 然后在测试过程中发现,点击 ...

  2. android 反编译 逆向工具整理

    需要准备的道具需要哪些软件会在后面逆向过程中详细介绍,这里先大致罗列一下 android一台root并安装了xposed框架的手机(主要是为了脱壳) 类似[海马玩]这种模拟器 android-kill ...

  3. Nexus手动更新索引

    如果有耐心的话,完全可以通过在线更新索引的方式来做,但所消耗的时间较长,下面介绍一种简单.可行的方式来手动更新索引文件. 访问http://repo.maven.apache.org/maven2/. ...

  4. [内核同步]自旋锁spin_lock、spin_lock_irq 和 spin_lock_irqsave 分析

    转自:http://blog.csdn.net/wh_19910525/article/details/11536279 自旋锁的初衷:在短期间内进行轻量级的锁定.一个被争用的自旋锁使得请求它的线程在 ...

  5. Linux xargs命令

    xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具.它把一个数据流分割为一些足够小的块,以方便过滤器和命令进行处理.通常情况下,xargs从管道或者stdin中读取数据,但是它也能够从 ...

  6. 3. c的输入输出

    putchar与getchar操作输入输出通道 #include <stdio.h> #include <ctype.h> main(){ int c; while((c = ...

  7. EntityFramework ,ef 介绍

    EntityFramework之领域驱动设计实践 分层架构 在引入实例以前,我们有必要回顾,并进一步了解分层架构.“层”是一种体系结构模式[POSA1],也是被广大软件从业人员用得最为广泛而且最为灵活 ...

  8. 不用删除整个Maven本地库文件夹,Eclipse下直接更新Maven库

  9. Ant -- Another Neat Tool

      最早用来构建著名的Tomcat,可以看成是一个Java版本的Make.也正因为使用了Java,Ant是跨平台的.   Ant有一个构建脚本build.xml <?xml version = ...

  10. [jQuery]《锋利的jQuery》插件部分总结

    /** * <锋利的jQuery>插件部分总结 * * jQuery插件推荐命名:jquery.name.js * * $.fn.extend用于封装对象方法的插件 * $.extend用 ...