Advertisement
ivandrofly

Visitor Design Pattern - C# - v2

Feb 6th, 2021 (edited)
1,364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. // github: https://github.com/ivandrofly
  2. namespace VisitorPattern
  3. {
  4.     using System;
  5.  
  6.     public interface IVisitor
  7.     {
  8.         void Visit(IGoodGuest goodGuest);
  9.         void Visit(IBadGuest badGuest);
  10.     }
  11.  
  12.     public interface IVisitable
  13.     {
  14.         public string Name { get; set; }
  15.         void Accept(IVisitor visitor);
  16.     }
  17.  
  18.     public interface IGoodGuest : IVisitable
  19.     {
  20.     }
  21.  
  22.     public interface IBadGuest : IVisitable
  23.     {
  24.     }
  25.  
  26.     internal class BadGuest : IBadGuest
  27.     {
  28.         public string Name { get; set; }
  29.  
  30.         public void Accept(IVisitor visitor) => visitor.Visit(this);
  31.     }
  32.  
  33.     internal class GoodGuest : IGoodGuest
  34.     {
  35.         public string Name { get; set; }
  36.  
  37.         public void Accept(IVisitor visitor) => visitor.Visit(this);
  38.     }
  39.  
  40.     public class WeJudgeVisitor : IVisitor
  41.     {
  42.         public void Visit(IGoodGuest goodGuest) => Console.WriteLine($"You are welcome: {goodGuest.Name}");
  43.         public void Visit(IBadGuest badGuest) => Console.WriteLine($"You are not welcome here: {badGuest.Name}");
  44.     }
  45.  
  46.     public class WeDontJudgeVisitor : IVisitor
  47.     {
  48.         public void Visit(IGoodGuest goodGuest) => Console.WriteLine($"You are welcome {goodGuest.Name}");
  49.  
  50.         public void Visit(IBadGuest badGuest) => Console.WriteLine($"You are welcome: {badGuest.Name}");
  51.     }
  52.  
  53.     internal class Program
  54.     {
  55.         private static void Main(string[] args)
  56.         {
  57.             // VisitorPattern2.TestPattern();
  58.             // Console.ReadLine();
  59.  
  60.             GoodGuest goodGuest = new GoodGuest() {
  61.                 Name = "Mather Luther King",
  62.             };
  63.             BadGuest badGuest = new BadGuest() {
  64.                 Name = "Adolf Hitler"
  65.             };
  66.  
  67.             IVisitor weJudgeVisitor = new WeJudgeVisitor();
  68.             IVisitor weDontJudgeVisitor = new WeDontJudgeVisitor();
  69.  
  70.             // invoke we don't judge visitor
  71.             goodGuest.Accept(weDontJudgeVisitor);
  72.             badGuest.Accept(weDontJudgeVisitor);
  73.            
  74.             Console.WriteLine();
  75.            
  76.             // invoke we judge visitor
  77.             goodGuest.Accept(weJudgeVisitor);
  78.             badGuest.Accept(weJudgeVisitor);
  79.  
  80.             Console.ReadLine();
  81.         }
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement