Design Mode (III)-Structure Mode

Source: Internet
Author: User

Proxy mode:

If the business class does not have a security mechanism, all operations can be done, and any danger can occur. The proxy mode provides a solution to the crisis. Some business brothers are derived as agents of the business class. In this way, the boss has the proxy class of the boss, and the marketing has the agent class of the marketing class. Never directly use the hidden anti-bot service class.

Solve the problem. The figure is as follows:

In the figure, the proxy class invites the business class to perform specific operations.

Mask (adapter) mode:

Literal translation is an adapter. I think it is better to translate it into a mask.

If you have something to do: football, there is no source code, but it can be referenced. Now you find that this football matches youProgramThe ball games in the program are inherited, and you are a brother of basketball in your program. Because football is not written by you, it cannot be required to re-derive from your ball. After a while, you are wise enough to find inspiration from the mask.

For example, adapter is called a football mask, and adaptee is a football player suffering you. There is no way for the football mask itself. Its methods all come from the football class methods. Its strength is that it is born well. This mask class derives from your ball class and ensures that polymorphism can be used in the future.

Bridge Mode:

As long as you use interfaces, the bridge mode is used. This model may have no language implementation at the time of writing. Now the times are different. Any ixxxx is a bridge. Interfaces in. NET are very common and there is no need to prove them.

Composite mode:

Consider folders and files. When abstracting them, we put them together as brothers and construct a parent class for them, called a file project. Folder derived from the file project class can also be placed in folders and files, that is, folders are the containers of the file project.

This mode is often used to solve tree-like organization problems.

Note that the composite class can accommodate its base class component.

Decorator mode:

This problem may have been found when using the stream. Basic streams include text streams and binary streams. Now, a buffer mechanism is required, so:

X) The invention can buffer text streams and buffer binary streams.

This is dangerous. For example, if encryption is further required, you need to invent: Encryption can buffer text streams, encryption can not buffer text streams, encryption can not buffer text streams ...... (200 words are omitted below ).

In. net, both the buffer stream and the encrypted stream are generated from the school and can be regarded as the stream. All their constructors require a stream as their accessory. When an accessory is invited to perform an action, the buffer stream and encrypted stream are adjusted accordingly to supplement the buffering and encryption functions.

Similar in Java,. Net has copied a lot of Java stuff.

This method is cool. People who have used it are impressed and will consciously imitate it. No wonder how java. Net Works.

A base class is required as an accessory.

Transaction (facade) mode:

According to the current application, this mode is the most used in transactions-it may be called a transaction in a partial sense. The concept of a transaction is to combine some very small-granularity actions into a transaction for one processing. The introduction of transactions also involves atomic considerations. You can roll back and lock the transactions.

The transaction mode does not have much to do with rollback. It only combines the methods scattered in each object to complete a function. In this way, the small object can only publish this transaction class without being published to the outside world. Therefore, this transaction class is the facade of small objects, and facade translation is the meaning of the facade.

Color palette (flyweight) mode:

Translated into yuan. Objection.

People who have done bitmap know that there is a 256-color image. A maximum of 256 colors are supported. After the color is selected, for example, if Red is the first digit, to indicate Red in the image, do not write 0xff0000, just write 1. This is the meaning of the color palette.

In word, althoughFlowersSample Multiple , Can still be summarized into several types, put them in a set. Assume that the flower is in bold italic and numbered 1. In the future, all coarse oblique styles can be recorded as <1> flowers </1> instead of <bold> <italic> flowers </italic> </bold>.

Anyone who has learned CSS knows the benefits of doing so.

Note the flyweight factory action: if the requested key does not exist, create one. If yes, use the previous one. Therefore, the flyweight factory is actually an object pool with the features of the Manager implicitly.

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.