junit组合模式应用
组合模式
定义:
将对象组合成树形结构以表示“部分-整体”的层次结构。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组合模式应用的更多相关文章
- junit设计模式--组合模式
Composite,英语翻译下,复合,组合. 组合模式有时候又叫做部分-整体模式,它使我们在树形结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以像处理简单元素一样来处理复杂元素,从而使得客户 ...
- [19/04/26-星期五] GOF23_结构型模式(桥接模式、组合模式)
一.桥接模式(bridge) 场景:商城系统中常见的商品分类,以电脑为例,首先想到使用多层继承结构. —— 台式机(联想台式机.戴尔台式机.神舟台式机) 电脑 ——笔记本(联想笔记本.戴尔笔记本 ...
- GOF23设计模式之组合模式(composite)
一.组合模式概述 将对象组合成树状结构以表示“部分和整体”层次结构,使得客户可以统一的调用叶子对象和容器对象. (1)组合模式的使用场景 把部分和整体的关系用树形结构来表示,从而使客户端可以使用统 ...
- 设计模式GOF23(结构型模式:代理模式,适配模式,桥接模式,组合模式,装饰模式,外观模式,享元模式)
结构型模式: – 分类: • 适配器模式.代理模式.桥接模式.装饰模式.组合模式.外观模式.享元模式 – 核心作用:是从程序的结构上实现松耦合,从而可以扩大整体的类结构,用来解决更大的问题. 结构 ...
- ComponentPattern (组合模式)
import java.util.LinkedList; /** * 组合模式 * * @author TMAC-J 主要用于树状结构,用于部分和整体区别无区别的场景 想象一下,假设有一批连锁的理发店 ...
- 设计模式(十一):从文Finder中认识"组合模式"(Composite Pattern)
上一篇博客中我们从从电影院中认识了"迭代器模式"(Iterator Pattern),今天我们就从文件系统中来认识一下“组合模式”(Composite Pattern).说到组合模 ...
- 设计模式(十)组合模式(Composite Pattern)
一.引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象 ...
- 设计模式--组合模式Composite(结构型)
一.概念 组合模式允许你将对象组合成树形结构来表现"整体/部分"层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 二.UML图 1.Component(对象接口),定义 ...
- 组合模式/composite模式/对象结构型模式
组合模式/composite模式/对象结构型 意图 将对象组合成树形结构以表示"整体--部分"的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. 动机 C ...
随机推荐
- 阿里云Mysql重置密码
1.关闭mysql服务 # service mysql stop 如果提示mysql: unrecognized service这样的错误提示. 先查看查找mysql.server,使用:find / ...
- 并行计算之Memory barrier(内存
本文转载自:http://name5566.com/4535.html 参考文献列表:http://en.wikipedia.org/wiki/Memory_barrierhttp://en.wiki ...
- asp.net 页面 输出之前修改 html(render)
protected override void Render(HtmlTextWriter writer) { StringWriter output = new StringWriter(); ba ...
- 【Properties文件】Java使用Properties来读取配置文件
配置文件位置及内容 执行结果 程序代码 package Utils.ConfigFile; import java.io.BufferedInputStream; import java.io.B ...
- php base64编码和urlencode
base64编码 加密 base64_encode($str); 解密 base64_decode(base64_encode($str)); urlencode和base64混合使用 functio ...
- 成功移植SQLite3到ARM Linux开发板
SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了 ...
- Session和Cookie深度剖析
Session和Cookie的区别 具体来说cookie机制采用的是在客户端保持状态的方案,而session机制采用的是在服务器端保持状态的方案.同时我们也看到,由于采用服务器端保持状态的方案在客户端 ...
- 如何缩短SQL Server 的启动时间
将/nosplash添加到SQLServer Manageement Studio的快捷方式可以缩短启动时间.为此,右击SSMS快捷方式(位于你的桌面上.start菜单中或任务栏中)并选择Proper ...
- jquery源码
null 与 undefined 都是 ==null 为true alert(typeof(123)) number alert(typeof(NAN)) 打印 number 不靠谱 ale ...
- URL编码CFURLCreateStringByAddingPercentEscapes使用(ARC)
URL 编码:CFURLCreateStringByAddingPercentEscapes If you have tried to send any information using a GET ...