隨筆 - 117  文章 - 72  trackbacks - 0

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

          常用鏈接

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

          訂閱

          訂閱

          留言簿(7)

          隨筆分類(130)

          隨筆檔案(123)

          搜索

          •  

          積分與排名

          • 積分 - 156395
          • 排名 - 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
          主站蜘蛛池模板: 兖州市| 无为县| 牟定县| 阿拉善盟| 南康市| 万全县| 富阳市| 云林县| 电白县| 共和县| 沂水县| 临桂县| 丁青县| 清水县| 伊金霍洛旗| 宜兰市| 梁河县| 西畴县| 丰顺县| 江永县| 海伦市| 柯坪县| 措美县| 开封市| 沛县| 峨山| 南召县| 张家港市| 和田市| 孟州市| 吉木萨尔县| 荔波县| 凉城县| 新津县| 乌恰县| 北票市| 穆棱市| 济源市| 大悟县| 大冶市| 万山特区|