• 设计模式之 七(Adapter)


    package com.anby.Adapter;
    
    import java.awt.List;
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.sql.Date;
    import java.util.ArrayList;
    
    import javax.print.attribute.standard.DateTimeAtCompleted;
    
    /**
     * 适配器模式: 为了拓展之前的功能: 之前:之前LogFileOperationApi接口只实现了readFileLog,writeFileLog
     * 为了需求现有功能需要扩展,
     * 现在:那么增加了LogDbOperateApi实现createLog,updateLog,removeLog,getAllLog
     * 
     * 思路:1,将已有的功能封装到适配器中Adapter
     *       2, Adapter中接受已有的功能类,
     *       3,重新封装功能
     *     
     * @author Anby
     * 
     */
    public class AdapterDemo
    {
        public static void main(String[] args)
        {
            LogModel lml=new LogModel();
            lml.setLogid("001");
            lml.setOperateuser("admin");
            lml.setOperatetime("2010-03-02");
            lml.setLogcontent("test!");
            ArrayList<LogModel>  list=new ArrayList<LogModel>();
            list.add(lml);
            
            LogFileOperationApi logFileApi=new LogFileOperation("");
            
            LogDbOperateApi  api=new Adapter(logFileApi);
            
            api.createLog(lml);
            
           java.util.List<LogModel> alllog=(java.util.List<LogModel>) api.getAllLog();
           
            
        }
    }
    
    //模板类
    class LogModel
    {
        private String logid;
        private String operateuser;
        private String operatetime;
        private String logcontent;
    
        public String getLogid()
        {
            return logid;
        }
    
        public void setLogid(String logid)
        {
            this.logid = logid;
        }
    
        public String getOperateuser()
        {
            return operateuser;
        }
    
        public void setOperateuser(String operateuser)
        {
            this.operateuser = operateuser;
        }
    
        public String getOperatetime()
        {
            return operatetime;
        }
    
        public void setOperatetime(String operatetime)
        {
            this.operatetime = operatetime;
        }
    
        public String getLogcontent()
        {
            return logcontent;
        }
    
        public void setLogcontent(String logcontent)
        {
            this.logcontent = logcontent;
        }
    
        public LogModel(String logid, String operateuser, String operatetime,
                String logcontent)
        {
            this.logid = logid;
            this.operateuser = operateuser;
            this.operatetime = operatetime;
            this.logcontent = logcontent;
        }
    
        public LogModel()
        {
        }
    
        public String toString()
        {
            return "LogMode [logid=" + logid + ", operateuser=" + operateuser
                    + ", operatetime=" + operatetime + ", logcontent=" + logcontent
                    + "]";
        }
    
    }
    
    //之前的功能接口
    interface LogFileOperationApi
    {
        public java.util.List<LogModel> readFileLog() throws Exception;
    
        public void writeFileLog(java.util.List<LogModel> list);
    }
    //实现接口的方法
    class LogFileOperation implements LogFileOperationApi
    {
    
        private String FilePath;
    
        public LogFileOperation(String filePath)
        {
            if (FilePath != null && FilePath.trim().length() > 0)
            {
                FilePath = filePath;
            }
    
        }
    
        public java.util.List<LogModel> readFileLog() throws Exception
        {
            java.util.List<LogModel> list = null;
            FileReader fileReader = new FileReader("c:\\test.txt");
            char[] buffer = new char[1024];
            int ch = 0;
            while ((ch = fileReader.read(buffer)) != -1)
            {
            }
            fileReader.read();
            return list;
    
        }
    
        public void writeFileLog(java.util.List<LogModel> list)
        {
    
            File file = new File(FilePath);
            ObjectOutputStream oout = null;
            try
            {
                oout = new ObjectOutputStream(new BufferedOutputStream(
                        new FileOutputStream(file)));
                oout.writeObject(list);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                try
                {
                    oout.close();
                }
                catch (Exception e2)
                {
                    e2.printStackTrace();
                }
            }
        }
    
    }
    
    //后期扩展
    interface LogDbOperateApi
    {
        public void createLog(LogModel lm);
    
        public void updateLog(LogModel lm);
    
        public void removeLog(LogModel lm);
    
        public java.util.List<LogModel> getAllLog();
    }
    //扩展接口的实现类
    class Adapter implements LogDbOperateApi
    {
    
        //传入之前的接口对象
        private LogFileOperationApi adaptee;
    
        public Adapter(LogFileOperationApi adaptee)
        {
            this.adaptee = adaptee;
        }
    
        //利用之前接口的方法重新封装日志处理
        public void createLog(LogModel lm)
        {
            // 1,先读取文件的内容;
            java.util.List<LogModel> list;
            try
            {
                list = adaptee.readFileLog();
                list.add(lm);
                adaptee.writeFileLog(list);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
    
        }
    
        public void updateLog(LogModel lm)
        {
    
            java.util.List<LogModel> list;
            try
            {
                list = adaptee.readFileLog();
                for (int i = 0; i < list.size(); i++)
                {
                    if (list.get(i).getLogid().equals(lm.getLogid()))
                        ;
                    list.set(i, lm);
                    break;
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
    
        }
    
        public void removeLog(LogModel lm)
        {
    
            // 1,读取文件的内容
            java.util.List<LogModel> list;
            try
            {
                list = adaptee.readFileLog();
                // 2,删除相应的日志对象
                list.remove(lm);
                adaptee.writeFileLog(list);
            }
            catch (Exception e)
            {
    
                e.printStackTrace();
            }
    
        }
    
        public java.util.List<LogModel> getAllLog()
        {
            try 
            {
                return  adaptee.readFileLog();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return null;
        }
    
    }
  • 相关阅读:
    UVA 11198 Dancing Digits
    UVA 10085 The most distant state
    UVA 321 The New Villa
    UVA 10422 Knights in FEN
    poj2876
    poj2895
    poj2914
    poj2892
    poj2941
    LD SDK LDCControlDll 中 CXMLFile的进一步使用方法,建议以后改进
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/3056247.html
Copyright © 2020-2023  润新知