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

    package com.bjsxt.adapter;
    
    /**
     * 被适配的类
     * (相当于例子中的,PS/2键盘)
     * @author Administrator
     *
     */
    public class Adaptee {
        
        public void request(){
            System.out.println("可以完成客户请求的需要的功能!");
        }
    }
    Adaptee
    package com.bjsxt.adapter;
    
    /**
     * 客户端类
     * (相当于例子中的笔记本,只有USB接口)
     * @author Administrator
     *
     */
    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();
    
            Target t = new Adapter2(a);
            
            c.test1(t);
            
        }
        
    }
    Client
    package com.bjsxt.adapter;
    
    public interface Target {
        void handleReq();
    }
    Target
    package com.bjsxt.adapter;
    
    /**
     * 适配器 (类适配器方式)
     * (相当于usb和ps/2的转接器)
     * @author Administrator
     *
     */
    public class Adapter extends Adaptee implements Target {
        
        
        @Override
        public void handleReq() {
            super.request();
        }
        
    }
    adapter 类适配器
    package com.bjsxt.adapter;
    
    /**
     * 适配器 (对象适配器方式,使用了组合的方式跟被适配对象整合)
     * (相当于usb和ps/2的转接器)
     * @author Administrator
     *
     */
    public class Adapter2  implements Target {
        
        private Adaptee adaptee;
        
        @Override
        public void handleReq() {
            adaptee.request();
        }
    
        public Adapter2(Adaptee adaptee) {
            super();
            this.adaptee = adaptee;
        }
        
        
        
    }
    Adapter 对象适配器
  • 相关阅读:
    CF538H Summer Dichotomy
    CF1558F Strange Sort
    CF429E Points and Segments
    CF986F Oppa Funcan Style Remastered
    [JOI Open 2016] 摩天大楼
    [做题笔记] 浅谈笛卡尔树结构的应用
    CF1383C String Transformation 2
    CF1558E Down Below
    weex打包安卓艰苦之路
    IntelliJ IDEA 推荐15款插件
  • 原文地址:https://www.cnblogs.com/ou-pc/p/7182375.html
Copyright © 2011-2022 走看看