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

    适配器模式:

    将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以工作。

    /**
     * 适配器模式,组合
     */
    public class AdapterTest {
        public static void main(String[] args) {
            Target target = new Adapter(new Adaptee());
            target.output5v();
        }
    }
    
    class Adaptee{
        public int output220v(){
            return 220;
        }
    }
    
    interface Target{
        int output5v();
    }
    
    class Adapter implements Target {
        private Adaptee adaptee;
    
        public Adapter(Adaptee adaptee) {
            this.adaptee = adaptee;
        }
    
        @Override
        public int output5v() {
            int i = adaptee.output220v();
            //....
            System.out.println(String.format("原始电压:%d v   - >  输出电压: %d v  ",i,5));
            return 5;
        }
    }
  • 相关阅读:
    servlet
    反射
    网络通信协议
    线程安全,
    线程池, Callable<V>接口
    Thread类,Runnable 接口
    commons-IO
    序列化流与反序列化流,打印流
    转换流,Properties 集合
    缓冲流
  • 原文地址:https://www.cnblogs.com/chenfx/p/14788054.html
Copyright © 2011-2022 走看看