Decorator mode Summary

Source: Internet
Author: User

The decorator mode contains two objects: the decorator and the component [decorator]

The core idea of the decorator model:

1. Replace inheritance, dynamically attach the responsibility to the object

2. Effective scalability does not violate the principle of opening or closing

3. Effective modifier Reuse

The design principle of the decorator mode: the open and closed principle, less inheritance and low coupling.

The purpose of the modifier mode:

1. A large number of classes need to be produced

2. These classes can be atomic into a small number of classes.

3. The combination of these small classes can constitute (1) Classes

Disadvantages of the modifier mode:

1. components should not be decorated with too many classes, and the code will be lengthy (the factory mode can be effectively used)

2. If a decoration class is decorated with multiple components at the same time, maintenance is still troublesome (for example, you need to pay for sugar in coffee, but you do not need to pay for sugar in dessert)

3. the same component is decorated by the decorator for multiple times (add 10 eggs), and you can only call the decoration method repeatedly. In particular, when the decoration method is still a constructor, you still need to keep New

4. You cannot use the modifier of the same category [not inherited from the same parent class/interface] to modify the same component. (Such as water, condiments are all food, and the blue root is a drug. It is difficult to make a copy of the blue root with a taste)

5. The component can be modified, but cancellation is very troublesome. After all, the objects passed in are processed and not saved.


Simple Example

In text editing, the modifier relationship between sentences and words [seems a little far-fetched, And the modifier mode is a combination]

Package decratorpattern; public interface decrator {/*** display information * @ return */string display ();}

Package decratorpattern; public class sentence implements decrator {word words = New Word (""); @ overridepublic string display () {return words. display ();}/*** decoration ** @ Param D */Public void decrate (word d) {If (D! = NULL) {words. Connect (d );}}}

Package decratorpattern; public class word implements decrator {private string word = NULL; public word (string word) {This. WORD = word;}/*** display content */@ overridepublic string display () {string displaystring = ""; if (word! = NULL) {displaystring + = word;} return displaystring;}/*** decoration */Public void connect (decrator d) {If (D! = NULL) {word + = D. Display ();}}}

Package decratorpattern; public class test {public static void main (string [] ARGs) {word W1 = New Word ("today"); Word W2 = New Word ("Weather "); word W3 = New Word ("good"); Word W4 = New Word ("! "); Sentence S = new sentence (); S. decrate (W1); S. decrate (W2); S. decrate (W3); S. decrate (W4); system. out. println (S. display ());}}

Test Results

The weather is good today!


Decorator mode Summary

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.