using System; // extend the following visitor pattern with an additional city class // without changing existing code except main. // ************** SEE SOLUTION AT END: ******************** interface city // visitees { void accept(visitor v); } interface visitor { void visit(NY n); void visit(LA n); } class NY : city { public void accept(visitor v) { v.visit(this); } } class LA : city { public void accept(visitor v) { v.visit(this); } } //// sample visitor class tourist : visitor { public void visit(NY n) { Console.WriteLine("goto statue of liberty"); } public void visit(LA n) { Console.WriteLine("goto hollywood"); } }//tourist public class vex { public static void Main() { city[] Cs = {new NY(), new LA()}; visitor you = new tourist(); foreach(city c in Cs) c.accept(you); }//Main } //////////////////// extension interface visitor2 : visitor { void visit(Paris x); } class Paris : city { public void accept(visitor v) { if (v is visitor2) { ((visitor2)v).visit(this); } else throw new Exception("this visitor can't visit Paris"); } } class globaltourist : tourist, visitor2 { public void visit(Paris x) { Console.WriteLine("buvez beaucoup du vin"); } } //// another example interface visitor3 : visitor2 { void visit(Florida x); } class Florida : city { public void accept(visitor v) { ((visitor3)v).visit(this); } } // Even though this "works", type errors are missed at compile time. class confusedtourist : globaltourist, visitor3 { public void visit(Florida x) {Console.WriteLine("Oops this is not a city. You're in a swamp full of alligators!");} } //// but when you compile // new Florida().accept(new tourist()) // will you get errors? No, and this illustrates a defect in this oop design, // which is already using dynamic dispatch most effectively.