zoukankan      html  css  js  c++  java
  • junit适配器模式应用

    适配器模式

    定义

      将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作;

    构成

      目标抽象角色(Target):定义客户要用的特定领域的接口
      适配器(Adapter):调用另一个接口,作为一个转换器
      适配器(Adaptee):定义一个接口,Adapter需要接入

      比如我们家的台灯的插头是2相的,但是我们墙壁上的电源插座是3相的,那我们如何用呢???可以去买一个插排,插排接电源的是3孔的,插排上有很多插孔是2孔的,那么插排起到的作用就是桥梁,起转换的作用,在这里就是适配器模式中的适配器

    分类

      常用的适配器模式有两种:类适配器(采用继承的方式)和对象适配器(采用对象组合的方式)

    java代码实现

    类适配器:

    //台灯插头
    public interface Target {
        public void method1();  
    }
    //墙壁上的电源
    public class Adaptee {
        public void method2(){
            System.out.println("Adaptee.method2()");
        }
    }
    //插排
    public class Adapter extends Adaptee implements Target {
        @Override
        public void method1() {
            this.method2();
        }
    }
    public class Client {
        public static void main(String[] args) {
            Target target = new Adapter();
            target.method1();
        }
    }

    对象适配器:

    //台灯插头
    public interface Target {
        public void method1();  
    }
    //墙壁上的电源
    public class Adaptee {
        public void method2(){
            System.out.println("Adaptee.method2()");
        }
    }
    //插排
    public class Adapter implements Target {
        private Adaptee adaptee;
    
        public Adapter(Adaptee adaptee) {
            this.adaptee = adaptee;
        }
    
        @Override
        public void method1() {
            adaptee.method2();
        }
    }
    public class Client {
        public static void main(String[] args) {
            Target target = new Adapter(new Adaptee());
            target.method1();
        }
    }

    适配器模式在junit3框架中的应用

    TestCase.java中的runTest()方法

    public void runBare() throws Throwable {
        setUp();
        try {
            runTest();
        }
        finally {
            tearDown();
        }
    }
        
    protected void runTest() throws Throwable {
        assertNotNull(fName);
        Method runMethod= null;
        try {
            runMethod= getClass().getMethod(fName, null);
        } catch (NoSuchMethodException e) {
            fail("Method ""+fName+"" not found");
        }
        if (!Modifier.isPublic(runMethod.getModifiers())) {
            fail("Method ""+fName+"" should be public");
        }
    
        runMethod.invoke(this, new Class[0]);
    }

    在runBare()方法中,通过runTest()方法将我们自己编写的testXXX()方法进行了适配,使得junit框架可以执行我们自己编写的TestCase;

    runTest()方法中,首先获得我们自己编写的testXXX方法所对应的Method对象(无参),然后检查该Method对象所编写的方法是否是pulbic的,如果是则调用Method对象的invoke方法来执行我们自己编写的testXXX方法

    在这里目标接口Target和适配器Adapter变成了同一个类TestCase,而测试用例,作为Adaptee

    junit3中引入适配器模式的好处

    1)使用Adapter模式简化测试用例的开发,通过按照方法命名的规范来开发测试用例,不需要进行大量的类继承,提高代码的复用,减轻测试人员的工作量;

    2)使用Adapter可以重新定义Adaptee的部分行为,如增强异常处理等

  • 相关阅读:
    【leetcode】反转字符串
    【leetcode】反转字符串 II
    053-669
    053-668
    053-667
    053-666
    053-665
    053-664
    053-663
    053-662
  • 原文地址:https://www.cnblogs.com/luogankun/p/3996326.html
Copyright © 2011-2022 走看看