4该模式的目标是,通过一个代理(Adapter),在原来的类(Adaptee)和客户(Client)之间进行协调,从而达到兼容的目的。
例如:我的项目中Davinci中使用的模式,将整体的游戏类作为Adapter,把用于通讯的Server类作为成员变量(也就是ObjectAdapter)。
示意代码
package adapter; interface Target { public void request(); public void specRequest(); } class Adaptee { //适配者 public void specRequest() { System.out.println("this is specRequest."); } } class ClassAdapter extends Adaptee implements Target { public void request() { System.out.println("This is request in ClassAdapter."); } } class ObjectAdapter implements Target { private Adaptee adaptee = new Adaptee(); public void request() { System.out.println("this. is request in ObjectAdapter."); } public void specRequest() { adaptee.specRequest(); } } public class Client { private static Target ctarget = new ClassAdapter(); private static Target otarget = new ObjectAdapter(); public static void main(String args[]) { ctarget.request(); ctarget.specRequest(); otarget.request(); otarget.specRequest(); } }