Design mode Visitor - send gifts (original)

xiaoxiao2021-03-06  74

Design mode Visitor - send gifts

This year, the New Year is not a gift, and the gift is only backed up. I heard this implied advertisement, my head suddenly lit. Because recently, because something must be done, the leadership must be given to the unit. What to send, it is really a headache, but also a brain gold, grandmother. . . , Corruption, sin!

First, you must analyze the object of the gift, and the unit has two leaders, one positive, one. So give different leadership gifts are also different (Ha, should the income are proportional to the output, it is better to return.

1. Here, first define the leadership into an interface class:

Public interface leader {

Public void backinfo (Visitor Visitor); // Received feedback from Visitor (Visitors)

}

Define the visitors into another interface class:

Public Interface Visitor

{

Public void visitfirsthand (firsthand first); // Visit a hand (with gift)

Public void visitsecondhand (SecondHand Second); // Visit the two hands (gifts with belts)

Public void visitcollection (Collection Collection); // Judging is a boy or two hands

}

2, below we have to achieve these two interfaces:

A: a hand

Public class firsthand implements leader {

PRIVATE STRING Present;

Public firsthand (String present) {// Constructor of a hand

Present = present;

}

Public string getpresent () {// Get a gift

Return Present;

}

// Define the specific content of the BACKINFO is a very simple call

Public void backinfo (visitor visitor) {

Visitor.visitfirsthand (this); // Provide feedback to visitors

}

}

B: Two hands

Public Class SecondHand IMPLEments Leader {

Private string present (String present) {// Texture function of the two hand

Present = present;

}

Public String getPresent () {// Gift Return Present;}

// Define the specific content of the BACKINFO is a very simple call

Public void backinfo (visitor visitor) {

Visitor.visitsecondHand (this); // feedback on visiting people

}

}

C: Visitors (I)

Public class visitme imports visitor {

Public Void Visitcollection (Collection Collection) {

Iterator itrator = collection.iterator ();

While (item.hasnext ()) {

Object o = iterator.next ();

IF (o instanceof leader) // judge which leader

((Leader) o) .BackInfo (this); // Different leadership feedback information}}

Public void visitfirsthand (firsthand first) {

System.out.println ("The gift is:" first.getpresent () ", your business is working!");

}

Public void visitsecondhand (SecondHand Second) {

System.out.println ("Gifts is:" SECond.getPresent () ", your business is not good!");

}

}

3, write test classes:

Public class test {

Public static void main (string args []) {

Visitor Visitor = New Visitme ();

Collection list = new arraylist ();

List.add (New Firsthand ("Ten Brain Platinum"));

List.add (New SecondHand)));

Visitor.visitcollection (List);

}

4, explanation:

A: The code is only used to learn Visitor mode. If you want to run, you must do a change.

B: firstHand, SecondHand is just a specific implementation. It can actually expand to more implementations. The entire core mystery is in the Backinfo method, when traversing the Collection, call the specific type of visited by the corresponding Accept method. This step determines the type of visitor

C: Using the visitor mode is a small change in the object type in the object group structure, that is, leaders rarely change. 5, especially gratitude: Thanks to the comments of Kvill users, in order to better illustrate the problem, I will modify the example. In addition, it is to say: This article is standing on me (visitor) position to see the problem, so I care about the situation of things (that is, the response given to me). You have given the leaders to send gifts (when leadership is initialized), then you will go to the leadership, leadership answers you have received a certain gift, how to do things, etc. If it is standing in the leader, he cares about the problem of bribery (haha, but can also consider if this is the case, how to implement).

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

New Post(0)