Design Mode: structural mode of Reading Notes

Source: Internet
Author: User

1. Adapter Mode)

Basic tutorial

Read <design mode> and this articleArticleI think the focus of the adapter isThe interface provided by the object does not necessarily adapt to our new environment. We need to convert it into the interface we need.(In fact, it is not suitable for the new environment that the name of the class member function is different and the subclass cannot be directly operated through the parent class pointer)

 

An example of the design pattern is as follows:ProgramWe have lineshape and polygnshape which all inherit from shape and have a unified interface. For example, we have an interface boundingbox. Now we want to add a textshape, generally, you need to write a new one by yourself, but some libraries provide textview which can be slightly changed to meet our needs, but the member function name of the class is different from ours, for example, textview has a member function, boundingfun, which is the same as boundingbox.

 

In this case, I can directly use textview or create a class textview2 that inherits from textview. This is also a good method I think, saving time and reusing.Code

 

However, if we want to operate the textshape directly through the shape pointer, that is to say, our customers only need to create a new geometric image, whether it is lineshape or textshape, you can use the same interface to perform operations. In this case, let textshape inherit the shape in the public way, inherit the textview in the private way, or have a private textview member variable, in this way, we can achieve the above purpose

 

The adapter mode is divided into object adapters and class adapters. In fact, an adapter is implemented through combination and inheritance. Of course, I prefer combination because if textview has child classes, for example, textview2, when we want to adapt to these classes, if we use inheritance, we need to create another class, such as textshape2, but if we use a combination, we can add a settextview interface to textshape, and operate textview2 directly through the same interface.

 

Decorator ):

Basic tutorial

As the name suggests, the modifier mode adds a decoration to the class. We have a decorator and component. If we want to attach the component or its base class, then we can use decorator to decorate component, and then decorator to perform additional operations on component. decortor is more inclined to change the shell of the object than strategy tends to change the kernel of the object.

 

<Design mode> in this example, the textview control is faulty. The textview control has a parent class visualcomponent, which is drawn by draw but does not support scroll bars, now we need textview to have the scroll bar function. What should we do ??

 

Generally, I create a new class named textviewscroll. If I need a textview control with a scroll bar, I just need to create this class, however, if we have another control, such as list, which is inherited from visualcomponent, and this control also requires a scroll bar, do we need to create another textviewscroll ??

 

At this time, we need a decorator, that is, to decorate textview and list with a scroll bar. At this time, we also have a decorator, which inherits from visualcomponent, in addition, this class can accept a class or subclass of visualcomponent. When we need a scroll bar, we only need the decorator to decorate the passed-In visualcomponent class. In the draw control, the draw scroll bar is used by the way.

 

The disadvantage of this mode is that if the decorated object does not have a public parent class and there is no unified interface, then there is no way to use the modifier, I can only use the method I mentioned (there may be other methods later, and continue to look at it)

 

Flyweight)

Basic tutorial

The metadata mode makes me feel more like copying another version when writing. The difference is that when I change it, for example, there are three strings a, B, c, string a points to "ABC", string B points to "BCD", if C = A, then C also points to "ABC ", there are actually only two strings in the memory: "ABC" and "BCD". If C = "BCD" is used at this time, if it is a copy at the time of writing, C will point to "BCD ", but it is not the "BCD" that B points to, it is a new memory. In this way, the memory has three memories: ABC, BCD, and BCD. If it is in the meta mode, then C will first find out whether "BCD" exists in all the strings that can be shared. If yes, direct C to "BCD" so that there are only two in the memory, "ABC" and "BCD"

 

In my understanding, the metadata will create a group of memory that can be shared. If we need to change the data of some objects, we will first find them in the memory, then direct the object to the shared memory, and no further creation is performed.

 

Enjoy yuan is suitable for some data with a large amount of repeated content, such as a document editor mentioned in <design mode>. If each character in the document editor uses an object storage, the memory is very low, but there are actually more than 20 letters. These letters are the same. Only when the character positions and colors are different can the characters be shared at this time, only positions and colors cannot be shared, and shared items can be extracted. (Of course, a character pointer actually consumes more memory than a letter. Here is just an example)

 

Appearance and proxy)

To be honest, I think the two models are the same, that is, adding a layer to cover up the internal situation, but I did not expect the cmd_ptr is the proxy mode.

 

Bridge ):

The Bridge Mode also gives me the feeling of a proxy mode or a variant of the appearance mode. All they provide to users is interfaces, which masks internal implementations.

<Design Pattern> for example, a window is a cross-platform window. The bridge pattern separates abstraction and implementation. The implementation is windowimp. Using the windowsimp pointer in the Windows class, create different classes based on different platforms, and then we can mask whether window calls the implementation of XWindow or pmwindow,

 

Composite ):

For example, the combination mode is consistent between the user's use of a single object or a combination object. My understanding is whether it is a node of 0403 or 04-0401-0402-0403, the entire tree is the same for the interfaces provided by users. users do not have to worry about whether it is a leaf node or a subtree when adding or removing the tree.

 

Looking at the design pattern, I still feel confused. I can only say that I can understand it slowly. However, the design pattern still relies on practice. I can only say that I will try to see if I can use the design pattern in the future, of course, it's just to increase experience. Never change the design pattern.

 

After reading this, I feel that the structural model still needs to be used on the basis of knowing what to do. Otherwise, the entire architecture cannot be designed, but I have not reached that level yet, it can only be said that the Code should be written with low coupling and good readability, so that later changes will be very easy.

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.