コード例 #1
0
        //Visitor
        public void Visitor()
        {
            ObjectGuard     guard   = new ObjectGuard();
            AbstractVisitor visitor = new ConcreteVisitor();

            guard.Attach(new ConcreteElement1());
            guard.Attach(new ConcreteElement2());
            guard.Notify(visitor);
        }
コード例 #2
0
        public static void Run()
        {
            var elementA = new ConcreteElementA();
            var elementB = new ConcreteElementB();
            var visitor  = new ConcreteVisitor();

            elementA.Accept(visitor);
            elementB.Accept(visitor);
        }
コード例 #3
0
        public void TestVisitor()
        {
            var context = new VisitorContext();

            var concreteElementOne = new ConcreteElementOne();

            var visitor = new ConcreteVisitor();

            context.AddElement(concreteElementOne);
            context.Accept(visitor);
        }
コード例 #4
0
        private static void GenericVisitor()
        {
            var list = new List <IVisitee>
            {
                new ConcreteVisitee1(),
                new ConcreteVisitee2(),
                new ConcreteVisitee1(),
            };
            var visitor = new ConcreteVisitor();

            foreach (var visitee in list)
            {
                visitee.Accept(visitor);
            }
        }
コード例 #5
0
    public static void Main()
    {
        var elements = new Element[]
        {
            new ConcreteElementA("EA1"),
            new ConcreteElementA("EA2"),
            new ConcreteElementB("EB1"),
            new ConcreteElementA("EA3"),
            new ConcreteElementC("EC1"),
            new ConcreteElementC("EC2"),
            new ConcreteElementA("EA5"),
            new ConcreteElementB("EB2"),
            new ConcreteElementC("EC3")
        };
        ConcreteVisitor visitor = new ConcreteVisitor();

        foreach (var element in elements)
        {
            element.Accept(visitor);
        }
        Console.ReadKey();
    }
コード例 #6
0
ファイル: Program.cs プロジェクト: dixonte/VisitorFody
        static void Main(string[] args)
        {
            var people = new List <Person>();

            people.Add(new Employee()
            {
                Name = "Frank"
            });
            people.Add(new Employee()
            {
                Name = "Rob"
            });
            people.Add(new Employee()
            {
                Name = "Joe"
            });
            people.Add(new Director()
            {
                Name = "Andy"
            });
            people.Add(new Director()
            {
                Name = "Barry", GolfPartner = "Fabio"
            });
            people.Add(new Employee()
            {
                Name = "Stuart"
            });


            int employeeCountByVisitor = 0;

            // Since ConcreteVisitor already implements IPersonVisitor, these statements should generate the same IL
            var concreteVisitor = VisitorFactory <IPersonVisitor> .Create(new ConcreteVisitor());

            concreteVisitor = new ConcreteVisitor();

            // Anonymous classes provide their implementations by way of Action<> delegates
            // The name of the properties is not important
            var anonymousVisitor = VisitorFactory <IPersonVisitor> .Create(new
            {
                Employee = new Action <Employee>(e => employeeCountByVisitor++),
                Director = new Action <Director>(d =>
                {
                    d.PlayGolf();
                })
            }, ActionOnMissing.NoOp);

            // Concrete classes can also provide implementations by way of Action<> delegates
            // Use caution, as any properties of type Action<> that match a Visit method in the given interface will be wired up as implementation
            var concreteDelegate = VisitorFactory <IPersonVisitor> .Create(new ConcreteDelegate()
            {
                Employee = new Action <Employee>(e => employeeCountByVisitor++)
            });

            // If for some reason you want to, you can use VisitorFactory to inject visitor interfaces into concrete classes with methods
            // At the moment, this will take the first method that matches the required signature, regardless of name.
            // This will probably change in future.
            var concreteDuck = VisitorFactory <IPersonVisitor> .Create(new ConcreteDuck());

            // In the case where a concrete class has a method and a Action<> delegate that both match, the method trumps the delegate
            var delegateDuck = VisitorFactory <IPersonVisitor> .Create(new DelegateDuck()
            {
                Employee = new Action <Employee>(e => employeeCountByVisitor++),
                Director = new Action <Director>(director =>
                {
                    director.PlayGolf();
                })
            });

            foreach (var person in people)
            {
                person.Accept(delegateDuck);
            }

            Console.WriteLine($"There are {employeeCountByVisitor} employees.");
            Console.Read();
        }
コード例 #7
0
 public void Accept(ConcreteVisitor visitor)
 {
     visitor.Visit(this);
 }
コード例 #8
0
 public void Accept(ConcreteVisitor visitor)
 {
     visitor.Visit <ConcreteElementC>(this);
 }