隨筆 - 31  文章 - 2  trackbacks - 0
          decorator的結構如下:

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

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

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

          decorator裝飾模式的解決方法就是,只實現基本功能,附加功能都抽出來,
          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();
          ??? }

          }
          這時需要實現my hello word就比較簡單了
          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

          基礎的流只有左邊4個,這些流代表了數據的來源,所有的流都必須從這四個中之一開始(注,還有一個RandomAccessFile、File,這兩個不在本文介紹范圍)。
          當我們需要什么新功能的時候就在右邊找個裝飾類,在用到緩存的時候我們就用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 緣來如此 閱讀(349) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 图片| 土默特右旗| 金平| 贵阳市| 枞阳县| 格尔木市| 鸡东县| 阿巴嘎旗| 嘉荫县| 竹北市| 古蔺县| 北碚区| 东山县| 满城县| 乐山市| 乌兰浩特市| 柯坪县| 安福县| 乃东县| 马公市| 兴国县| 双流县| 扶风县| 河东区| 固始县| 彭山县| 清水县| 聂拉木县| 井冈山市| 沙洋县| 江北区| 荥阳市| 鹿邑县| 天台县| 商水县| 北票市| 内丘县| 桂林市| 灵宝市| 克拉玛依市| 阳高县|