Skip to content

Instantly share code, notes, and snippets.

@kosanmil kosanmil/GetUnitByNameVisitor.cs Secret
Last active Feb 2, 2019

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