Decorator mode for design mode (JAVA)

Source: Internet
Author: User
Tags modifiers

Previously did not contact the decorator mode, yesterday, a brief study, record, for further study bedding, just contact, there is no place to welcome you correct.

First, what is decorator mode?

For example, it is like a production line of cake, you can not use this assembly line to produce cars, not to produce tanks, of course, you can not produce. Your cake is produced, to save it, to sell it, you can not put what people want to eat directly on the ground directly on the car, so you need the box, you need the base of the cake, then the box and the base is your cake packaging, that is, decoration. As for the color of the box and the base, Ah, shape ah, you can completely various, how to combine the combination.

The example may not be appropriate, but the truth is this. Decorator mode is the ability to dynamically extend an object without having to change the original class file and use inheritance. It is by creating a wrapper object, that is, decorating to wrap the real object. This is an official explanation, what does it mean? That is, now another class, you can not change the class, you can not change the original inheritance of this class, but also to dynamically extend the instantiation of the class of the function of an object, how to extend the function of the object? To create a new wrapper object, use the wrapper object you created to wrap the original object in order to extend the original object.

Secondly, what are the characteristics of decorator mode?

1. Decorators and decorators have inherited from the same superclass.

2. The decorator can be decorated by multiple decorators at the same time.

Or the top of the production of cakes, you can add cream on the cake, then become a cream cake, add cream and then you want to eat fruit, you can also add the cream of the cake with fruit, add fruit you still feel uncomfortable, also want to add chocolate, this is completely possible. No matter what you want to eat can be added (add more the last can not eat I do not know, you can try), cake things add more may not eat, but the modifier can be modified by more than one modifier at the same time, but the number of modifiers and modifiers to add the order according to the specific needs to add, It is not said that can be modified by a number of modifiers then you can add casually, you give the cake four car wheels to try to see if someone will not kill you?

3. The key to using inheritance in decorating mode is to achieve the type matching of the decorator and the object being decorated, rather than obtaining its behavior.

      The decorator and the decorator must be the same type, that is, to have a common superclass. The application of inheritance here is not a copy of the implementation method, but rather a match of the type. Because the decorator and the decorator are of the same type, the decorator can replace the decorator so that the decorator has the unique behavior of the decorator. Based on the concept of decorator mode, we can add new behaviors to new decorators at any time. If you are using inheritance, you should modify the original program whenever you need to add new behavior.

4. Differences from inheritance

Learn Java, you must also know what is inheritance, I will not nonsense. The first look at the decorator pattern is really like inheritance, but there is a big difference between looking at it carefully. With inheritance for feature extensions, the features you want to extend must be predictable, determined at compile time, immutable at runtime, static, and all subclasses inherit the same behavior. However, the adorner pattern is variable for the extension of the function, and the adorner pattern expands the object's behavior by using the combined method, which can then be dynamically extended at run time.

  

Decorator mode for design mode (JAVA)

Related Article

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.