Design mode (10): Decorator mode

Source: Internet
Author: User

In the introduction of JavaScript design mode, decorator mode is an alternative to the mixin (mixed) pattern, which is another feasible object subclass (Mixin mode). Decorator (Decorator) mode Definition:Add additional functionality to the object dynamically. It is more streamlined to add (decorate) properties or methods to the base object, rather than subclass it. Usage Scenarios:The Java IO stream is a typical decorative pattern. PS: Applications contain features that require a large number of different types of objects. Example:
//The constructor to decoratefunctionMacBook () { This. Cost =function() {return997; };  This. screensize =function() {return11.6; };}//Decorator 1functionmemory (MacBook) {varv =Macbook.cost (); Macbook.cost=function() {    returnV + 75; };}//Decorator 2functionengraving (MacBook) {varv =Macbook.cost (); Macbook.cost=function(){    returnV + 200; };}//Decorator 3functionInsurance (MacBook) {varv =Macbook.cost (); Macbook.cost=function(){    returnV + 250; };}varMB =NewMacBook (); Memory (MB); engraving (MB); Insurance (MB) ;//outputs:1522Console.log (Mb.cost ());//outputs:11.6Console.log (Mb.screensize ());
The adornment does not rewrite the original MacBook object's constructor method (such as screensize ()), and the other properties are the same, and remain intact. PS: The key point in this is that the decoration is an instance object, not a class. Advantages:1. The purpose of the decorator pattern and the inheritance relationship is to extend the functionality of the object, but decorator can provide more flexibility than inheritance. 2. By using different decorative classes and arranging combinations of these decorations, you can create a combination of many different behaviors. Disadvantages:1. This is more flexible than inheritance, but it also means more complexity. 2. Decorating mode causes many small classes to appear in the design, which can complicate the program if overused. (Here the class is to decorate the object) decorator mode differs from mixed mode1. Decorator mode adds function to object dynamically and can be decorated several times. (Decorator mode modifies the instance object) PS:decorations can be used for classes or instance objects, so don't get me wrong here. 2. Mixed mode combines the functionality of multiple classes to produce a class. (class is modified in mixed mode) SummaryDecorator mode and mixed mode, the ultimate goal is to enhance the function of objects, but the means of enhancement are different. In addition to using the design pattern on JavaScript, and not having to imitate the object-oriented language to write neatly (like the top illustration), it's a bit stiff. Reference Documents1. Decoration mode (Baidu Encyclopedia) 2. "JavaScript design mode" by Xutao "translation" This article for the original article, reproduced please retain the original source, convenient traceability, if there is a wrong place, thank you correct. This address: http://www.cnblogs.com/lovesong/p/5625245.html

Design mode (10): Decorator mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.