Skip to content

Instantly share code, notes, and snippets.

@kosanmil
Last active April 22, 2018 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosanmil/c9e1fe608889c9f949621fc85c84842f to your computer and use it in GitHub Desktop.
Save kosanmil/c9e1fe608889c9f949621fc85c84842f to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
public class House
{
public List<Room> Rooms;
public void AcceptVisitor(IVisitor visitor)
{
//The handyman will perform checks on this house
//(e.g. check the outside walls)
//The house does not care what the handyman does.
visitor.VisitHouse(this);
//Next, the handyman will go from room to room
foreach (var room in Rooms)
{
room.AcceptVisitor(visitor);
}
}
}
public class Room
{
public void AcceptVisitor(IVisitor visitor)
{
//The handyman performs his checks and fixes on this room.
//The room does not care what the handyman does.
visitor.VisitRoom(this);
}
}
//Visitors
public interface IVisitor
{
void VisitHouse(House house);
void VisitRoom(Room room);
}
public class Plumber : IVisitor
{
public void VisitHouse(House house)
{
CheckOutsidePipes(house);
}
public void VisitRoom(Room room)
{
//The plumber checks and fixes leaky pipes (if any) in that room
CheckPipes(room);
}
private void CheckPipes(Room room)
{
//Implementation omitted for the simplicity of the example
}
private void CheckOutsidePipes(House house)
{
//Implementation omitted for the simplicity of the example
}
}
public class CableTechnician : IVisitor
{
public void VisitHouse(House house)
{
CheckOutsideCableWirings(house);
}
public void VisitRoom(Room room)
{
CheckTV(room);
CheckModem(room);
}
private void CheckModem(Room room)
{
//Implementation omitted for the simplicity of the example
}
private void CheckTV(Room room)
{
//Implementation omitted for the simplicity of the example
}
private void CheckOutsideCableWirings(House house)
{
//Implementation omitted for the simplicity of the example
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment