适配器模式(Adapter Pattern)
他的功能是将一个类的接口变成客户端所期望的另一种接口,从而使得原本因接口不匹配而不能一起工作的类一起工作。简而言之,就是之前有一个功能,但是新的功能需要原有的功能的一些代码,我们不想新开发,所以就新建一个适配器,使得可以使用。类似,手机充电转接头的功能,我们手机充电器有很头,双头的三头的,我们可以使用一个转接头,把三头的,二头的都插入转接头,然后转接头进行电量的输入。功能:解决不同功能兼容性问题 通用写法:类适配、接口适配、对象适配
Class Adapter
public class AC220 { public int outputAC220(){ int output=220; System.out.println("outPut"+output); return output; } } public interface DC5 { int output5(); } public class PowerAdapter extends AC220 implements DC5 { public int output5() { int adapterInput=super.outputAC220(); int adaptOutput=adapterInput/44; System.out.println("adapterInput:"+adapterInput+"adaptOutput:"+adaptOutput); return adaptOutput; } }
To test
public class Test { public static void main(String[] args) { DC5 powerAdapter=new PowerAdapter(); powerAdapter.output5(); } }
To explain
The Least Knowledge Principle would be against(bacause in PowerAdapter we can use methods of AC220)
Object adapter
public class PowerAdapter implements DC5 { private AC220 ac220; public PowerAdapter(AC220 ac220) { this.ac220 = ac220; } public int output5() { int adapterInput=ac220.outputAC220(); int adaptOutput=adapterInput/44; System.out.println("adapterInput:"+adapterInput+"adaptOutput:"+adaptOutput); return adaptOutput; } }
To explain
Actually,we just pass on object by construction,the problem of Least Knowledge Principle will be solved easily, but do we need create each adapter for each new classes?it is too complex to create adatper for each new classes ,so we use interface adapter to solve the problem
interface adapter
public class PowerAdapter implements DC { private AC220 ac220; public PowerAdapter(AC220 ac220) { this.ac220 = ac220; } public int outPut5V() { // can be use ac220.outputAC220(); System.out.println("outPut5V"); return 5; } public int outPut12V() { // can be use ac220.outputAC220(); return 12; } public int outPut24V() { // can be use ac220.outputAC220(); System.out.println("outPut24V"); return 24; } } public interface DC { int outPut5V(); int outPut12V(); int outPut24V(); }
To Test
public class Test { public static void main(String[] args) { DC powerAdapter=new PowerAdapter(new AC220()); powerAdapter.outPut5V(); powerAdapter.outPut12V(); powerAdapter.outPut24V(); } }
To explain
apparently, Interface Adapter like a proxy pattern,we pass on class named AC220 then to reconstruct method of class of AC220
A scene of login to illustrate how to use Adapter Pattern
that is our kernel class to process logic of login
public class LoginService { // to do register protected ResultMeg register(String userName, String password) { System.out.println("succeed in register" + userName + ":" + password); return null; } // to do login protected ResultMeg login(String userName, String password) { System.out.println("succeed in login" + userName + ":" + password); return null; } }
we need an adapter to redirect all of third party login
- we create an interface to restrict all thrid part login(IPassportForThird)
- we extends LoginService ,which can use method of LoginService(actually,that is class that we shoud adapt)
- because each thrid part login have different logic,we haven't to write all logic in the same class named LoginAdapterForThird,so we have to have a method named processLogin to distribute all requirement into each calsses
tips: the method named support is to verify rationality of classes
public class LoginAdapterForThird implements IPassportForThird { // login for QQ public ResultMeg loginForQQ(String openId) { return processLogin(openId, LoginForQQAdapter.class); } // login for WeChat public ResultMeg loginForWeChat(String openId) { return processLogin(openId, LoginForWeChatAdapter.class); } // login for Token public ResultMeg loginForToken(String token) { return processLogin(token, LoginForTokenAdapter.class); } // all third part login are distributed with this method private ResultMeg processLogin(String id, Class<? extends ILoginAdapter> clazz) { try { ILoginAdapter iLoginAdapter = clazz.newInstance(); if (iLoginAdapter.support(iLoginAdapter)) { return iLoginAdapter.login(id, iLoginAdapter); } } catch (Exception e) { e.printStackTrace(); } return null; } }
public interface IPassportForThird { ResultMeg loginForQQ(String openId); ResultMeg loginForWeChat(String openId); ResultMeg loginForToken(String token); }
Then each adapter have to has two methods one of is method named support the other is login,Two of them are ruled by ILoginAdapter
public interface ILoginAdapter { boolean support(Object o); ResultMeg login(String id, Object adapter); }
each adapter have to has logic of LoginService ,which means each adapter should extends LoginService implements ILoginAdapter,do we need to create an abstract class to write some common method and make each adapter extends AbstractAdapter?,we do!
public abstract class AbstractAdapter extends LoginService implements ILoginAdapter { ResultMeg loginForRegister(String userName, String password) { if (null == password) { password = "EMPTY_THIRD"; } super.register(userName, password); return super.login(userName, password); } }
each adapter like them
public class LoginForQQAdapter extends AbstractAdapter{ public boolean support(Object o) { return o instanceof LoginForQQAdapter; } public ResultMeg login(String id, Object adapter) { System.out.println("LoginForQQAdapter"); return super.loginForRegister(id,null); } } public class LoginForTokenAdapter extends AbstractAdapter { public boolean support(Object o) { return o instanceof LoginForTokenAdapter; } public ResultMeg login(String id, Object adapter) { System.out.println("LoginForTokenAdapter"); return super.loginForRegister(id,null); } } public class LoginForWeChatAdapter extends AbstractAdapter { public boolean support(Object o) { return o instanceof LoginForWeChatAdapter; } public ResultMeg login(String id, Object adapter) { System.out.println("LoginForWeChatAdapter"); return super.loginForRegister(id,null); } }
To test(we can use LoginAdapterForThird to get different method to login)
public class Test { public static void main(String[] args) { LoginAdapterForThird loginAdapterForThird=new LoginAdapterForThird(); loginAdapterForThird.loginForQQ("121212"); } }
additional class
public class ResultMeg { String code; String message; }
How does the adpater use in source of code
org.springframework.aop.framework.adapter.AdvisorAdapter(its classes that implementing it are all like what we wrote-(its supportsAdvice like our method named support,dosen't it)
)
org.springframework.web.servlet.HandlerAdapter
Sum up
advantages:
- the classes can be reused,and do not need to be change
- raise expansibility of system as you see if you want add a new function just add a new adapter
disadvantages:
- raise complexity of system(we have to add many new adaoters)
- raise difficulty of reading