• Adapter pattern


    把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。

    具体例子来说,我们都用过充电器,有些充电器的接口是圆形的,有些是方形的, 那如果我要在圆形的插座上面使用方形的

    充电器,是应该买一个圆形的充电器还是买一个转换器将圆形变成方形?

    public class CylindricalPlug {

    public String supply(String cylinStem1, String cylinStem2) {

    String result = cylinStem1 + cylinStem2;

    DebugLog.log("the input is" + result);

    return result;

    }

    }

     
     
    public class RectangularPlug {
    private String rectaStem1;
    private String rectaStem2;
     
    public RectangularPlug(String rectaStem1, String rectaStem2) {
    super();
    this.rectaStem1 = rectaStem1;
    this.rectaStem2 = rectaStem2;
    }
     
    public String getRectaStem1() {
    return rectaStem1;
    }
     
    public void setRectaStem1(String rectaStem1) {
    this.rectaStem1 = rectaStem1;
    }
     
    public String getRectaStem2() {
    return rectaStem2;
    }
     
    public void setRectaStem2(String rectaStem2) {
    this.rectaStem2 = rectaStem2;
    }
     
    public void getPower() {
    RectangularAdapter adapter = new RectangularAdapter();
    String power = adapter.adapt(rectaStem1, rectaStem2);
    }
    }
     
     
    public class RectangularAdapter extends CylindricalPlug {
    public String adapt(String rectaStem1, String rectaStem2) {
    // some conversion logic
    DebugLog.log("before converter");
    String cylinStem1 = rectaStem1+"Cylindrical input";
    String cylinStem2 = rectaStem2+"Cylindrical input";
    DebugLog.log("after converter");
    return supply(cylinStem1, cylinStem2);
    }
     
    }
     
    public class Test {
     
    /**
    * @param args
    */
    public static void main(String[] args) {
    RectangularPlug rPlug=new RectangularPlug("rectangular input 1","rectangular input 2");
    rPlug.getPower();
    }
     
    }
     
    虽然上面的代码实现转换的功能,但是作为一个合格的程序员是不能允许上面的代码存在这个地球上的,因为既不
    满足dip,又不满足ocp.
     
    public interface IAdapter {
    public String adapt(iSupply isupply, String rectaStem1, String rectaStem2);
    }
     
     
    public interface iSupply {
    String supply(String Stem1, String Stem2);
    }
     
    public class CylindricalPlug implements iSupply {
    public String supply(String cylinStem1, String cylinStem2) {
    String result = cylinStem1 + cylinStem2;
    DebugLog.log("the input is" + result);
    return result;
    }
    }
     
    public class RectangularPlug {
    private String rectaStem1;
    private String rectaStem2;
        
     
    public RectangularPlug(String rectaStem1, String rectaStem2) {
    super();
    this.rectaStem1 = rectaStem1;
    this.rectaStem2 = rectaStem2;
    }
     
    public String getRectaStem1() {
    return rectaStem1;
    }
     
    public void setRectaStem1(String rectaStem1) {
    this.rectaStem1 = rectaStem1;
    }
     
    public String getRectaStem2() {
    return rectaStem2;
    }
     
    public void setRectaStem2(String rectaStem2) {
    this.rectaStem2 = rectaStem2;
    }
     
    public void getPower(iSupply isupply,IAdapter adapter) {
    adapter.adapt(isupply,rectaStem1, rectaStem2);
    }
    }
     
    public class RectangularAdapter implements IAdapter {
     
    public String adapt(iSupply isupply, String rectaStem1, String rectaStem2) {
    DebugLog.log("before converter");
    String cylinStem1 = rectaStem1+"Cylindrical input";
    String cylinStem2 = rectaStem2+"Cylindrical input";
    DebugLog.log("after converter");
    return isupply.supply(cylinStem1, cylinStem2);
    }
     
    }
     
     
    public class Test {
     
    /**
    * @param args
    */
    public static void main(String[] args) {
    iSupply supply = new CylindricalPlug();
    IAdapter adapter = new RectangularAdapter();
    RectangularPlug rPlug = new RectangularPlug("rectangular input 1",
    "rectangular input 2");
    rPlug.getPower(supply, adapter);
    }
     
    }
     
     
     
  • 相关阅读:
    Java中使用Lua脚本语言(转)
    lua、groovy嵌入到java中的性能对比(转)
    有人实践过 Phabricator 以及 Arcanist 作为 code review 的工具么?(转)
    Lua Development Tools (LDT)
    海量数据处理面试题集锦
    三层架构之抽象工厂加反射----实现数据库转换
    八卦一下
    java实现各种数据统计图(柱形图,饼图,折线图)
    activity-alias的使用
    C#反射Assembly 具体说明
  • 原文地址:https://www.cnblogs.com/budoudou/p/2299879.html
Copyright © 2020-2023  润新知