Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // github: https://github.com/ivandrofly
- namespace VisitorPattern
- {
- using System;
- public interface IVisitor
- {
- void Visit(IGoodGuest goodGuest);
- void Visit(IBadGuest badGuest);
- }
- public interface IVisitable
- {
- public string Name { get; set; }
- void Accept(IVisitor visitor);
- }
- public interface IGoodGuest : IVisitable
- {
- }
- public interface IBadGuest : IVisitable
- {
- }
- internal class BadGuest : IBadGuest
- {
- public string Name { get; set; }
- public void Accept(IVisitor visitor) => visitor.Visit(this);
- }
- internal class GoodGuest : IGoodGuest
- {
- public string Name { get; set; }
- public void Accept(IVisitor visitor) => visitor.Visit(this);
- }
- public class WeJudgeVisitor : IVisitor
- {
- public void Visit(IGoodGuest goodGuest) => Console.WriteLine($"You are welcome: {goodGuest.Name}");
- public void Visit(IBadGuest badGuest) => Console.WriteLine($"You are not welcome here: {badGuest.Name}");
- }
- public class WeDontJudgeVisitor : IVisitor
- {
- public void Visit(IGoodGuest goodGuest) => Console.WriteLine($"You are welcome {goodGuest.Name}");
- public void Visit(IBadGuest badGuest) => Console.WriteLine($"You are welcome: {badGuest.Name}");
- }
- internal class Program
- {
- private static void Main(string[] args)
- {
- // VisitorPattern2.TestPattern();
- // Console.ReadLine();
- GoodGuest goodGuest = new GoodGuest() {
- Name = "Mather Luther King",
- };
- BadGuest badGuest = new BadGuest() {
- Name = "Adolf Hitler"
- };
- IVisitor weJudgeVisitor = new WeJudgeVisitor();
- IVisitor weDontJudgeVisitor = new WeDontJudgeVisitor();
- // invoke we don't judge visitor
- goodGuest.Accept(weDontJudgeVisitor);
- badGuest.Accept(weDontJudgeVisitor);
- Console.WriteLine();
- // invoke we judge visitor
- goodGuest.Accept(weJudgeVisitor);
- badGuest.Accept(weJudgeVisitor);
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement