组合模式

定义

  将对象组合成树形结构以表示“部分-整体”的层次结构。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. R(五): R常用函数

    工作笔记记录,会持续更新.... 目录: apply tapply lapply sapply merge substr.substring.strsplit.unlist.paste.paste0. ...

  2. [原]Fedora 20安装记录

    Fedora是我最喜欢的Linux版本,很长时间以来我都在安装使用.近一年多以来一直在搞一个C#相关的开发,很久都没有接触Fedora了,我上一次使用的版本还是Fedora 17.本以为作为一个“老” ...

  3. (转)C# 使用BackgroundWorker

    本文转载自:http://blog.csdn.net/andrew_wx/article/details/6615077 该例子为使用BackgroundWorker在TextBox文本中产生一个10 ...

  4. MySQL存储引擎【InnoDB、MyISAM、Memory】

    数据库,MySQL这样存在多存储引擎的数据库软件,清楚常见的存储引擎的区别,使用合适的存储引擎,使得项目跑的更顺畅,有时候对于一个项目,甚至比项目本身都重要.这篇文章,旨在浅谈常见的三种存储引擎的区别 ...

  5. 冒泡算法C#

    冒泡算法C# namespace数组排序 { classProgram { staticvoidMain(string[]args) { inttemp=; ,,,,,,,,}; #region该段与 ...

  6. ASP.NET的POST和GET提交并接收处理返回值

    POST方法: 数据提交 /// <summary> /// POST提交数据接收字符json /// </summary> /// <param name=" ...

  7. 黄聪:wordpress如何开启文章格式post format

    发现很多“古老”的WordPress主题使用量非常大,虽然部分也在随着WordPress版本的升级而“升级”,只不过是修复了bug而已,wordpress的新特性并没有使用.而且多数国内的wordpr ...

  8. PLSQL_性能优化系列20_Oracle Result Cash结果缓存

    20150528 Created By BaoXinjian

  9. AP_AP系列 - 发票管理分析(案例)

    2014-07-07 Created By BaoXinjian

  10. HDU 2188 悼念512汶川大地震遇难同胞――选拔志愿者(巴什博奕)

    选拔志愿者 题意: 对于四川同胞遭受的灾难,全国人民纷纷伸出援助之手,几乎每个省市都派出了大量的救援人员,这其中包括抢险救灾的武警部队,治疗和防疫的医护人员,以及进行心理疏导的心理学专家.根据要求,我 ...