Visitor Pattern

From eqqon

(Difference between revisions)
Jump to: navigation, search
(New page: Category:CSharp == Motivation for applying the Visitor Pattern == == The complete example Program == <span><span class="S5">using</span><span class="S0"> </span>System<span class="S...)
m (The complete example Program)
Line 3: Line 3:
== Motivation for applying the Visitor Pattern ==
== Motivation for applying the Visitor Pattern ==
-
== The complete example Program ==
+
== The complete example program ==
<span><span class="S5">using</span><span class="S0"> </span>System<span class="S10">;</span><br />
<span><span class="S5">using</span><span class="S0"> </span>System<span class="S10">;</span><br />

Revision as of 09:10, 21 November 2007


Motivation for applying the Visitor Pattern

The complete example program

using System;
using System.Collections.Generic;
using System.Text;

namespace CodeSnippet
{

    public class A
    {
        public virtual void Print(VisitorPattern.Printer printer) { printer.Print(this); } // calls Print(A element)
    }

    public class B : A
    {
        public override void Print(VisitorPattern.Printer printer) { printer.Print(this); } // calls Print(B element)
    }

    public class C : B
    {
        public override void Print(VisitorPattern.Printer printer) { printer.Print(this); } // calls Print(C element)
    }

    namespace NaiveApproach
    {
        public class Printer
        {
            public void Print(A element) { Console.WriteLine("A"); }

            public void Print(B element) { Console.WriteLine("B"); }

            public void Print(C element) { Console.WriteLine("C"); }

            public void Print(A[] array)
            {
                foreach (A element in array)
                {
                    // here we reimplement polymorphic method dispatching!
                    if (element is C)
                        Print(element as C);
                    else if (element is B)
                        Print(element as B);
                    else if (element is A)
                        Print(element);
                }
            }
        }
    }

    namespace VisitorPattern
    {
        public class Printer
        {
            public void Print(A element) { Console.WriteLine("A"); }

            public void Print(B element) { Console.WriteLine("B"); }

            public void Print(C element) { Console.WriteLine("C"); }

            public void Print(A[] array)
            {
                foreach (A element in array)
                {
                    // here we make use of the language's polymorphic method dispatching
                    element.Print(this);
                }
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            A[] array = { new A(), new B(), new C() };
            Console.WriteLine("Naive Approach:");
            new NaiveApproach.Printer().Print(array);
            Console.WriteLine("Visitor Pattern:");
            new VisitorPattern.Printer().Print(array);
            Console.ReadLine();
        }
    }
}