Junit所使用的设计模式
使用继承的方式来实现适配器模式
//目标类,它的方法里面调用Adaptee对象的方法 public interface Target { public void method1(); } //被适配对象,它的方法会被Target类的方法调用。 public class Adaptee { public void method2() { System.out.println("method2 invoked"); } } //使用继承的方式来实现适配器模式 //适配器对象,实现用Target类的方法调用Adaptee类的方法,起到一个中间转化的作用。 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("method2 invoked"); } } /** * 使用对象组合的方式来实现适配器对象 * @author anllin * */ 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(); } } |
模拟AWT事件模型所使用的适配器模式
public class MyFrame { public void
addMouseMotionListener(MyMouseMotionListener listener) { listener.mouseMoved(); listener.mouseDragged(); } } public interface MyMouseMotionListener { public void mouseMoved(); public void mouseDragged(); } public abstract class MyMouseMotionAdapter implements
MyMouseMotionListener { @Override public void mouseMoved() { } @Override public void mouseDragged() { } } public class Client { public static void main(String[] args) { MyFrame frame = new MyFrame(); frame.addMouseMotionListener(new
MyMouseMotionAdapter() { @Override public void mouseMoved() { System.out.println("mouse Moved"); } }); } } |
命令模式
通用模型
public interface Command { public void execute(); } public class ConcreteCommand implements Command { private Receiver recevier; public ConcreteCommand(Receiver
recevier) { this.recevier = recevier; } @Override public void execute() { this.recevier.doAction(); } } public class Invoker { private Command command; public Invoker(Command command) { super(); this.command = command; } public void doInvokerAction() { this.command.execute(); } } public class Receiver { public void doAction() { System.out.println("执行操作"); } } public class Client { public static void main(String[] args) { Command command = new ConcreteCommand(new Receiver()); Invoker invoker = new
Invoker(command); invoker.doInvokerAction(); } } |
现实中例子
我们去餐厅吃饭,点菜,然后服务员上菜
//订单 public interface Order { public void orderDishes(); } //具体定单内容 public class ConcreteOrder implements Order { private public ConcreteOrder( { this.kitchener = } //点菜 @Override public void orderDishes() { kitchener.cooking(); } } //厨师 public class Kitchener { //烹饪 public void cooking() { System.out.println("the dishes of customer is cooking..."); System.out.println("cooking is finished"); System.out.println("the waiter served the dishes"); } } //服务员 public class Waiter { private Order order; //下定单 public Waiter(Order order) { this.order = order; } //上菜 public void serveTheDishes() { this.order.orderDishes(); } } public class Client { public static void main(String[] args) { Order order = new ConcreteOrder(new Waiter waiter = new Waiter(order); waiter.serveTheDishes(); } } |
模拟junit中的使用命令模式的方式
public abstract class MyTestCase { private TestFixture testFixture = new TestFixture(); protected abstract void setUp(); protected abstract void tearDown(); protected void execute(Method method) throws Exception { setUp(); testFixture.run(this,method); tearDown(); } } public class MyConcreteTestCase extends MyTestCase { @Override protected void setUp() { System.out.println("set up"); } @Override protected void tearDown() { System.out.println("tear down"); } public void testAdd() { System.out.println("test add"); } public void testSubstract() { System.out.println("test subtract"); } public void testMuitiply() { System.out.println("test multiply"); } public void testDevide() { System.out.println("test devide"); } } public class TestFixture { public void run(MyTestCase
testCase,Method method) throws Exception { method.invoke(testCase, new Object[0]); } } public class TestRunner { private MyTestCase myTestCase; public TestRunner(MyTestCase
myTestCase) { this.myTestCase = myTestCase; } public void runTest() throws Exception { Class<?> clazz = myTestCase.getClass(); Method[] methods = clazz.getMethods(); for (Method method : methods) { if (method.getName().contains("test") && Modifier.isPublic(method.getModifiers()) &&
method.getReturnType() == void.class) { myTestCase.execute(method); } } } } |
模板方法
/** * junit3.8中的模板方法的应用 利用反射实现的adapter模式 * * @author anllin * */ public abstract class AbastractTestCase { // 当这个类被继承,this指定的就是子类的对象 public void execute() throws Exception { /** * 一般使用时,需要先调用构造函数实例化一个对象。 然后利用实例化对象调用方法 */ // Object constructor = clazz.getConstructor(new // Class[]{}).newInstance(new Object[]{}); // method.invoke(constructor, new Object[] {}); Class<?> clazz = this.getClass();// 子类的类型 Method[] methods = clazz.getMethods();// 子类的方法 this.setUpBeforeClass(); for (Method method : methods) { if (method.getName().contains("test") && Modifier.isPublic(method.getModifiers())) { this.setUp(); method.invoke(this, new Object[0]);// this就是子类的实例,所以不用引用构造函数 this.tearDown(); } } this.tearDownAfterClass(); } protected abstract void setUpBeforeClass(); protected abstract void tearDownAfterClass(); protected abstract void setUp(); protected abstract void tearDown(); } public class TestMyTemplate { public static void main(String[] args) throws Exception { AbastractTestCase test = new MyTestCase(); test.execute(); } } public class MyTestCase extends AbastractTestCase { @Override protected void setUpBeforeClass() { System.out.println("set up before class"); } @Override protected void tearDownAfterClass() { System.out.println("tear down after class"); } @Override protected void setUp() { System.out.println("set up "); } @Override protected void tearDown() { System.out.println("tear down"); } public void testAdd() { System.out.println("test add"); } public void testSubstract() { System.out.println("test subtract"); } public void testMuitiply() { System.out.println("test multiply"); } public void testDevide() { System.out.println("test devide"); } } |
组合模式
通用实现模型
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 this.list; } //注意:这时隐含了一个递归 @Override public void doSomething() { for(Component component : list) { component.doSomething(); } } } public class Leaf implements Component { @Override public void doSomething() { System.out.println("执行操作"); } } public class Client { public static void main(String[] args) { Component leaf1 = new Leaf(); Component
leaf2 = new Leaf(); Composite com1 = new Composite(); com1.add(leaf1); com1.add(leaf2); Component leaf3 = new Leaf(); Component leaf4 = new Leaf(); Composite com2 = new Composite(); com2.add(com1); com2.add(leaf3); com2.add(leaf4); com2.doSomething(); } } |
空实现模型
public interface Component { public void add(Component component); public void remove(Component component); public List<Component>
getAll(); public void doSomething(); } public class Composite implements Component { private List<Component> list = new
ArrayList<Component>(); @Override public void add(Component component) { list.add(component); } @Override public void remove(Component component) { list.remove(component); } @Override public List<Component>
getAll() { return this.list; } @Override public void doSomething() { for(Component component : list) { component.doSomething(); } } } public class Leaf implements Component { @Override public void add(Component component) { } @Override public void remove(Component component) { } @Override public List<Component>
getAll() { return null; } @Override public void doSomething() { System.out.println("执行操作"); } } public class Client { public static void main(String[] args) { Component leaf1 = new Leaf(); Component leaf2 = new Leaf(); Component com1 = new Composite(); com1.add(leaf1); com1.add(leaf2); Component leaf3 = new Leaf(); Component leaf4 = new Leaf(); Component com2 = new Composite(); com2.add(com1); com2.add(leaf3); com2.add(leaf4); com2.doSomething(); } } |
模拟junit中测试套件的组合模式的实现方式
public interface MySuite { public void runTest(); } public class MyTestSuite implements MySuite { private List<MySuite> list = new
ArrayList<MySuite>(); public void add(MySuite suite) { list.add(suite); } public void remove(MySuite suite) { list.remove(suite); } public List<MySuite> getAll() { return list; } @Override public void runTest() { for(MySuite suite : list) { suite.runTest(); } } } public class MyTestCase implements MySuite { @Override public void runTest() { System.out.println("execute tese case method"); } } public class TestRunner { public static void main(String[] args) { MySuite testCase1 = new MyTestCase(); MySuite testCase2 = new MyTestCase(); MyTestSuite testSuite1 = new MyTestSuite(); testSuite1.add(testCase1); testSuite1.add(testCase2); MySuite testCase3 = new MyTestCase(); MySuite testCase4 = new MyTestCase(); MyTestSuite testSuite2 = new MyTestSuite(); testSuite2.add(testSuite1); testSuite2.add(testCase3); testSuite2.add(testCase4); testSuite2.runTest(); } } |