Vernacular design pattern-structural pattern-decorator pattern, adapter pattern and appearance Pattern

Source: Internet
Author: User

What is a modifier? It inherits the abstract object and then combines the abstract object (both ISA and hasa relationships). At this time, add the behavior to be added in the decoration, at the same time, it is necessary to implement specific a behavior, thus adding new functions.
Initialize the abstract object when constructing the modifier.

The decoration mode allows you to dynamically expand object functions at runtime. You do not need to use inheritance to expand the functions separately, so you do not need to create subclass expansion problems. The decorator needs to know the functions of objects to be extended while the decorator does not know the objects to be extended, these functions can be used at runtime. After the object is decorated (extended), it is still the original object (with the ISA relationship ).

This is like a wall that does not need to paint it, but the paint must know which (surface) walls to paint. After the wall paint is finished, it is still the relationship between the walls (ISA.

Decoration is one of the easy-to-understand design patterns. I think the focus is on the relationship between the decorator and the object to be extended, both ISA and hasa, the decorated object is still the original object and still has the characteristics of the original object, because the modifier only adds functions.

Teacher Li Jianzhong said that the main point of the decorator model is to solve the problem of "extension of the Subject Class in multiple directions". I think it is very appropriate.

As in the past, I also want to talk about the differences and connections between the modifier mode and the adapter mode. When I first started learning the adapter, I always thought that the adapter could be applied to add new features. It should have a hasa relationship in the adapter. But I don't know the adapter. It can be seen from its name to adapt to certain behaviors. That is to say, let something that cannot be used by some type of conversion be usable and adapted. The key lies in a kind of transformation, and the modifier, from the perspective of the name, adds decoration. Adaptation is to reuse existing classes so that their interfaces are consistent with new environment requirements. The annotator dynamically adds new functions for existing objects and focuses on solving the dynamic problem (subclass expansion problem ).

The appearance mode focuses on simplified interfaces, so you don't have to worry about internal implementations When referencing them. You only need to care about external interfaces. In fact, it is to put some related classes (Note: they are related classes) together to facilitate access.

By comparing the appearance mode, adapter mode, bridge mode, and modifier mode, we can see that the appearance mode focuses on interface simplification and makes access simple; the adapter mode makes it appropriate for an originally inappropriate interface, which is an interface conversion. The Bridge Mode combines two changed dimensions into one. The auxiliary logic is rubbed into the main logic and abstracted at the same time, let changes be separated so that changes do not affect stability. Focus is on the separation of abstraction and implementation. The modifier mode focuses on adding new functions to objects.

The design pattern is great. We should learn it. This chapter is over. Thank you. Haha :)

In addition to the question. In my opinion, the focus of understanding the design pattern is not on how to draw class diagrams,CodeHow to write. It is true that the code can intuitively express the intent and usage of a design pattern, and at the same time it limits our understanding of ideas. The most important thing about a design pattern lies in our thinking. When we understand what the design pattern can do, what problems it can solve, and what scenarios it can use, I think, it should not be difficult to understand that image. A very old, very old, very earthy word, the design model is studious, but not easy to use. Therefore, I think everyone should pay more attention to the idea of a design model, more attention to the problems it can solve, and more attention to the scenarios in which it can be used.

In addition, it is my habit to compare various design patterns. In this way, I think we can learn new things with a warm understanding, so that we can combine new knowledge and old knowledge to compare differences and connections for a deeper understanding. We often repeat the comparison and are more impressed.

 

 

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.