設(shè)計模式試驗--適配器模式、裝飾模式_第1頁
設(shè)計模式試驗--適配器模式、裝飾模式_第2頁
設(shè)計模式試驗--適配器模式、裝飾模式_第3頁
設(shè)計模式試驗--適配器模式、裝飾模式_第4頁
設(shè)計模式試驗--適配器模式、裝飾模式_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、浙江工商大學計算機與信息工程學院 學期 上機實驗報告課程名稱: 設(shè)計模式 姓 名: 學 號: 指導教師: 班 級: 日 期: 【一】實驗?zāi)康模海?) 掌握結(jié)構(gòu)型設(shè)計模式的意圖、使用方法和優(yōu)缺點,包括適配器模式、裝飾模式、外觀模式?!径繉嶒瀮?nèi)容:(1) 假設(shè)某系統(tǒng)中使用了一個第三方的日志記錄工具,該日志記錄工具支持數(shù)據(jù)庫日志記錄DatabaseLog和文本文件記錄FileLog兩種方式,如圖1所示,它提供給的API接口是Write()方法,使用方法如下:Log.Write("Logging Message!");圖1 由于某種原因不能繼續(xù)使用該日志記錄工具了,需要采用另外一

2、個日志記錄工具,它同樣也支持數(shù)據(jù)庫日志記錄DatabaseLog和文本文件記錄FileLog兩種方式,如圖2所示,只不過它提供的API接口是WriteLog()方法,使用方法如下:Log.WriteLog("Logging Message!");圖2現(xiàn)要求設(shè)計一個解決方案,使得在不修改原有程序的基礎(chǔ)上,能實現(xiàn)日志工具的替換。(2)Java I/O包的類關(guān)系圖如圖3所示,其中FilterInputSteam為裝飾類。要求編寫一個裝飾者,把輸入流內(nèi)的所有小寫字符轉(zhuǎn)換為大寫字符。圖3 【三】完成報告(預備知識、步驟、程序框圖、程序、思考等):第一題UML類圖:Java代碼:Cli

3、entDemo.java/* * author * * */public class ClientDemo public static void main (String args)ILog newLog = new Adapter();newLog.write();ILog.java/* * author * * */public interface ILog public void write();DatabaseLog.java/* * author * * */public class DatabaseLog implements ILog public void write()Sys

4、tem.out.println("數(shù)據(jù)庫日志記錄");FileLog.java/* * author * * */public class FileLog implements ILog public void write()System.out.println("文本文件記錄");Adapter.java/* * author * * */public class Adapter extends LogAdaptee implements ILog public void write()super.writeLog();System.out.print

5、ln("適配器轉(zhuǎn)換。");LogAdaptee.java/* * author * * */public interface LogAdaptee public void writeLog();DatabaseLogAdaptee.java/* * author * * */public class DatabaseLogAdaptee implements LogAdapteepublic void writeLog()System.out.println("轉(zhuǎn)換后的數(shù)據(jù)庫日志記錄");FileLogAdaptee.java/* * author *

6、* */public class DatabaseLogAdaptee implements LogAdapteepublic void writeLog()System.out.println("轉(zhuǎn)換后的數(shù)據(jù)庫日志記錄");第二題UML類圖:Java代碼:ClientDemo.java/* * author * * */public class ClientDemo public static void main (String args) InputStream newInput = new StringBufferInputStream(); BufferInputS

7、tream newBuffer = new BufferInputStream(newInput); ToUpperCaseInputStream newToUpperCase = new ToUpperCaseInputStream(newBuffer); newToUpperCase.read();InputStream.java/* * author * * */public interface InputStream public void read();FileInputStream.java/* * author * * */public class FileInputStream

8、 implements InputStream public void read() System.out.println("FileInputStream!"); ByteArrayInputStream.java/* * author * * */public class ByteArrayInputStream implements InputStream public void read() System.out.println("ByteArrayInputStream!"); StringBufferInputStream.java/* *

9、author * * */public class StringBufferInputStream implements InputStream public void read() System.out.println("StringBufferInputStream!"); FilterInputStream.java/* * author * * */public class FilterInputStream implements InputStream public void read() System.out.println("FilterInputS

10、tream!"); DataInputStream.java/* * author * * */public class DataInputStream extends FilterInputStream public DataInputStream(InputStream inputStream) super(inputStream); public void read() super.read(); System.out.println("DataInputStream!"); BufferInputStream.java/* * author * * */p

11、ublic class BufferInputStream extends FilterInputStream public BufferInputStream (InputStream inputStream) super(inputStream); public void read() super.read(); System.out.println("BufferInputStream!"); LineNumberInputStream.java/* * author * * */public class LineNumberInputStream extends FilterInputStream public LineNumberInputStream (InputStream inputStream) super(inputStream); public void read() super.read(); System.out.println("LineNumberInputStream!"); ToUpperCaseInputStream.java/* * author * * */public class ToUpperCaseInputStream extends Fi

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論