隨筆 - 117  文章 - 72  trackbacks - 0

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

          常用鏈接

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

          訂閱

          訂閱

          留言簿(7)

          隨筆分類(130)

          隨筆檔案(123)

          搜索

          •  

          積分與排名

          • 積分 - 155795
          • 排名 - 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 天堂露珠 閱讀(1005) 評論(0)  編輯  收藏 所屬分類: Pattern
          主站蜘蛛池模板: 凤城市| 永仁县| 贵港市| 泰宁县| 连南| 德清县| 芜湖县| 伊川县| 临清市| 临颍县| 宜兴市| 呼玛县| 米林县| 内黄县| 礼泉县| 固镇县| 遵义县| 凌源市| 新泰市| 新宾| 奉贤区| 龙门县| 内江市| 琼结县| 都兰县| 靖江市| 淮南市| 嘉祥县| 古田县| 新蔡县| 余江县| 嘉善县| 乐亭县| 健康| 沁水县| 津南区| 盐池县| 郓城县| 化德县| 玛纳斯县| 保靖县|