Design Pattern: modifier pattern (JAVA); design pattern: java

Source: Internet
Author: User
Tags what inheritance

Design Pattern: modifier pattern (JAVA); design pattern: java

I have never touched on the decorator mode before. I did a brief research yesterday and recorded it to pave the way for further study. I just got in touch with it. If there is anything wrong, please let us know.

First, what is the decorator model?

For example, it is like an assembly line for producing cakes. You cannot use this assembly line to produce cars, or even tanks. Of course, you cannot. Your cake has been produced. Save it. Sell it. You cannot put the food that people want directly on the ground in the car. Therefore, you need a box, if you need the base of the cake, the box and base are your packaging and decoration of the cake. As for the color and shape of the box and base, you can combine them in a variety of ways.

The example may be inappropriate, but the truth is that. The modifier mode dynamically extends the functions of an object without changing the original class file and using inheritance. It creates a packaging object, that is, decoration, to package a real object. What does this mean? That is to say, now there is another class. You can neither change this class nor change the original inheritance relationship of this class. However, you must dynamically extend the function of an object instantiated by this class, how can we expand the functions of objects? Create a new packaging object and wrap the original object with the created packaging object to expand the original object.

What are the characteristics of the decorator model?

1. the decorator and the decorator inherit from the same superclass.

2. the decorator Can be decorated by multiple decorator at the same time.

Or the above cake, you can add cream on the cake, it becomes a cream cake, after adding cream you want to eat fruit, then you can add fruit on the cake with cream, and you still feel uncomfortable when adding fruit. You also want to add some chocolate, which is completely OK. No matter what you want to eat, you can add it in the past (I don't know if you can eat it after adding more). If you want to eat more cake, you may not be able to eat it, however, the modifier can be simultaneously modified by multiple modifiers, but the number of modifier and the order in which the modifier is added must be based on specific requirements, it doesn't mean that you can add them as you like if they can be modified by multiple modifiers. You can add four cars to the cake and try to see if someone will not kill you?

3. In the decoration mode, the key to inheritance is to match the type of the modifier and the object to be decorated, rather than obtaining the behavior.

The decorator and the decorator must be of the same type, that is, they must have a common superclass. Here, application inheritance is not a replication of implementation methods, but a matching of the actual type. Because the decorator and the decorator are of the same type, the decorator can replace the decorator, so that the decorator has the behavior unique to the decorator. According to the decorator model, we can add new behaviors to new decorator at any time. If inheritance is used, the original program will be modified whenever new behaviors need to be added.

4. Differences from inheritance

If you want to learn JAVA, you must also know what inheritance is. At first glance, the modifier pattern is really like inheritance, but if you look at it carefully, there is still a lot of difference. If you use inheritance for function extension, the functions you want to extend must be foreseeable. It has been confirmed at the compilation stage and cannot be changed at runtime. It is static, all child classes inherit the same behavior. However, the modifier mode can be used to expand functions. The modifier mode can use a combination to expand the behavior of objects, and then dynamically expand objects during runtime.


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: 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.