Skip to content

Instantly share code, notes, and snippets.

@miteshsureja
Created June 16, 2018 06:55
Show Gist options
  • Save miteshsureja/d4093c1c8a24bb348f06f2a15ac68a3b to your computer and use it in GitHub Desktop.
Save miteshsureja/d4093c1c8a24bb348f06f2a15ac68a3b to your computer and use it in GitHub Desktop.
Visitor Design Pattern
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VisitorPattern
{
public enum Categories
{
Food,
Oil,
Electronics,
Cloths
}
//element
public interface IElement
{
void Accept(IVisitor visitor);
}
//concrete element
public class Products : IElement
{
public void Accept(IVisitor visitor)
{
Console.WriteLine("Name - {0}, Product Visited - {1}, Category - {2}",
visitor.Name, visitor.ProductVisited, visitor.Category);
}
}
//concrete element
public class Discounts : IElement
{
public void Accept(IVisitor visitor)
{
double discount = 0;
switch (visitor.Category)
{
case Categories.Food:
discount = 10;
break;
case Categories.Electronics:
discount = 20;
break;
case Categories.Cloths:
discount = 15;
break;
default:
discount = 5;
break;
}
Console.WriteLine("Product Name - {0}, Price - {1}, Discount ({2}%) - {3}",
visitor.ProductVisited, visitor.Price, discount, (visitor.Price * discount)/100);
}
}
//visitor
public interface IVisitor
{
string Name { get; set; }
string ProductVisited { get; set; }
double Price { get; set; }
Categories Category { get; set; }
void Visit(IElement element);
}
//concrete visitor
public class GroceriesVisitor : IVisitor
{
public string Name { get; set; }
public string ProductVisited { get; set; }
public double Price { get ; set; }
public Categories Category { get; set; }
public GroceriesVisitor(string name, string productVisited, double price, Categories category )
{
Name = name;
ProductVisited = productVisited;
Price = price;
Category = category;
}
public void Visit(IElement element)
{
element.Accept(this);
}
}
//concrete visitor
public class AppliancesVisitor : IVisitor
{
public string Name { get; set; }
public string ProductVisited { get; set; }
public double Price { get; set; }
public Categories Category { get; set; }
public AppliancesVisitor(string name, string productVisited, double price, Categories category)
{
Name = name;
ProductVisited = productVisited;
Price = price;
Category = category;
}
public void Visit(IElement element)
{
element.Accept(this);
}
}
//concrete visitor
public class FashionVisitor : IVisitor
{
public string Name { get; set; }
public string ProductVisited { get; set; }
public double Price { get; set; }
public Categories Category { get; set; }
public FashionVisitor(string name, string productVisited, double price, Categories category)
{
Name = name;
ProductVisited = productVisited;
Price = price;
Category = category;
}
public void Visit(IElement element)
{
element.Accept(this);
}
}
//entry point
class Program
{
static void Main(string[] args)
{
List<IVisitor> Items = new List<IVisitor>();
Items.Add(new GroceriesVisitor("Visitor1", "Rice", 150, Categories.Food));
Items.Add(new GroceriesVisitor("Visitor2", "Oil", 95, Categories.Oil));
Items.Add(new AppliancesVisitor("Visitor3", "Speakers", 500, Categories.Electronics));
Items.Add(new AppliancesVisitor("Visitor4", "Cameras", 500, Categories.Electronics));
Items.Add(new FashionVisitor("Visitor5", "Shirts", 1500, Categories.Cloths));
Items.Add(new FashionVisitor("Visitor6", "Jeans", 3000, Categories.Cloths));
Console.WriteLine("-----Visiting prices of all items-----");
foreach(var item in Items)
{
item.Visit(new Products());
}
Console.WriteLine("-----Visiting discounts of all items-----");
foreach (var item in Items)
{
item.Visit(new Discounts());
}
Console.Read();
}
}
}
@miteshsureja
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment