设计模式之适配器模式

223 阅读5分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

  • 个人简介:微信公众号关注:SteveCode。为您分享更多的知识学术。生于忧患死于安乐
  • 专注Java技术干货分享,Java基础技术、数据结构、相关工具、Spring全家桶、intellij idea......

适配器模式-结构化

在这里插入图片描述 在这里插入图片描述

类适配器

在这里插入图片描述

上代码

/*******************************************************************************
 * Package: com.example.demo.design.adapter.classadapter
 * Type:    Voltage
 * Date:    2022-03-12 10:14
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.adapter.classadapter;

/**
 * 功能描述:5V电压转换器
 *
 * @author Songxianyang
 * @date 2022-03-12 10:14
 */
public class Change extends Source implements VoltageAdapter {
    @Override
    public String output5v() {
        String v220 = super.output();
        v220="5v";
        return v220;
    
    }
}

/*******************************************************************************
 * Package: com.example.demo.design.adapter.classadapter
 * Type:    Phone
 * Date:    2022-03-12 10:28
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.adapter.classadapter;

/**
 * 功能描述:手机充电
 *
 * @author Songxianyang
 * @date 2022-03-12 10:28
 */
public class Phone {
    private VoltageAdapter adapter;
    
    public Phone(VoltageAdapter adapter) {
        this.adapter = adapter;
    }
    
    public void chargePhone() {
        System.out.println("我得手机以及充上"+adapter.output5v());
    }
}

/*******************************************************************************
 * Package: com.example.demo.design.adapter
 * Type:    Source
 * Date:    2022-03-12 10:03
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.adapter.classadapter;

/**
 * 类适配器
 * 功能描述:电源类220v   被适配器
 *
 * @author Songxianyang
 * @date 2022-03-12 10:03
 */
public class Source {
    public String output() {
        return "220v";
    }
}

/*******************************************************************************
 * Package: com.example.demo.design.adapter.classadapter
 * Type:    Test
 * Date:    2022-03-12 10:34
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.adapter.classadapter;

/**
 * 功能描述:
 *
 * @author Songxianyang
 * @date 2022-03-12 10:34
 */
public class Test {
    public static void main(String[] args) {
        Phone phone = new Phone(new Change());
        phone.chargePhone();
    }
}

/*******************************************************************************
 * Package: com.example.demo.design.adapter.classadapter
 * Type:    SourceAdapter
 * Date:    2022-03-12 10:06
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.adapter.classadapter;

/**
 * 功能描述: 充电器适配器
 *
 * @author Songxianyang
 * @date 2022-03-12 10:06
 */
public interface VoltageAdapter {
    /**
     * 输出5v电压
     * @return s
     */
    String output5v();
}

在这里插入图片描述

对象适配器

在这里插入图片描述 在这里插入图片描述

/*******************************************************************************
 * Package: com.example.demo.design.adapter.classadapter
 * Type:    Voltage
 * Date:    2022-03-12 10:14
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.adapter.objadapter;

/**
 * 功能描述:5V电压转换器 :适配器
 *
 * @author Songxianyang
 * @date 2022-03-12 10:14
 */
public class Change {
    private Source source = new Source();
    
    public String output5v() {
        String v220 = source.output();
        v220="5v";
        return v220;
    
    }
}

/*******************************************************************************
 * Package: com.example.demo.design.adapter.classadapter
 * Type:    Phone
 * Date:    2022-03-12 10:28
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.adapter.objadapter;

/**
 * 功能描述:手机充电
 *
 * @author Songxianyang
 * @date 2022-03-12 10:28
 */
public class Phone {
    private Change change= new Change();
    
    public void chargePhone() {
        System.out.println("我得手机以及充上"+change.output5v());
    }
}

/*******************************************************************************
 * Package: com.example.demo.design.adapter
 * Type:    Source
 * Date:    2022-03-12 10:03
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.adapter.objadapter;

/**
 * 类适配器
 * 功能描述:电源类220v   被适配器
 *
 * @author Songxianyang
 * @date 2022-03-12 10:03
 */
public class Source {
    public String output() {
        return "220v";
    }
}

/*******************************************************************************
 * Package: com.example.demo.design.adapter.classadapter
 * Type:    Test
 * Date:    2022-03-12 10:34
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.adapter.objadapter;

/**
 * 功能描述:
 *
 * @author Songxianyang
 * @date 2022-03-12 10:34
 */
public class Test {
    public static void main(String[] args) {
        Phone phone = new Phone();
        phone.chargePhone();
    }
}

在这里插入图片描述

接口适配器

在这里插入图片描述

结构:

在这里插入图片描述

代码以及优化
/*******************************************************************************
 * Package: com.example.demo.design.adapter.interfaceadapter
 * Type:    AVoltageAdapter
 * Date:    2022-03-12 15:48
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.adapter.interfaceadapter;

/**
 * 功能描述:抽象的一个多种电压 初始化
 *
 * @author Songxianyang
 * @date 2022-03-12 15:48
 */
public abstract class AbsVoltageAdapter implements IVoltageAdapter {
    
    @Override
    public String v5() {
        return null;
    }
    
    @Override
    public String v21() {
        return null;
    }
}

/*******************************************************************************
 * Package: com.example.demo.design.adapter.interfaceadapter
 * Type:    V
 * Date:    2022-03-12 15:47
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.adapter.interfaceadapter;

/**
 * 功能描述: 多种电压 接口
 *
 * @author Songxianyang
 * @date 2022-03-12 15:47
 */
public interface IVoltageAdapter {
    /**
     * 充手机
     * @return s
     */
    public String v5();
    
    /**
     * 充电车
     * @return s
     */
    String v21();
    /*
    ......
     */
}

/*******************************************************************************
 * Package: com.example.demo.design.adapter.interfaceadapter
 * Type:    Phone
 * Date:    2022-03-12 15:56
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.adapter.interfaceadapter;

/**
 * 功能描述:手机需要5v电压
 *
 * @author Songxianyang
 * @date 2022-03-12 15:56
 */
public class Phone {
    private AbsVoltageAdapter abs;
    
    public Phone(AbsVoltageAdapter abs) {
        this.abs = abs;
    }
    
    public void change() {
        System.out.println("小米手机已经充上"+abs.v5());
    }
}

/*******************************************************************************
 * Package: com.example.demo.design.adapter.interfaceadapter
 * Type:    PhoneAdapter
 * Date:    2022-03-12 15:53
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.adapter.interfaceadapter;

/**
 * 功能描述:手机适配器5v
 * 优化点
 * @author Songxianyang
 * @date 2022-03-12 15:53
 */
public class PhoneAdapter extends AbsVoltageAdapter {
    @Override
    public String v5() {
        return "5v";
    }
}

/*******************************************************************************
 * Package: com.example.demo.design.adapter.classadapter
 * Type:    Test
 * Date:    2022-03-12 10:34
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.adapter.interfaceadapter;

/**
 * 功能描述:
 *
 * @author Songxianyang
 * @date 2022-03-12 10:34
 */
public class Test {
    public static void main(String[] args) {
        Phone phone = new Phone(new PhoneAdapter());
        phone.change();
    }
}

在这里插入图片描述

源码分析 mvc

在这里插入图片描述

源码 分析

DispatcherServlet dispatcherServlet = new DispatcherServlet();
DispatcherServlet:{
	方法:
	doDispatch(HttpServletRequest request){
		// 获取 request
		HttpServletRequest processedRequest = request;
		// 执行器对象:Handler
        HandlerExecutionChain mappedHandler = null;
        // 获取映射执行器对象
        mappedHandler = this.getHandler(processedRequest);
        // 获取具体的 HandlerAdapter 的适配器
        HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());
        // 不同的适配器 返回不同的 mv
        mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

		//被调用的代码
		//遍历获取具体的handler 并返回
		protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
        if (this.handlerAdapters != null) {
            Iterator var2 = this.handlerAdapters.iterator();

            while(var2.hasNext()) {
                HandlerAdapter adapter = (HandlerAdapter)var2.next();
                if (adapter.supports(handler)) {
                    return adapter;
                }
            }
        }

        throw new ServletException("No adapter for handler [" + handler + "]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler");
    }

	}
	//接口 :具体的实现 有6个适配器
	public interface HandlerAdapter {
	
	handler 标记
    boolean supports(Object handler);

    @Nullable
    ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;

   
    @Deprecated
    long getLastModified(HttpServletRequest request, Object handler);
}
}

在这里插入图片描述 适配器到此结束; 在这里插入图片描述