适配器模式(Adapter)
考虑一个记录日志的应用,由于用户对日志记录的要求很高,使得开发人员不能简单地采用一些已有的日志工具或日志框架来满足用户的要求,而需要按照用户的要求重新开发新的日志管理系统,如需要用文件和数据库形式分别保存日志数据。
适配器模式的定义是将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
public classLogModel {
privateString logId;
privateString operateUser;
privateString operateTime;
privateString logContent;
publicString getLogId(){
return logId;
}
publicvoidsetLogId(String logId){
this.logId= logId;
}
publicString getOperateUser(){
return operateUser;
}
publicvoidsetOperateUser(String operateUser){
this.operateUser=operateUser;
}
publicString getOperateTime(){
return operateTime;
}
publicvoidsetOperateTime(String operateTime){
this.operateTime=operateTime;
}
publicString getLogContent(){
return logContent;
}
publicvoidsetLogContent(String logContent){
this.logContent=logContent;
}
}
import java.util.List;
public interfaceLogFileOperateApi {
publicList<LogModel> readLogFile();
publicvoidwriteLogFile(List<LogModel> list);
}
import java.util.List;
public interfaceLogDbOperateApi {
publicvoidcreateLog(LogModel lm);
publicvoidupdateLog(LogModel lm);
publicvoidremoveLog(LogModel lm);
publicList<LogModel>getAllLog();
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
public classLogFileOperate implements LogFileOperateApi{
private String logFilePathName = "test.log";
public LogFileOperate(StringlogFilePathName){
if(logFilePathName!=null &&logFilePathName.trim().length()>0){
this.logFilePathName=logFilePathName;
}
}
@Override
public List<LogModel>readLogFile() {
// TODO Auto-generated method stub
List<LogModel>list = null;
ObjectInputStreamoin = null;
try{
Filef = newFile(logFilePathName);
if(f.exists()){
oin=new ObjectInputStream(new BufferedInputStream(new FileInputStream(f)));
list=(List<LogModel>)oin.readObject();
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(oin!=null){
oin.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
return list;
}
@Override
public voidwriteLogFile(List<LogModel> list) {
// TODO Auto-generated method stub
Filef=newFile(logFilePathName);
ObjectOutputStreamoout=null;
try{
oout=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
oout.writeObject(list);
}catch(IOException e){
e.printStackTrace();
}finally{
try{
oout.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
import java.util.List;
public classLogDbOperate implements LogDbOperateApi{
@Override
public void createLog(LogModel lm){
// TODO Auto-generated method stub
System.out.println("now in LogDbOperate createLog,lm="+lm);
}
@Override
public void updateLog(LogModel lm){
// TODO Auto-generated method stub
System.out.println("now in LogDbOperate updateLog,lm="+lm);
}
@Override
public void removeLog(LogModel lm){
// TODO Auto-generated method stub
System.out.println("now in LogDbOperate removeLog,lm="+lm);
}
@Override
public List<LogModel>getAllLog() {
// TODO Auto-generated method stub
System.out.println("now in LogDbOperate getAllLog");
return null;
}
}
import java.util.List;
public classTwoDirectAdapter implements LogDbOperateApi,LogFileOperateApi{
private LogFileOperateApi fileLog;
private LogDbOperateApi dbLog;
publicTwoDirectAdapter(LogFileOperateApi fileLog,LogDbOperateApi dbLog){
this.fileLog=fileLog;
this.dbLog=dbLog;
}
@Override
public List<LogModel>readLogFile() {
// TODO Auto-generated method stub
return null;
}
@Override
public voidwriteLogFile(List<LogModel> list) {
// TODO Auto-generated method stub
}
@Override
public void createLog(LogModel lm){
// TODO Auto-generated method stub
List<LogModel>list=fileLog.readLogFile();
list.add(lm);
fileLog.writeLogFile(list);
}
@Override
public void updateLog(LogModel lm){
// TODO Auto-generated method stub
}
@Override
public void removeLog(LogModel lm){
// TODO Auto-generated method stub
List<LogModel>list=fileLog.readLogFile();
list.remove(lm);
fileLog.writeLogFile(list);
}
@Override
public List<LogModel>getAllLog() {
// TODO Auto-generated method stub
return fileLog.readLogFile();
}
}
import java.util.ArrayList;
import java.util.List;
public classClient {
publicstaticvoidmain(String[] args){
LogModel lm = new LogModel();
lm.setLogId("001");
lm.setOperateUser("admin");
lm.setOperateTime("2010-03-22");
lm.setLogContent("this is just for testing on purpose");
List<LogModel> list=newArrayList<LogModel>();
list.add(lm);
LogFileOperateApi fileLogApi=new LogFileOperate("");
LogDbOperateApi dbLogApi=new LogDbOperate();
LogFileOperateApi fileLogApi2=newTwoDirectAdapter(fileLogApi,dbLogApi);
LogDbOperateApi dbLogApi2=newTwoDirectAdapter(fileLogApi,dbLogApi);
dbLogApi2.createLog(lm);
List<LogModel>allLog=dbLogApi2.getAllLog();
System.out.println(allLog);
fileLogApi2.writeLogFile(list);
fileLogApi2.readLogFile();
}
}