适配器模式

x33g5p2x  于2022-03-08 转载在 其他  
字(3.9k)|赞(0)|评价(0)|浏览(233)

1.概念

将一个类的接口转换成客户希望的另一个接口,适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。适配器的主要功能是进行转换匹配,目的是复用已有的功能,而不是来实现新的接口,当然适配器里面也可以实现功能,我们称这种适配器为智能适配器。

2.适配器模式案例

1.log实体类

package com.yl.design;

import java.io.Serializable;
import java.util.Date;

public class Log implements Serializable {
    private String id;
    private String operateUser;
    private String content;
    private Date date;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getOperateUser() {
        return operateUser;
    }

    public void setOperateUser(String operateUser) {
        this.operateUser = operateUser;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return "Log{" +
                "id='" + id + '\'' +
                ", operateUser='" + operateUser + '\'' +
                ", content='" + content + '\'' +
                ", date=" + date +
                '}';
    }
}

2.LogService(第一版的功能)

package com.yl.design;

import java.util.List;

public interface LogService {
    public List<Log> readLogList();
    public void writeLog(List<Log> list);
}

3.LogServiceImpl

package com.yl.design;

import java.io.*;
import java.util.List;

public class LogServiceImpl implements LogService {
    private static  final String logFileName = "adapter.log";
    //读取日志文件内容
    @Override
    public List<Log> readLogList() {
        List<Log> list = null;
        ObjectInputStream ois = null;
        File file = new File(logFileName);
        try {
            if (file.exists()) {
                ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
                list = (List<Log>) ois.readObject();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    //将内容写入到日志文件
    @Override
    public void writeLog(List<Log> list) {
        File file = new File(logFileName);
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
            oos.writeObject(list);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

4.在根目录下创建adapter.log文件

5.第一版功能的测试(主要是写和读两个功能)

6.第二版LogDBService

package com.yl.design;
import java.util.List;

public interface LogDBService {
    public void addLog(Log log);
    public void updateLog(Log log);
    public void removeLog(Log log);
    public List<Log> getAllLogs();
}

7.适配器

package com.yl.design;

import java.util.List;

// 适配器对象
public class Adapter implements LogDBService {

    // 持有需要被适配的接口对象
    private LogService adaptee;

    public Adapter(LogService adaptee) {
        this.adaptee = adaptee;
    }

    @Override
    public void addLog(Log log) {
        //读取日志文件的内容
        List<Log> logs = adaptee.readLogList();
        //加入新的日志对象
        logs.add(log);
        //将内容写入到日志文件中
        adaptee.writeLog(logs);
    }

    @Override
    public void updateLog(Log log) {
        List<Log> logs = adaptee.readLogList();
        for (int i = 0; i < logs.size(); i++) {
            if (logs.get(i).getId().equals(log.getId())) {
                //修改
                logs.set(i,log);
            }
        }
        adaptee.writeLog(logs);
    }

    @Override
    public void removeLog(Log log) {
        List<Log> logs = adaptee.readLogList();
        //删除
        logs.remove(log);
        adaptee.writeLog(logs);
    }

    @Override
    public List<Log> getAllLogs() {
        return adaptee.readLogList();
    }
}

8.测试(第二版功能,在复用第一版的功能上又添加了其它功能)

package com.yl.design;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class test {
    public static void main(String[] args) {
//        //第一版
        Log log = new Log();
        log.setId("L0001");
        log.setOperateUser("root");
        log.setContent("view function");
        log.setDate(new Date());
        List<Log> list = new ArrayList<>();
        list.add(log);
        LogService logService = new LogServiceImpl();
        //写入日志
        logService.writeLog(list);
        //读取日志
        List<Log> logs = logService.readLogList();
        System.out.println(logs);
        //第二版
        Log log = new Log();
        log.setId("L0002");
        log.setOperateUser("admin");
        log.setContent("qingfen");
        log.setDate(new Date());
        //创建被适配的对象
        LogService logService = new LogServiceImpl();
        //创建适配器对象
        Adapter adapter = new Adapter(logService);
        adapter.addLog(log);
        List<Log> allLogs = adapter.getAllLogs();
        System.out.println(allLogs);
    }
}

相关文章