Subscribe belonging to yourself (Visitor)

zhaozj2021-02-16  56

Sometimes. We always don't want to have no guest. But more hope is our friend. But if

It is difficult to judge those who are our friends with an ordinary pen.

For. If there is a notebook with a friendship and a notebook that can present a friendship. So much better.

Just as the container like ARRAYLIST or HashTable is loaded with data. All data

Don't dymed the same color --- Object, you must time to judge whether you want the data type. You may

Always write if (o is yourtype) is like this. It is not necessary. Because you are now limited

Visitors. Instead of accommodating all containers. Maybe you will say that you can use the array yourtype [] YT

So. Unfortunately. You cannot own iterations and insert updates. Delete. Finding containers can be completed

Things. Similarly, in order to rinse the objects in the container, don't really have to replace the container.

It itself. So now you exist, how to specify a container you need. This is why

The necessity of proposing Visitor mode. So we still divide and abstraction. Through the above description

Two work is needed to complete this operation. One is the other of the Visitors responsible for custom access to the other.

It is used to determine the type of access. This is to say that he is guarding. Only if the type can be passed. In fact, this

Mode is simple. OK is still starting from a simple hypothesis.

We started with the list of friends I just said. Friends have friendship pen, so they can write to storage.

There is no possibility.

// Visitors abstract object start

Public interface ivisitors {void listvisitor (ilist il); // iterative judgment access to friends in the container Void Operationf (Friend F); // If it is a friend, display}

Public class frvisitor: ivisitors {

Public void listvisitor (ilist il) {int count, item = 0; count = il.count; // iterative element for (item; item

// Accessible access interface

Public interface ivisitable {void accept (ivisitor v);}

Public class friend: ipisitable {public string name; public friend (string n) {this.name = n;

Public void accept (ivisitor v) {v.operationf (this);}}

// Customer call

Public class client {public static int main (string [] args) {arraylist al = new arraylist (); Friend F1 = Friend ("YARSHRAY"); Friend F2 = Friend ("Tommy"); Al.Add ("Badboy" ); Al.Add ("Bow"); Al.Add (F1); Al.Add (f2);

Frvisitor fv = new frvisitor (); fv.listvisitor (al); return 0;}}

In the end, only friends have been displayed. In fact. When you use components instead of direct and database to deal

Use Visitor mode to play as effect as the data view. Customers don't want to see the data mask. And

It is interested in the customer. Visitor itself does not store any data. It is just as it asks and judge. And Visiable

Then be handled whether it is handled. Storage is still handed over to the container.

转载请注明原文地址:https://www.9cbs.com/read-27418.html

New Post(0)