Skip to content

Instantly share code, notes, and snippets.

@kosanmil
Last active February 2, 2019 20:15
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/0f406b91166c2dddb91c666c5b23e124 to your computer and use it in GitHub Desktop.
Save kosanmil/0f406b91166c2dddb91c666c5b23e124 to your computer and use it in GitHub Desktop.
public class GetUnitByNameVisitor : IVisitor
{
//The parameter
private string _unitName;
//The return value
public Unit ReturnedUnit { get; private set; }
public GetUnitByNameVisitor(string unitName)
{
_unitName = unitName;
}
public void VisitProcessUnit(ProcessUnit unit)
{
//Accessing the parameter using the private field.
if (unit.Name == _unitName)
{
//If this process unit is the one we are looking for
//return, since there is no need to search in other children
ReturnedUnit = unit;
return;
}
foreach (var childUnit in unit.ChildUnits)
{
childUnit.AcceptVisitor(this);
//If the unit has been found in this child, return,
//since there is no need to search in the other siblings.
if (ReturnedUnit != null)
return;
}
}
public void VisitAgentUnit(AgentUnit unit)
{
//Accessing the parameter using the private field
if (unit.Name == _unitName)
ReturnedUnit = unit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment