zoukankan      html  css  js  c++  java
  • 设计模式之适配器模式 Adapter

    从现在开始,将转入设计模式中的结构型模式

    定义与角色

    工作场景

    代码实现

    /**
     * 被适配的类--相当于键盘
     * @author bzhx
     * 2017年3月10日
     */
    public class Adaptee {
    
        public void request(){
            System.out.println("可以完成客户请求的需要的功能");
        }
        
    }
    Adaptee类
    public interface Target {
        void handleReq();
    }
    Target 接口
    /**
     * 适配器(类适配器方式)--相当于usb转接器
     * @author bzhx
     * 2017年3月10日
     */
    public class Adapter extends Adaptee implements Target{
    
        @Override
        public void handleReq() {
            super.request();
        }
    
    }
    适配器类Adapter
    /**
     * 适配器(对象适配器,使用了组合方式跟被适配对象整合)--相当于usb转接器
     * @author bzhx
     * 2017年3月10日
     */
    public class Adapter2 implements Target{
    
        private Adaptee adaptee;
        
        @Override
        public void handleReq() {
            adaptee.request();
        }
    
        public Adapter2(Adaptee adaptee) {
            super();
            this.adaptee = adaptee;
        }
        
    }
    适配器类 Adapter2
    /**
     * 客户端类----相当于笔记本,只有usb接口
     * @author bzhx
     * 2017年3月10日
     */
    
    public class Client {
    
        public void test1(Target t){
            t.handleReq();
        }
        
        public static void main(String[] args) {
            Client c = new Client();
            Adaptee a = new Adaptee();
            
            Target t = new Adapter();
            c.test1(t);
        }
    }
    调用1
    /**
     * 客户端类----相当于笔记本,只有usb接口
     * @author bzhx
     * 2017年3月10日
     */
    
    public class Client2 {
    
        public void test1(Target t){
            t.handleReq();
        }
        
        public static void main(String[] args) {
            Client2 c = new Client2();
            Adaptee a = new Adaptee();
            
            Target t = new Adapter2(a);
            c.test1(t);
        }
    }
    调用2
  • 相关阅读:
    二、云计算openstack共享组件--时间同步服务ntp
    一、云计算openstack介绍
    五、Kvm虚拟机迁移
    四、Kvm虚拟化网络管理
    三、Kvm虚拟化存储管理
    二、kvm虚拟机管理
    一、kvm虚拟化介绍
    九、Linux网络技术管理及进程管理
    园主的码云网站,可以在里面查看园主的练习代码哦
    万能Makefile,前戏做足项目做起来才顺畅。
  • 原文地址:https://www.cnblogs.com/qingdaofu/p/7472713.html
Copyright © 2011-2022 走看看