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 对象适配器
  • 相关阅读:
    Error C1189: #error: Please use the /MD switch for _AFXDLL builds
    block,inline和inline-block概念和区别(转载)
    jQuery学习--Code Organization Concepts
    Kafka— —副本(均衡负载)
    Kafka实践1--Producer
    漫画HDFS工作原理(转)
    JavaScript学习笔记3
    搭建简单SBT工程实践
    Hive SQL测试
    SparkSql常用语句
  • 原文地址:https://www.cnblogs.com/ou-pc/p/7182375.html
Copyright © 2011-2022 走看看