Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.