<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    人在江湖

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      82 Posts :: 10 Stories :: 169 Comments :: 0 Trackbacks

    參考: http://en.wikipedia.org/wiki/Decorator_pattern

    class=thumbimage

    Motivation

    class=thumbimage

    UML Diagram for the Window Example

    As an example, consider a window in a windowing system. To allow scrolling of the window's contents, we may wish to add horizontal or vertical scrollbars to it, as appropriate. Assume windows are represented by instances of the Window class, and assume this class has no functionality for adding scrollbars. We could create a subclass ScrollingWindow that provides them, or we could create a ScrollingWindowDecorator that adds this functionality to existing Window objects. At this point, either solution would be fine.

    Now let's assume we also desire the ability to add borders to our windows. Again, our original Window class has no support. The ScrollingWindow subclass now poses a problem, because it has effectively created a new kind of window. If we wish to add border support to all windows, we must create subclasses WindowWithBorder and ScrollingWindowWithBorder. Obviously, this problem gets worse with every new feature to be added. For the decorator solution, we simply create a new BorderedWindowDecorator—at runtime, we can decorate existing windows with the ScrollingWindowDecorator or the BorderedWindowDecorator or both, as we see fit.

    Another good example of where a decorator can be desired is when there is a need to restrict access to an object's properties or methods according to some set of rules or perhaps several parallel sets of rules (different user credentials, etc.) In this case instead of implementing the access control in the original object it is left unchanged and unaware of any restrictions on its use, and it is wrapped in an access control decorator object, which can then serve only the permitted subset of the original object's interface.

    Example

    The following Java example illustrates the use of decorators using the window/scrolling scenario.

       1: // the Window interface
       2:  
       3: interface Window {
       4:  
       5:     public void draw(); // draws the Window
       6:  
       7:     public String getDescription(); // returns a description of the Window
       8:  
       9: }
      10:  
      11: // implementation of a simple Window without any scrollbars
      12:  
      13: class SimpleWindow implements Window {
      14:  
      15:     public void draw() {
      16:  
      17:         // draw window
      18:  
      19:     }
      20:  
      21:     public String getDescription() {
      22:  
      23:         return "simple window";
      24:  
      25:     }
      26:  
      27: }
      28:  
      29:  
      30:  
      31: // abstract decorator class - note that it implements Window
      32:  
      33: abstract class WindowDecorator implements Window {
      34:  
      35:     protected Window decoratedWindow; // the Window being decorated
      36:  
      37:     public WindowDecorator (Window decoratedWindow) {
      38:  
      39:         this.decoratedWindow = decoratedWindow;
      40:  
      41:     }
      42:  
      43: }
      44:  
      45:  
      46:  
      47: // the first concrete decorator which adds vertical scrollbar functionality
      48:  
      49: class VerticalScrollBarDecorator extends WindowDecorator {
      50:  
      51:     public VerticalScrollBarDecorator (Window decoratedWindow) {
      52:  
      53:         super(decoratedWindow);
      54:  
      55:     }
      56:  
      57:     public void draw() {
      58:  
      59:         drawVerticalScrollBar();
      60:  
      61:         decoratedWindow.draw();
      62:  
      63:     }
      64:  
      65:     private void drawVerticalScrollBar() {
      66:  
      67:         // draw the vertical scrollbar
      68:  
      69:     }
      70:  
      71:     public String getDescription() {
      72:  
      73:         return decoratedWindow.getDescription() + ", including vertical scrollbars";
      74:  
      75:     }
      76:  
      77: }
      78:  
      79:  
      80:  
      81: // the second concrete decorator which adds horizontal scrollbar functionality
      82:  
      83: class HorizontalScrollBarDecorator extends WindowDecorator {
      84:  
      85:     public HorizontalScrollBarDecorator (Window decoratedWindow) {
      86:  
      87:         super(decoratedWindow);
      88:  
      89:     }
      90:  
      91:     public void draw() {
      92:  
      93:         drawHorizontalScrollBar();
      94:  
      95:         decoratedWindow.draw();
      96:  
      97:     }
      98:  
      99:     private void drawHorizontalScrollBar() {
     100:  
     101:         // draw the horizontal scrollbar
     102:  
     103:     }
     104:  
     105:     public String getDescription() {
     106:  
     107:         return decoratedWindow.getDescription() + ", including horizontal scrollbars";
     108:  
     109:     }
     110:  
     111: }
     112:  
     113:  
     114:  
     115: public class DecoratedWindowTest {
     116:  
     117:     public static void main(String[] args) {
     118:  
     119:         // create a decorated Window with horizontal and vertical scrollbars
     120:  
     121:         Window decoratedWindow = new HorizontalScrollBarDecorator (
     122:  
     123:                 new VerticalScrollBarDecorator(new SimpleWindow()));
     124:  
     125:         // print the Window's description
     126:  
     127:         System.out.println(decoratedWindow.getDescription());
     128:  
     129:     }
     130:  
     131: }
    posted on 2011-02-09 22:58 人在江湖 閱讀(985) 評論(0)  編輯  收藏 所屬分類: design pattern
    主站蜘蛛池模板: 好男人视频社区精品免费| 免费又黄又爽又猛的毛片 | 亚洲一区AV无码少妇电影☆| 色www永久免费网站| 亚洲欧洲精品久久| 一二三四免费观看在线电影| 亚洲欧美日韩自偷自拍| 亚洲五月午夜免费在线视频| 96免费精品视频在线观看| 国产成人亚洲精品电影| 亚洲欧洲日产国码无码久久99 | 毛片大全免费观看| fc2成年免费共享视频网站| 亚洲国产高清美女在线观看| 亚洲第一网站男人都懂| 精品免费人成视频app| 无码的免费不卡毛片视频 | 国产一区二区免费视频| 亚洲中文字幕一区精品自拍| 亚洲精品成人片在线播放| 成人免费毛片内射美女APP| 中国内地毛片免费高清| 亚洲中文字幕久久精品蜜桃| 亚洲乱码精品久久久久..| 浮力影院第一页小视频国产在线观看免费 | 亚洲国产成人九九综合| 亚洲中文字幕久久精品无码APP | 久久成人永久免费播放| 亚洲а∨天堂久久精品9966 | 亚洲av永久无码嘿嘿嘿| 亚洲va国产va天堂va久久| 国产男女猛烈无遮档免费视频网站| 青青草无码免费一二三区| 国产无限免费观看黄网站| 亚洲乱亚洲乱妇24p| 亚洲第一成年人网站| 亚洲产国偷V产偷V自拍色戒| 亚洲第一区精品观看| 日韩成全视频观看免费观看高清 | 国产精品亚洲不卡一区二区三区 | 亚洲AV无码久久精品色欲|