研磨设计模式解析及python代码实现——(三)适配器模式(Adapter)
一、适配器模式定义
将一个类的接口转换成另外一个接口,适配器模式使得原本由于接口不兼容,而不能在一起工作的哪些类能够在一起工作。
二、python 实现
1 import string 2 import cPickle as p 3 import datetime 4 import os 5 class LogModel: 6 logId="" 7 operateUser="" 8 operateTime="" 9 logContent="" 10 11 def __init__(self,id,user,time,content): 12 self.logId=id 13 self.operateUser=user 14 self.operateTime=time 15 self.logContent=content 16 17 def setLogId(self,id): 18 self.logId=id 19 def setOperateUser(self,user): 20 self.operateUser=user 21 def setOperateTime(self,time): 22 self.operateTime=time 23 def setLogContent(self,content): 24 self.logContent=content 25 def getLogId(self): 26 return self.logId 27 def getOperateUser(self): 28 return self.operateUser 29 def getOperateTime(self): 30 return self.operateTime 31 def getLogContent(self): 32 return self.logContent 33 def toString(self): 34 return "logId=%s,operateUser=%s,operateTime=%s,logContent=%s" %(self.logId,self.operateUser,self.operateTime,self.logContent) 35 36 class LogFileOperateApi: 37 def readFile(self): 38 pass 39 def WriteLogFile(self,list): 40 pass 41 class LogFileOperate(LogFileOperateApi): 42 LogFilePath="AdapterLog.log" 43 def __init__(self,logFilePath): 44 if (logFilePath!=None)and(len(logFilePath.strip()) > 0): 45 self.LogFilePath=logFilePath 46 def readFile(self): 47 f = file(self.LogFilePath)#open(self.LogFilePath,‘r‘) 48 if os.stat(self.LogFilePath).st_size==0: 49 list=[] 50 else: 51 list=p.load(f) 52 f.close() 53 return list 54 def WriteLogFile(self,list): 55 f = open(self.LogFilePath, ‘w‘) 56 p.dump(list, f) 57 f.close() 58 class LogDbOperateApi: 59 def createLog(self,logModel): 60 pass 61 def updateLog(self,logModel): 62 pass 63 def removeLog(self,logModel): 64 pass 65 def getAllLog(self): 66 pass 67 class LogDbOperate(LogDbOperateApi): 68 def createLog(self,logModel): 69 print "Now LogDbOperate createLog , lm=%s" %(logModel) 70 def updateLog(self,logModel): 71 print "Now LogDbOperate updateLog , lm=%s" %(logModel) 72 def removeLog(self,logModel): 73 print "Now LogDbOperate removeLog , lm=%s" %(logModel) 74 def getAllLog(self): 75 print "Now in LogDbOperate getAllLog" 76 class TwoDirectAdapter(LogFileOperateApi,LogDbOperateApi): 77 fileLog=None 78 dbLog = None 79 def __init__(self,fileLog,dbLog): 80 self.fileLog=fileLog 81 self.dbLog=dbLog 82 def createLog(self,logModel): 83 list=self.fileLog.readFile() 84 list.append(logModel) 85 self.fileLog.WriteLogFile(list) 86 def updateLog(self,logModel): 87 list=self.fileLog.readFile() 88 for item in list: 89 if item.getLogId()==logModel.getLogId(): 90 item=logModel 91 break 92 self.fileLog.WriteLogFile(list) 93 def removeLog(self,logModel): 94 list=self.fileLog.readFile() 95 for item in list: 96 if item.getLogId()==logModel.getLogId(): 97 del item 98 break 99 self.fileLog.WriteLogFile(list) 100 def getAllLog(self): 101 return self.fileLog.readFile() 102 def readFile(self): 103 return self.dbLog.getAllLog() 104 def WriteLogFile(self,list): 105 for item in list: 106 self.dbLog.createLog(item) 107 #main function 108 LogFilePath="AdapterLog.log" 109 f=open(LogFilePath,‘w+‘) 110 f.truncate() 111 lml=LogModel("001","admin",datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),"This is a test!").toString() 112 list=[] 113 list.append(lml) 114 115 file_api= LogFileOperate("") 116 db_api= LogDbOperate() 117 file_api2=TwoDirectAdapter(file_api,db_api) 118 db_api2=TwoDirectAdapter(file_api,db_api) 119 db_api2.createLog(lml) 120 121 allLog=db_api2.getAllLog() 122 print "allLog:", 123 print allLog 124 file_api2.WriteLogFile(list) 125 file_api2.readFile()
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。