隨筆 - 31  文章 - 2  trackbacks - 0
          decorator的結(jié)構(gòu)如下:

          ????MyInterface
          ???????|
          _______|_______
          |?????????????|?
          Myclass?????Decorator
          ??????????____|_____
          ??????????|????????|?
          ??DecoratorA??????DecoratorB
          decorator的目的是在不改變基礎(chǔ)類的前提下,添加新的功能(在比較少的子類前提),Myclass是你的擴(kuò)展類,decoratorA,decoratorB封裝了你要擴(kuò)展的功能,并保持了
          MyInterface的引用

          考慮一下代碼
          public static void main(String[]args){
          ? MyInterface a=new Myclass();
          ??? a.print();

          }
          ?MyInterface是Myclass的接口,在 MyInterface里面就聲明了一個(gè)print方法,myclass實(shí)現(xiàn)了該方法
          public void print(){
          System.out.print("hello");
          }
          如果我們要實(shí)現(xiàn)打印 ”hello word“,”my hell“就要要寫(xiě)很多類似的類,

          decorator裝飾模式的解決方法就是,只實(shí)現(xiàn)基本功能,附加功能都抽出來(lái),
          public decoratorA
          implements?Decorator{
          ???
          MyInterface?myObject;
          ??? public decoratorA(MyInterface my){
          ??? ??
          myObject=my;
          ??? }
          ??? public void print(){
          ??? ?? System.out.print("
          world ");
          ??? }

          }

          public decoratorB implements?Decorator{
          ???
          MyInterface?myObject;
          ??? public decoratorB(MyInterface my){
          ??? ??
          myObject=my;
          ??? }
          ??? public void print(){
          ??? ?? System.out.print("MY
          ");
          ??? ?? ??
          myObject.print();
          ??? }

          }
          這時(shí)需要實(shí)現(xiàn)my hello word就比較簡(jiǎn)單了
          public void main(String[]args){
          MyInterface a=new decoratorA (new decoratorB(new Myclass()) );
          a.print();
          }

          BufferedInputStream?bis?=?new?BufferedInputStream(new?DataInpuStream(new?FileInputStream("xxx.txt")));

          InputStream.為例:
          ? ??? ??? ??? java.io.InputStream
          ????????????????????????|
          ?_______________________|________________________
          ?|?????????????????????????????????????????????|
          ByteArrayInputStream??????????????????????FilterInputStream
          StringBufferInputStream???_____________________|____________________________
          FileInputStream???????????|????????????????|????????????????????|??????????|
          PipedInputStream??DataInputStream?BufferedInputStream??LineNumInpuStream
          PushbackInputStream

          基礎(chǔ)的流只有左邊4個(gè),這些流代表了數(shù)據(jù)的來(lái)源,所有的流都必須從這四個(gè)中之一開(kāi)始(注,還有一個(gè)RandomAccessFile、File,這兩個(gè)不在本文介紹范圍)。
          當(dāng)我們需要什么新功能的時(shí)候就在右邊找個(gè)裝飾類,在用到緩存的時(shí)候我們就用bufferedInputStream

          BufferdInputStream?is?=?new?BufferedInputStream(new?FileInputStream("xxx.txt"));


          假如再要DataInputStream的功能,只要在加一層:
          DataInputStream?dis?=?new?DataInputStream(new?BufferdInputStream(new?FileInputStream));




















          posted on 2008-04-20 11:35 緣來(lái)如此 閱讀(350) 評(píng)論(0)  編輯  收藏 所屬分類: Java

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 建德市| 黑龙江省| 嫩江县| 昭通市| 贵港市| 泰州市| 渝北区| 疏附县| 永兴县| 金沙县| 巴楚县| 称多县| 讷河市| 久治县| 房产| 马尔康县| 樟树市| 清河县| 洛川县| 红安县| 博野县| 社旗县| 黎城县| 道孚县| 吐鲁番市| 沅陵县| 班戈县| 顺昌县| 新密市| 会理县| 临沧市| 环江| 青龙| 侯马市| 佳木斯市| 玉田县| 临海市| 东台市| 霍山县| 错那县| 张家界市|