隨筆 - 117  文章 - 72  trackbacks - 0

          聲明:原創作品(標有[原]字樣)轉載時請注明出處,謝謝。

          常用鏈接

          常用設置
          常用軟件
          常用命令
           

          訂閱

          訂閱

          留言簿(7)

          隨筆分類(130)

          隨筆檔案(123)

          搜索

          •  

          積分與排名

          • 積分 - 156419
          • 排名 - 390

          最新評論

          [關鍵字]:java,design pattern,設計模式,《Java與模式》學習,decorator,裝飾模式
          [環境]:StarUML5.0 + JDK6
          [作者]:Winty (wintys@gmail.com)
          [正文]:

          package pattern.decorator;

          /**
           * 裝飾模式:Decorator Pattern
           * @version 2009-6-5
           * @author Winty(wintys@gmail.com)
           */
          public class DecoratorTest{
              public static void main(String[] args){
                  Component component;
                  component = new ConcreteComponent();
                  component.doSomething();
                  System.out.println("");
                  
                  //透明的裝飾模式
                  Component decorator;
                  decorator = new ConcreteDecorator(component);
                  decorator.doSomething();
                  System.out.println("");

                  //半透明的裝飾模式
                  ConcreteDecorator decorator2;
                  decorator2 = new ConcreteDecorator(component);
                  decorator2.doSomething();
                  decorator2.doOtherThings();
              }
          }


          /**
           * 抽象構件:Component
           */
          interface Component{
              public abstract void doSomething();
          }

          /**
           * 具體構件:ConcreteComponent
           */
          class ConcreteComponent implements Component{
              @Override
              public void doSomething(){
                  System.out.println("do something");
              }
          }

          /**
           * 裝飾:Decorator
           */
          abstract class Decorator implements Component{
              private Component comp;

              public Decorator(Component comp){
                  this.comp = comp;
              }

              @Override
              public void doSomething(){
                  comp.doSomething();
              }
          }

          /**
           * 具體裝飾:ConcreteDecorator
           */
          class ConcreteDecorator extends Decorator{
              public ConcreteDecorator(Component comp){
                  super(comp);
              }

              @Override
              public void doSomething(){
                  super.doSomething();
                  doMoreThings();
              }

              private void doMoreThings(){
                  System.out.println("do more things.");
              }

              /**
               * 新增的方法不是Component接口的一部分,
               * 所以不能通過Component接口透明地訪問。
               */
              public void doOtherThings(){
                  System.out.println("do other things.");
              }
          }
          posted on 2009-06-07 22:33 天堂露珠 閱讀(1008) 評論(0)  編輯  收藏 所屬分類: Pattern
          主站蜘蛛池模板: 田东县| 卢龙县| 佳木斯市| 龙口市| 德化县| 郯城县| 永昌县| 晋中市| 普陀区| 上饶县| 吐鲁番市| 宁城县| 呈贡县| 鲁甸县| 象山县| 灵璧县| 宿松县| 栾川县| 霍山县| 正阳县| 日照市| 阜宁县| 连云港市| 津南区| 富宁县| 岳阳县| 阿拉善右旗| 洱源县| 淅川县| 浮山县| 会同县| 碌曲县| 登封市| 榆社县| 景德镇市| 剑川县| 同仁县| 晋城| 通化市| 吉安县| 花垣县|