美文网首页
Adapter模式(设计模式)

Adapter模式(设计模式)

作者: DarknessShadow | 来源:发表于2020-05-09 17:36 被阅读0次

对象适配器模式(使用委托的适配器)

个人理解

用一个比喻来描述比较好,比如:中国现在用电的标准电压是220V交流电,但是我的手机只能使用20V的电压,我的需求就是需要中间有一个转换器把220V电压转化为20V的电压供我的手机使用。实际情况:220V,需求:20V,转换:适配器。适配器模式可以使用到很多场景中,比如:API升级兼容以往的版本

上代码,看代码实现,摸索思路更好一点

类适配器模式(使用继承的适配器)

FileIO.java

package practice;
import java.io.IOException;
/**
 * @Author:DarknessShadow
 * @description:需求方(需要适配)
 * @date 2020/5/9 14:04
 */
public interface FileIO {
    public void readFromFile(String fileName) throws IOException;

    public void writeToFile(String fileName) throws IOException;

    public void setValue(String key, String value);

    public String getValue(String key);
}

FileProperties.java

package practice;
import java.io.*;
import java.util.Properties;
/**
 * @Author:DarknessShadow
 * @description:适配器(做中间功能转换)
 * @date 2020/5/9 14:04
 */
public class FileProperties extends Properties implements FileIO {

    public FileProperties() {
    }

    @Override
    public void readFromFile(String fileName) throws IOException {
        load(new FileInputStream(new File(fileName)));
        System.out.println(getProperty("year"));
    }

    @Override
    public void writeToFile(String fileName) throws IOException {
        OutputStream out = new FileOutputStream(fileName);
        store(out, "properties增强型适配器");
    }

    @Override
    public void setValue(String key, String value) {
        setProperty(key,value);
    }

    @Override
    public String getValue(String key) {
        return getProperty(key);
    }
}

Main.java

package practice;
import java.io.IOException;
/**
 * @Author:DarknessShadow
 * @description:调用者,java.util.Properties是被适配者
 * @date 2020/5/9 14:03
 */
public class Main {

    public static void main(String[] args) throws IOException {
        FileIO io = new FileProperties();
        io.readFromFile("src/file.txt");
        io.setValue("year", "2004");
        io.setValue("month", "4");
        io.setValue("day", "21");
        io.writeToFile("src/newfile.txt");
    }
}

对象适配器模式(使用委托的适配器)

把FileIO和FileProperties类改改

package practice;
import java.io.IOException;
/**
 * @Author:DarknessShadow
 * @description:需求方(需要适配)
 * @date 2020/5/9 14:04
 */
public abstract class FileIO {
    public abstract void readFromFile(String fileName) throws IOException;

    public abstract void writeToFile(String fileName) throws IOException;

    public abstract void setValue(String key, String value);

    public abstract String getValue(String key);
}
package practice;
import java.io.*;
import java.util.Properties;
/**
 * @Author:DarknessShadow
 * @description:适配器(做中间功能转换)
 * @date 2020/5/9 14:04
 */
public class FileProperties extends FileIO {

    private Properties properties;
    public FileProperties() {
        this.properties = new Properties();
    }

    @Override
    public void readFromFile(String fileName) throws IOException {
        load(new FileInputStream(new File(fileName)));
        System.out.println(properties.getProperty("year"));
    }

    @Override
    public void writeToFile(String fileName) throws IOException {
        OutputStream out = new FileOutputStream(fileName);
        properties.store(out, "properties增强型适配器");
    }

    @Override
    public void setValue(String key, String value) {
        properties.setProperty(key,value);
    }

    @Override
    public String getValue(String key) {
        return properties.getProperty(key);
    }
}

类图


类适配模式
对象适配模式

总结:设计模式是一种思维的不断总结的结晶,不能固化思维按照书本的代码就把这个模式定位为这种样式.自我感觉是学习里面解决一些场景的解决方案.迭代器模式:就是在一个容器里面,你想要去遍历或者做其他操作,但是又不想在容器里面操作实现两者的分离,可以使用组合或者聚合的方式定义一个新的类去实现你想要的操作.适配器模式:感觉这种思维在计算机里面使用的很广泛,最好的案例就是Java虚拟机,让Java可以跨平台运行.它就是一个巨大复杂的适配器,上层的Java语法编写的代码不需要动,只需要编写适配不同的系统平台的Java虚拟机就可以了.

相关文章

  • 11.3设计模式-适配器-详解

    设计模式-适配器adapter模式 adapter模式详解 adapter模式在android中的实际运用1.li...

  • 浅谈设计模式之适配器模式

    适配器模式(Adapter Pattern) 概述: 在设计模式中,适配器模式(adapter pattern)有...

  • Adapter模式(设计模式)

    对象适配器模式(使用委托的适配器) 个人理解 用一个比喻来描述比较好,比如:中国现在用电的标准电压是220V交流电...

  • 简说设计模式之适配器模式

    前言:对于设计模式基础概念可以去看[简说设计模式之设计模式概述] 一、什么是适配器模式 适配器模式(Adapter...

  • 设计模式-adapter设计模式

    效果图 1.定义 将一种对象适配成另一种对象 2.示例 3.使用 listview的适配器 4.总结 1.适配器模...

  • 浅谈GoF23设计模式-“Adapter”模式

    “Adapter”模式为结构型设计模式,C#当中主要使用对象适配器。“Adapter”模式定义:将一个类的接口转换...

  • 设计模式-Adapter

    一,Adapter模式详解 适配器模式定义将一个接口转换成客户希望的另一个接口,适配器模式使接口不兼容的那些类一起...

  • Adapter 设计模式

    adapter 模式详解 类的适配器模式 把适配的类的 API转换为目标类的API。 对象适配器对象的适配器模式 ...

  • 6、结构型模式-适配器设计模式

    1、接口之间的桥梁-适配器设计模式 简介:讲解Adapeter设计模式和应用场景 适配器模式(Adapter Pa...

  • Typescript 适配器模式(Adapter)

    标签: 前端 设计模式 适配器模式 typescript Adapter 请仔细阅读下面代码,理解其中的设计理念。...

网友评论

      本文标题:Adapter模式(设计模式)

      本文链接:https://www.haomeiwen.com/subject/llflnhtx.html