java.lang.Runnable
- 对Runnable的使用就可以看做是对命令模式的应用;
- 实现Runnable接口的类,如果在run()方法中用到了别的类,调用端再面向Runnable编程,那么就通过Runnable调用到了其他的类;
public class T implements Runnable {
@Override
public void run() {
LazySingleton lazySingleton = LazySingleton.getInstance();
System.out.println(Thread.currentThread().getName() + " : " + lazySingleton);
}
}
public class Test {
public static void main(String[] args) {
Thread t1 = new Thread(new T());
Thread t2 = new Thread(new T());
t1.start();
t2.start();
System.out.println("Program End");
}
}
junit.framework.Test
- 使用JUnit做但单元测试的时候,只需实现Test接口,加上相应注解,就可以测试现有的类,而无须修改现有的类;
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);
}