隨筆 - 117  文章 - 72  trackbacks - 0

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

          常用鏈接

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

          訂閱

          訂閱

          留言簿(7)

          隨筆分類(130)

          隨筆檔案(123)

          搜索

          •  

          積分與排名

          • 積分 - 156400
          • 排名 - 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
          主站蜘蛛池模板: 承德市| 内乡县| 响水县| 修水县| 耿马| 阳谷县| 孟连| 浙江省| 新和县| 江阴市| 南丹县| 龙里县| 安国市| 高雄市| 锡林郭勒盟| 巴彦淖尔市| 本溪市| 清徐县| 天气| 新平| 和田县| 万盛区| 彭州市| 集安市| 吴堡县| 年辖:市辖区| 新和县| 天长市| 阜平县| 长乐市| 乡城县| 曲沃县| 株洲县| 迁西县| 鄢陵县| 瑞丽市| 墨玉县| 博湖县| 伊宁市| 察哈| 离岛区|