隨筆 - 117  文章 - 72  trackbacks - 0

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

          常用鏈接

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

          訂閱

          訂閱

          留言簿(7)

          隨筆分類(130)

          隨筆檔案(123)

          搜索

          •  

          積分與排名

          • 積分 - 156396
          • 排名 - 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
          主站蜘蛛池模板: 宿迁市| 玛纳斯县| 惠东县| 惠安县| 海晏县| 望江县| 西城区| 芮城县| 临高县| 宁夏| 龙陵县| 铁力市| 巩义市| 南平市| 开封县| 舟山市| 上犹县| 运城市| 怀柔区| 镇康县| 东明县| 宝山区| 三穗县| 四会市| 丰城市| 冀州市| 全州县| 陈巴尔虎旗| 吉木萨尔县| 凭祥市| 图们市| 教育| 吉水县| 遵义市| 搜索| 多伦县| 铁力市| 景德镇市| 博爱县| 玉山县| 宜丰县|