none
Visitor Pattern By using Delegate RRS feed

  • General discussion

  • Hi,

    Here is an implementation of the Visitor Pattern using a Delegate.

    namespace Program
    {
        using System;
    
        public delegate T VisitorFun<V, T>(V f);
    
        // Interface that is multiply inherited, and ready to accept a visitor
        public interface IFigureF
        {
            String GetName();
            int GetId();
            T Accept<T>(VisitorFun<IFigureF, T> v);
        };
    
        // Simple Figure 
        public class SimpleFigureF : IFigureF
        {
            private String _name;
            private int _id;
    
            public SimpleFigureF(String name) { this._name = name; }
            public SimpleFigureF(String name, int id) { this._name = name; this._id = id; }
            public String GetName() { return this._name; }
            public int GetId() { return this._id; }
            public T Accept<T>(VisitorFun<IFigureF, T> v)
            {
                return v(this);
            }
        }
    
        // Composite Figure : array of simple figures
        public class CompositeFigureF : IFigureF
        {
            private String _name;
            private int _id;
            private IFigureF[] _figureArray;
    
    
            public CompositeFigureF(String name, IFigureF[] s)
            {
                this._name = name; this._figureArray = s;
            }
            public CompositeFigureF(String name, int id, IFigureF[] s)
            {
                this._name = name; this._id = id; this._figureArray = s;
            }
            public String GetName() { return this._name; }
            public int GetId() { return this._id; }
    
            public T Accept<T>(VisitorFun<IFigureF, T> v)
            {
                foreach (IFigureF f in _figureArray)
                {
                    f.Accept(v);
                }
                return v(this);
            }
        }
    
        class Prog
        {
            // Extension by a visitor function
            public static VisitorFun<IFigureF, String>
              MakeNameFigureVisitorFun()
            {
                string _state = "";
                return obj =>
                {
                    if (obj is SimpleFigureF) _state += "I am visited!" + " name = " + obj.GetName() + ", Id = " + obj.GetId() + "\n";
                    else if (obj is CompositeFigureF) _state = obj.GetName() + " / Simple Figures : \n" + _state;
                    return _state;
                };
            }
    
            static void Main(string[] args)
            {
                Console.WriteLine("-- Visitor --");
    
                IFigureF Square = new SimpleFigureF("Square", 1);
                IFigureF Triangle = new SimpleFigureF("Triangle", 2);
                IFigureF Cercle = new SimpleFigureF("Cercle", 3);
                IFigureF Diamond = new SimpleFigureF("Diamond", 4);
    
                IFigureF Drawer = new CompositeFigureF("Drawer",
                                                       new IFigureF[4] { Square, Triangle, Cercle, Diamond });
    
                VisitorFun<IFigureF, String> f = MakeNameFigureVisitorFun();
                Console.WriteLine("{0} : {1}", Square.GetType(), Square.Accept(f));
                VisitorFun<IFigureF, String> g = MakeNameFigureVisitorFun();
                Console.WriteLine("{0} : {1}", Drawer.GetType(), Drawer.Accept(g));
            }
        }
    }
    
    Regards.

     

     

    Thursday, April 1, 2010 8:22 PM

All replies

  • Interesting, thanks Link

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Thursday, April 1, 2010 8:40 PM
  • very nice, but you should discuss briefly what is the visitor pattern
    Mohammad Elsheimy, Technical Writer, http://JustLikeAMagic.WordPress.com
    Thursday, April 1, 2010 8:48 PM
  • very nice, but you should discuss briefly what is the visitor pattern
    Mohammad Elsheimy, Technical Writer, http://JustLikeAMagic.WordPress.com

    The Visitor represents an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Thursday, April 1, 2010 8:49 PM
  • Hi,

     

    As John Said, Visitor lets you define a new operation without changing the classes of the elements on which it operates.

    Example :


    This pattern can be observed in the operation of a taxi company. When a person calls a taxi company (accepting a visitor),
    the company dispatches a cab to the customer.
    Upon entering the taxi the customer, or Visitor, is no longer in control of his or her own transportation, the taxi (driver) is.

     

    Regards,

    Thursday, April 1, 2010 9:36 PM
  • aha, thank you
    Mohammad Elsheimy, Technical Writer, http://JustLikeAMagic.WordPress.com
    Thursday, April 1, 2010 9:45 PM
  • thanks for sharing!
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, April 8, 2010 2:39 AM