I also designed the mode-25. Visitor

Source: Internet
Author: User

1) In reality, if my computer breaks down, I will ask a computer repair engineer to come to your house for repairs. This person will know the object of my house and then come to my house to re-install the computer system; of course, if your computer is broken, you will also find the Repair Engineer to repair it, but it may be a hard disk change. This is the visitor mode. The computer maintainer is concretevisitora and your home is concreteelementa. The UML diagram is as follows:

How can I use it on the client?

Computer Repair Engineer V1= NewComputer Repair Engineer ();
Family E= NewMy home ();
E. require the maintenance of the computer (V1 );

In this way, I maintained a "dual assignment"-My family passed this object to the computer Maintainer in the method of asking for computer maintenance; the computer repair engineer finds the corresponding heavy load method do according to my house. In this method, execute my house. computer reinstallation system (). As a result, the dual-sending interaction operation is completed, and all of them are exposed to the other party. Note: exposing identities to each other is the disadvantage of the visitor mode: the coupling between objects is too high. How can this problem be solved, that is, Pu Zhigao's rebellion, he will sell out Jiang Jie he knows.

2) if my house is unfortunate, and the house leaks, I have to seek a House Repair Engineer to repair the roof, and the object of my house will be exposed to the House Repair Engineer. Add a maintenance engineer:

It is used in the client as follows:

House Repair Engineer v2 =   New House Repair Engineer ();
Family E =   New My home ();
E. Require house repair (V2 );

3) it can be seen that for a family, to add a requirement (such as repairing the roof), you only need to add this requirement in itself. The method requires roof repair (), then add a new type of Maintenance Engineer-house maintenance engineer. Of course, I only need to add roof repair () in my house. Other homes (such as your home) do not necessarily need it, so in the corresponding heavy load do method of house repair workers, just do nothing.

Public   Void Do (your home you)
{
//Do not do anything
}

That is to say, it is easy to add new demands. A mature residential area is like this.
However, if a new family is added, that is, if a small strong family is added, all maintenance workers (including basic maintenance workers) should add operations corresponding to the new family, this is very troublesome-it does not comply with the open and closed principles. At this time, the Community should have a mediator responsible for executing commands, which is the convenience of the intermediary mode + command mode for new families in mature communities.

The more I write, the less I speak. I don't know how many people will understand it here. The above are the essence of the discussion with Will & Tiger this afternoon.
In the lower part of this section, I will introduce:
The principle of opening/closing,
Visitor UML diagram in gof,
Differences between Visitor and command,
Weak type set operations,
Advantages and disadvantages of visitor,
Use Cases of visitor.

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.