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 accept (Visitor Visitor); // Main task ---- Receive Visitor's gift
}
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 value; // Note here is String
Public firsthand (String String) {// Constructor of a hand
Value = String;
}
Public string getValue () {// Get a gift
Return Value;
}
/ / Define the specific content of Accept here is a very simple invocation
Public void accept (visitor visitor) {
Visitor.visitfirsthand (this); // Receive a gift from the voyage
}
}
B: Two hands
Public Class SecondHand IMPLEments Leader {
Private float value; // Note here is Float
Public SecondHand (Float string) {// Texture of the two hand
Value = String;
}
Public float getValue () {// Get a gift
Return Value;
}
/ / Define the specific content of Accept here is a very simple invocation
Public void accept (visitor visitor) {
Visitor.visitfirsthand (this); // Receive a gift from the voyage
}
}
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 you want to give
(Leader) .accept (this); // Different leaders enter different implementations
}
}
Public void visitfirsthand (firsthand first) {
System.out.println ("The gift is:" first.getvalue ());
}
Public void visitsecondhand (SecondHand Second) {
System.out.println ("Gifts is:" Second.getValue ());
}
}
3, write test classes:
Public class test {
Public static void main (string args []) {
Visitor Visitor = New Visitme ();
FirstHand Present = New FirstHand ("Ten Brain Platinum");
Visitor.visitfirstHand (Present);
Collection list = new arraylist ();
List.add (New Firsthand ("Ten Brain Platinum"));
List.add (New Float ("a pound of small snacks"))))))))))))))))); // To illustrate different, if you want to run, do type conversion.
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 Accept method. When traversing the Collection, the specific type of visited is called 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.