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

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

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

    Chan Chen Coding...

    One: Decorator pattern

    This pattern is designed so that multiple decorators can be stacked on top of each other, each time adding a new functionality to the overridden method(s).
    Introduction

    The decorator pattern can be used to extend (decorate) the functionality of a certain object at run-time, independently of other instances of the same class, provided some groundwork is done at design time. This is achieved by designing a new decorator class that wraps the original class. This wrapping could be achieved by the following sequence of steps:

    1.    Subclass the original "Decorator" class into a "Component" class (see UML diagram);

    2.    In the Decorator class, add a Component pointer as a field;

    3.    Pass a Component to the Decorator constructor to initialize the Component pointer;

    4.    In the Decorator class, redirect all "Component" methods to the "Component" pointer; and

    5.    In the ConcreteDecorator class, override any Component method(s) whose behavior needs to be modified.

    The decorator pattern is an alternative to subclassing. Subclassing adds behavior at compile time, and the change affects all instances of the original class; decorating can 
    provide new behavior at run-time for individual objects.

    This difference becomes most important when there are several independent ways of extending functionality. In some object-oriented programming languages, classes cannot 
    be created at runtime, and it is typically not possible to predict, at design time, what combinations of extensions will be needed. This would mean that a new class would have
     
    to be made for every possible combination. By contrast, decorators are objects, created at runtime, and can be combined on a per-use basis. The I/O Streams implementations
    of bothJava and the .NET Framework incorporate the decorator pattern.

     

    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 aScrollingWindowDecorator 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 Windowclass 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 subclassesWindowWithBorder 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 theBorderedWindowDecorator 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.


    Java

    First Example (window/scrolling scenario)

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

    // the Window interface interface Window
    {
         public void draw();
     // draws the Window
         public String getDescription();
     // returns a description of the Window
    }  

    // implementation of a simple Window without any scrollbars
    class SimpleWindow implements
    Window {
         public void draw() {
             // draw window     
        
    }
          public
    String getDescription() {
             return "simple window";    
        
    }
     }

    The following classes contain the decorators for all Window classes, including the decorator classes themselves.

    // abstract decorator class - note that it implements Window
    abstract class WindowDecorator implements
    Window {
         protected Window decoratedWindow;
    // the Window being decorated      
        public WindowDecorator
    (Window decoratedWindow) {
             this.decoratedWindow = decoratedWindow;
         }
         public void draw() {
             decoratedWindow.draw();
         }
    }  
    // the first concrete decorator which adds vertical scrollbar functionality
    class VerticalScrollBarDecorator extends WindowDecorator
    {
         public VerticalScrollBarDecorator (Window decoratedWindow) {
             super(decoratedWindow);
         }      
        public
    void draw() {
             decoratedWindow.draw();
             drawVerticalScrollBar();
         }
         private void drawVerticalScrollBar() {
             // draw the vertical scrollbar    
        
    }       
        public
    String getDescription() {
             return decoratedWindow.getDescription() + ", including vertical scrollbars";
         }
     }  

    // the second concrete decorator which adds horizontal scrollbar functionality
    class HorizontalScrollBarDecorator extends WindowDecorator
    {
         public HorizontalScrollBarDecorator (Window decoratedWindow) {
             super(decoratedWindow);

         }

         public void draw() {

             decoratedWindow.draw();

             drawHorizontalScrollBar();

         }

         private void drawHorizontalScrollBar() {

             // draw the horizontal scrollbar    

    }

    public String getDescription() {

          return decoratedWindow.getDescription() + ", including horizontal scrollbars";

    }

    }

    Here's a test program that creates a Window instance which is fully decorated (i.e., with vertical and horizontal scrollbars), and prints its description:

    public class DecoratedWindowTest {
         public static void main(String[] args) {
             // create a decorated Window with horizontal and vertical scrollbars
             Window decoratedWindow = new HorizontalScrollBarDecorator (
                     new VerticalScrollBarDecorator(new SimpleWindow()));
               // print the Window's description
             System.out.println(decoratedWindow.getDescription());
         }
     }

    The output of this program is "simple window, including vertical scrollbars, including horizontal scrollbars". Notice how the getDescription method of the two decorators first retrieve the decorated Window's description and decorates it with a suffix.

    Second Example (coffee making scenario)

    The next Java example illustrates the use of decorators using coffee making scenario. In this example, the scenario only includes cost and ingredients.

    // The Coffee Interface defines the functionality of Coffee implemented by decorator

    public interface Coffee {

         public double getCost();

     // returns the cost of the coffee

     public String getIngredients();

    // returns the ingredients of the coffee }  

    // implementation of a simple coffee without any extra ingredients

    public class SimpleCoffee implements Coffee {

         public double getCost() {

             return 1;

         }

         public String getIngredients() {

             return "Coffee";

         }

     }

    The following classes contain the decorators for all Coffee classes, including the decorator classes themselves..

    // abstract decorator class - note that it implements Coffee interface
    abstract public class CoffeeDecorator implements Coffee
    {
         protected final Coffee decoratedCoffee;
         protected String ingredientSeparator = ", ";
           public CoffeeDecorator(Coffee decoratedCoffee) {
             this.decoratedCoffee = decoratedCoffee;
         }
           public double getCost() {
     // implementing methods of the interface
             return decoratedCoffee.getCost();
         }
           public String getIngredients() {
             return decoratedCoffee.getIngredients();
         }
     }
       // Decorator Milk that mixes milk with coffee
     // note it extends CoffeeDecorator
        public class Milk extends CoffeeDecorator
    {
             public Milk(Coffee decoratedCoffee)
        
    {
             super(decoratedCoffee);
         }
           public double getCost() {
     // overriding methods defined in the abstract superclass
             return super.getCost() + 0.5;
         }
           public String getIngredients() {
             return super.getIngredients() + ingredientSeparator + "Milk";
         }
     }
       // Decorator Whip that mixes whip with coffee // note it extends CoffeeDecorator
         public class Whip extends CoffeeDecorator {
         public Whip(Coffee decoratedCoffee) {
             super(decoratedCoffee);
         }
         public double getCost() {
             return super.getCost() + 0.7;
         }
           public String getIngredients() {
             return super.getIngredients() + ingredientSeparator + "Whip";
         }
     }
       // Decorator Sprinkles that mixes sprinkles with coffee
     // note it extends CoffeeDecorator
         public class Sprinkles extends CoffeeDecorator {
             public Sprinkles(Coffee decoratedCoffee) {
                 super(decoratedCoffee);
         }      

        public
    double getCost() {

             return super.getCost() + 0.2;

         }

         public String getIngredients() {

             return super.getIngredients() + ingredientSeparator + "Sprinkles";

         }

     }

    Here's a test program that creates a Coffee instance which is fully decorated (i.e., with milk, whip, sprinkles), and calculate cost of coffee and prints its ingredients:

    public class Main {
         public static void main(String[] args)
         {
             Coffee c = new SimpleCoffee();
             System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());
             c = new Milk(c);
             System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());
             c = new Sprinkles(c);
             System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());
             c = new Whip(c);
             System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());         // Note that you can also stack more than one decorator of the same type
             c = new Sprinkles(c);
             System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());
         }
     }

    The output of this program is given below:

    Cost: 1.0; Ingredients: Coffee
    Cost: 1.5; Ingredients: Coffee, Milk
    Cost: 1.7; Ingredients: Coffee, Milk, Sprinkles
    Cost: 2.4; Ingredients: Coffee, Milk, Sprinkles, Whip
    Cost: 2.6; Ingredients: Coffee, Milk, Sprinkles, Whip, Sprinkles

    Dynamic languages

    The decorator pattern can also be implemented in dynamic languages with neither interfaces nor traditional OOP inheritance.




    -----------------------------------------------------
    Silence, the way to avoid many problems;
    Smile, the way to solve many problems;

    posted on 2012-06-26 05:30 Chan Chen 閱讀(334) 評論(0)  編輯  收藏 所屬分類: Design Pattern

    主站蜘蛛池模板: 亚洲AV永久无码精品成人| 免费一级肉体全黄毛片| 亚洲AV无码精品色午夜果冻不卡| 国产AV无码专区亚洲AV蜜芽| 天天操夜夜操免费视频| 亚洲国产精品综合久久20| 99久久免费国产精品特黄 | 亚洲综合无码AV一区二区| 特级一级毛片免费看| 亚洲精品成a人在线观看| ssswww日本免费网站片| 亚洲中文字幕无码爆乳AV| 国产精品网站在线观看免费传媒| 久久久久亚洲精品中文字幕| 国产免费高清69式视频在线观看| 亚洲精品少妇30p| 91制片厂制作传媒免费版樱花| 亚洲欧洲日本精品| 免费看国产成年无码AV片| 亚洲av无码专区亚洲av不卡| 可以免费观看一级毛片黄a| 中文字幕在线免费视频| 亚洲成a人片在线观看中文动漫| 2021精品国产品免费观看| 在线a亚洲老鸭窝天堂av高清| 国产免费怕怕免费视频观看| 一进一出60分钟免费视频| 久久国产亚洲观看| 无码人妻久久一区二区三区免费丨| 亚洲另类无码专区首页| 亚洲视频在线精品| 久久综合国产乱子伦精品免费 | 黑人大战亚洲人精品一区| 色欲国产麻豆一精品一AV一免费| 亚洲中文无码av永久| mm1313亚洲精品无码又大又粗| 两个人的视频www免费| 亚洲另类小说图片| 国产成人精品久久亚洲高清不卡 | 久久这里只精品99re免费| 中文字幕亚洲精品无码|