This article Source: GitHub · Click here || GitEE · Click here
I. Introduction adapter mode
1, the basic concept
The adapter mode interface of a class into a client interface to expect another, so that the two classes had to work together by an interface mismatch can not work together. There adapter mode adapter mode and a class object adapter mode, and a default (Interface) adapters in three different forms.
2, scenes of life
Based adapter mode, a voltage of 220V, 110V is converted to a voltage required.
public class C01_InScene {
public static void main(String[] args) {
CurrentAdapter adapter = new CurrentAdapter() ;
System.out.println(adapter.get110VCurrent()) ;
}
}
// 220V电流
class Current220V {
public int get220VCurrent (){
return 220 ;
}
}
// 110V电流接口
interface Current110V {
int get110VCurrent () ;
}
// 电流适配器
class CurrentAdapter extends Current220V implements Current110V {
// 电流转换方法
@Override
public int get110VCurrent() {
int high = get220VCurrent() ;
int low = high/2 ;
return low ;
}
}
Second, the adapter class
1, Model Description
Adapter mode adapted to the class of the class of the target class converted into API API.
2, the core role
- Target (Target) role
This is the interface to get the look.
- Source (Adapee) Role:
Now we need to adapt the interface.
- Adapter (Adaper) role
Adapters of this class is the core mode. Source interface adapter converts the destination interface.
3, source code implementation
interface Target {
void sampleOperation1();
void sampleOperation2();
}
class Adaptee {
public void sampleOperation1(){
System.out.println("Adaptee.sampleOperation1()");
}
}
class Adapter extends Adaptee implements Target{
@Override
public void sampleOperation2() {
System.out.println("Adapter.sampleOperation2()");
}
}
Third, the Object Adapter
1, Model Description
As with the adapter mode and the like, adapter API schema objects to the conversion of the class adapted to become the target class API, and the Adapter pattern class is different, the Adapter pattern object is not connected to Adaptee classes use inheritance, but Adaptee relation to the delegated connection class.
2, source code implementation
interface Target1 {
void sampleOperation1();
void sampleOperation2();
}
class Adaptee1 {
public void sampleOperation1(){
System.out.println("Adaptee.sampleOperation1()");
}
}
class Adapter1 implements Target1 {
private Adaptee1 adaptee ;
public Adapter1 (Adaptee1 adaptee){
this.adaptee = adaptee;
}
public void sampleOperation1() {
this.adaptee.sampleOperation1();
}
@Override
public void sampleOperation2() {
System.out.println("Adapter.sampleOperation2()");
}
}
Fourth, the interface adapter
1, Model Description
Default (interface) adapter (Default Adapter) mode interface provides a default implementation, this subtype may be implemented to extend from this default, without having to extend from the original interface.
2, the source code to achieve
public class C04_AdapterInte {
public static void main(String[] args) {
ServiceAdapter adapter = new ServiceAdapter(){
@Override
public int serviceOperation2() {
return 22 ;
}
};
System.out.println(adapter.serviceOperation2());
}
}
interface AbstractService {
void serviceOperation1();
int serviceOperation2();
String serviceOperation3();
}
class ServiceAdapter implements AbstractService{
@Override
public void serviceOperation1() {
}
@Override
public int serviceOperation2() {
return 0;
}
@Override
public String serviceOperation3() {
return null;
}
}
Five, Spring Framework application
1, the application scene description
When execution control SpringMvc execution request, such a process
1) preceding the controller adapter to the processor performs call DispatcherServlet Handler (i.e. the Controller);
2) the adapter processor to perform Handler, ModelAndView returned to the adapter;
. 3) to the front end processor adapter returns ModelAndView controller;
2, process analysis
- Core interface and implementation
Controller and HandlerAdapter two core interfaces.
- Handler Adapter
Adapter interface, causing the Handler to a corresponding adapter implementation class, instead of the adapter Handler (Control Layer Controller) respective method is performed.
public interface HandlerAdapter {
// 判断类型是否匹配
boolean supports(Object var1);
// 执行方法,返回ModelAndView
ModelAndView handle(HttpServletRequest var1,
HttpServletResponse var2, Object var3)
throws Exception;
}
the Supports () method passing the processor determines whether to support the adapter, the adapter support support if the implementation class is returned.
- DispatchServlert
Source extraction process embodied in several steps.
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HandlerExecutionChain mappedHandler = null;
mappedHandler = this.getHandler(processedRequest);
HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
mappedHandler.applyPostHandle(processedRequest, response, mv);
}
- SimpleControllerHandlerAdapter
Finally, look at the specific implementation and supports two methods handle.
public class SimpleControllerHandlerAdapter implements HandlerAdapter {
public SimpleControllerHandlerAdapter() {
}
public boolean supports(Object handler) {
return handler instanceof Controller;
}
public ModelAndView handle(HttpServletRequest request,
HttpServletResponse response, Object handler)
throws Exception {
return ((Controller)handler).handleRequest(request, response);
}
}
Sixth, the advantages and disadvantages adapter
1, the advantages analysis
Better reusability, the system requires the use of an existing class, and such interfaces do not meet the needs of the system. Then through the adapter mode to enable these functions to get better reuse. Better scalability.
2, analysis of shortcomings
Excessive use an adapter to make the system very messy and difficult to control a whole.
Seven, the source address
GitHub·地址
https://github.com/cicadasmile/model-arithmetic-parent
GitEE·地址
https://gitee.com/cicadasmile/model-arithmetic-parent