コード例 #1
0
 public void Visit(Asteroid asteroid)
 {
     Console.WriteLine("the " + this.Name + " mines " + asteroid.Metal + " tons of metal from an asteroid");
 }
コード例 #2
0
 public void Visit(Asteroid asteroid)
 {
     Console.WriteLine("the " + this.Name + " mines " + asteroid.Metal + " tons of metal from an asteroid");
 }
コード例 #3
0
        public static void Start()
        {
            Console.WriteLine("\nMultipleDispatchWithVisitor\n");

            SpaceShip ship = new SpaceShip() { Name = "enterprise" };
            IVisitable a = new Planet() { Gravity = 1 };
            IVisitable b = new Planet() { Gravity = 5 };
            IVisitable c = new Asteroid() { Metal = 3 };
            IVisitable d = new Asteroid() { Metal = 2 };

            a.Accept(ship);
            b.Accept(ship);
            c.Accept(ship);
            d.Accept(ship);

            // Functionality extracted from visitable to visitor. It would be easy to
            // add new visitors.

            // Downside:
            // - Boilerplate code in visitable
            // - Tight coupling between visitor and visitable
            // - With every new visitable all visitors need to be amended
        }