Complex Interactions
Multiple Dispatching
Handling interactions between multiple types may make the program become quite messy. For example, consider a system that resolves and performs mathematical expressions. You need to support numbers numbers, numbers × numbers, and so on, numbers are the base class of a series of digital objects. However, when we give only A B, we don't know the exact type of A or B, how do we make them correct interaction?
The answer to the question may be that you didn't think of: Java can only do single dispatch. That is, if you operate the unknown objects for excess type, Java can only enable dynamic binding mechanisms for one of these types. This doesn't solve the above problems, so you still have to handle your own (write code) frame test type and generate your own dynamic binding behavior.
This solution is multiple dispatching. Don't forget to be implemented by the member function call, so if you want Double Dispatching, you must have two member functions: the first call determines the first unknown type, the second time Call determines the second unknown type. For multiple assignments, you have to have a polymorphism that can be called to determine all types. Typically, you can use a member function call to generate more than one dynamic member function call by setting a configuration item, thereby determining more than one type in this process. In order to do this, you need more than one polymorphic method call: Each assignment requires a call. The (polymorphic) method in the following example is Compete () and Eval (), which are all the same class (type) member functions. (In this case, only twice is assigned, that is, double dispatch). If you need to handle interactions between two systems (Hierarchies), each system must have a polymorphism method call.
Here is a multi-partial example: (stone cutting cloth)
//: multipledispatch: PaperScissorsRock.java// Demonstration of multiple dispatching.package multipledispatch; import java.util *; import junit.framework *; // An enumeration type:.. Class Outcome {private String name; private Outcome (String name ) {this.name = name;} public final static outcome win = new Outcome ("wins"), Lose = New Outcome ("loses"), DRAW = New Outcome ("DRAWS"); public string toString () {Return name;}} interface Item {Outcome compete (Item it); Outcome eval (Paper p); Outcome eval (Scissors s); Outcome eval (Rock r);} class Paper implements Item {public Outcome compete (Item it) {return IT.EVAL (this);} public output {return outcome.draw;} public outcome evAl (scissors s) {return outcome.win;} public output {RETURN OUTCOME.LOSE;} Public string toString () {return "paper";}} class scissors imports item {public output (item it) {return it.eval (this);} public output {return outcome.lose (Paper P) {Return Outcome.lose ;} Public Outcome eval (Scissors s) {return Outcome.DRAW;} public Outcome eval (Rock r) {return Outcome.WIN;} public String toString () {return "Scissors";}} class Rock implements Item {public Outcome Compete (item it) {return it.eval (this);} public output {return outcome.win;} public outcome eval (scissors s) {return outcome.lose;} public Outcome Eval (Rock R) {Return Outcome.draw;} public string toString () {Return "Rock";}} class itemgenerator {private static random rand = new random (); public static item newItem () {switch (rand.nextint (3)) { Default: cas 0: return new scissors (); case 1: return new paper ();
Case 2: return new rock ();}}} Class Compete {public static void match (item a, item b) {system.out.println (A " a.compete (b) " vs. " b);}} public class paperscissorsrock extends testcase {static int size = 20; public void test () {for (int i = 0; i Suppose you say that you have a set of first-class hierarchies (Class Hierachy), these classes cannot be changed; they may be bought from third parties, so you can't change this class hierarchy system. However, you may want to add new polymorphism methods to this class hierarchy, usually this must have new things to the base class interface. So the problem is coming: You need to add a new method to the base class, and you can't move the basis. What should I do? Designing the design pattern of such problems called "Visitor" (Visitor) "(" Design Mode "), which is finally talked about, it is based on the two-way distribution mechanism in the previous section. Visitor mode allows you to extend the original type of interface by creating another independent Visitor type class hierarchy, so that the Virtualize implements the operation originally for the original type. The original type of object is just a simple "Accept" visitor, then calls the member function of the visitor dynamically binding. //: visitor: BeeAndFlowers.java// Demonstration of "visitor" pattern.package visitor; import java.util *; import junit.framework *; interface Visitor {void visit (Gladiolus g); void visit (Runuculus r).. ; void visit (Chrysanthemum c);} // The Flower hierarchy can not be changed: interface Flower {void accept (Visitor v);} class Gladiolus implements Flower {public void accept (Visitor v) {v.visit (this);} } class Runuculus implements Flower {public void accept (Visitor v) {v.visit (this);}} class Chrysanthemum implements Flower {public void accept (Visitor v) {v.visit (this);}} // Add the ability to produce a string: class stringVal implements Visitor {String s; public String toString () {return s;} public void visit (Gladiolus g) {s = "Gladiolus";} public void visit (Runuculus r) {s = "Runuculus "} Public void visit (chrysanthemum c) {s =" chrysanthem ";}}}}} // add the Ability to do" bee "Activities: Class Bee Implements Visitor {public void Visit ( Gladiolus g) {System.out.println ( "Bee and Gladiolus");} public void visit (Runuculus r) {System.out.println ( "Bee and Runuculus");} public void visit (Chrysanthemum c) {System. out.println ( "Bee and Chrysanthemum");}} class FlowerGenerator {private static Random rand = new Random (); public static Flower newFlower () {switch (rand.nextInt (3)) {default: case 0: return new Gladiolus (); case 1: return new Runuculus (); case 2: return new Chrysanthemum ();}}} public class BeeAndFlowers extends TestCase {List flowers = new ArrayList (); public BeeAndFlowers () {for (int i = 0 i <10; i ) Flowers.add (FlowerGenerator.newFlower ());} public void test () {// It's almost as if I had a function to // produce a Flower string representation: StringVal sval = new StringVal (); Iterator it = flowers .iterator (); while (% it.hasnext ()) {((FLOWER) IT.Next ()). Accept (sval); system.out.println (sval);} // perform "bee" Operation on All Flowers : Bee bee = new bee (); it = flowers.iterator (); while (it.hasNext ()) ((5) ()) ()). Accept (bee);} public static void main (String Args ]) {Junit.textui.teStrunner.Run (BEEANDFLOWERS.CLASS);}} ///: ~ Exercise 1. Write a business modeling system, including three types of INHABITANT: gnomes (representative engineers), elf (representative Market staff), giant (representative manager). Write a Project class to create different inhabitants and let them interact, you must use multiple assignments. 2. Remote Exercise 1, make their interaction more detailed. Every INHABITANT can be randomly generated by getWeapon (): gnomes with Jargon or Play, elf with InventFeature or SellimaginaryProduct, giant with edioct and schedule. You must decide which weapons in each interaction win the weapon loss (just like PaperSCissRock.java). Add a Battle () member function to the Project class, which accepts two INHABITANTS (as a parameter), so that they both fire. Add a Meeting () member function to the Project class, create a set of gnomes, elves, and giants, and let all groups will be in the last group. 3. Remote PaperScissRock.java, use the method of querying instead of the dual-channel assignment. The easiest way to write a value of a value of MAPS, use the class name of each object as a key of the MAP. Then look for this method: ((map) map.get (O1.GetClass ())). Get (O2.GetClass ()) This method is very easy to configure the system. When do you use this method to be more appropriate than hard coding dynamic distribution? Can you write a syntax simple system to realize dynamic dispatch? 4. Re-practice 2 with the method of practicing 3. table of Contents