Skip to content

Commit 1e64c1b

Browse files
committed
Update visitor pattern examples
1 parent de74f46 commit 1e64c1b

31 files changed

+138
-170
lines changed

src/BehavioralPatterns/Visitor/VisitorLibrary/CompanyExample/Company.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,19 @@ namespace VisitorLibrary.CompanyExample;
55

66
public class Company
77
{
8-
private readonly List<Employee> _employees = new List<Employee>();
8+
private readonly List<Employee> _employees = new();
99

10-
public void Hire(Employee employee)
11-
{
10+
public void Hire(Employee employee) =>
1211
_employees.Add(employee);
13-
}
1412

15-
public void LayOff(Employee employee)
16-
{
13+
public void LayOff(Employee employee) =>
1714
_employees.Remove(employee);
18-
}
1915

2016
public void ReviewEmployeesAtEndOfTheYear(IPerformanceReview performanceReview)
2117
{
22-
foreach (Employee employee in _employees)
18+
foreach (var employee in _employees)
2319
{
2420
employee.Evaluate(performanceReview);
2521
}
2622
}
27-
}
23+
}

src/BehavioralPatterns/Visitor/VisitorLibrary/CompanyExample/CompanyExecutor.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ public static void Execute()
1515
company.Hire(new ProductOwner());
1616
company.Hire(new ProjectManager());
1717

18-
company.ReviewEmployeesAtEndOfTheYear(new IncomeRegulation());
18+
Console.WriteLine("\nIt's time for annual performance reviews...\n");
1919

20-
Console.WriteLine("\nStarting second phase of performance review...\n");
20+
Console.WriteLine("\nStarting review for the income regulation...\n");
21+
company.ReviewEmployeesAtEndOfTheYear(new IncomeRegulation());
2122

23+
Console.WriteLine("\nStarting review for the vacation regulation...\n");
2224
company.ReviewEmployeesAtEndOfTheYear(new VacationRegulation());
2325
}
24-
}
26+
}

src/BehavioralPatterns/Visitor/VisitorLibrary/CompanyExample/Elements/Common/Employee.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@ public Employee(string name, double annualSalary, int vacationDays)
99
Name = name;
1010
AnnualSalary = annualSalary;
1111
VacationDays = vacationDays;
12+
13+
Console.WriteLine(
14+
"{0} {1} has annual salary of {2:C} and {3} vacation days",
15+
GetType().Name,
16+
Name,
17+
AnnualSalary,
18+
VacationDays);
1219
}
1320

1421
public string Name { get; set; }
15-
1622
public double AnnualSalary { get; set; }
17-
1823
public int VacationDays { get; set; }
1924

20-
public void Evaluate(IPerformanceReview performanceReview)
21-
{
25+
public void Evaluate(IPerformanceReview performanceReview) =>
2226
performanceReview.Appraise(this);
23-
}
24-
}
27+
}

src/BehavioralPatterns/Visitor/VisitorLibrary/CompanyExample/Elements/Common/IEmployeeEvaluation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ namespace VisitorLibrary.CompanyExample.Elements.Common;
55
public interface IEmployeeEvaluation
66
{
77
void Evaluate(IPerformanceReview performanceReview);
8-
}
8+
}

src/BehavioralPatterns/Visitor/VisitorLibrary/CompanyExample/Elements/Developer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ public Developer()
88
: base("Marc Murray", 80000, 25)
99
{
1010
}
11-
}
11+
}

src/BehavioralPatterns/Visitor/VisitorLibrary/CompanyExample/Elements/ProductOwner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ public ProductOwner()
88
: base("Arnold Aster", 75000, 25)
99
{
1010
}
11-
}
11+
}

src/BehavioralPatterns/Visitor/VisitorLibrary/CompanyExample/Elements/ProjectManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ public ProjectManager()
88
: base("Anna Dover", 82000, 27)
99
{
1010
}
11-
}
11+
}

src/BehavioralPatterns/Visitor/VisitorLibrary/CompanyExample/Visitors/Common/IPerformanceReview.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ namespace VisitorLibrary.CompanyExample.Visitors.Common;
55
public interface IPerformanceReview
66
{
77
void Appraise(IEmployeeEvaluation employeeEvaluation);
8-
}
8+
}

src/BehavioralPatterns/Visitor/VisitorLibrary/CompanyExample/Visitors/IncomeRegulation.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ public class IncomeRegulation : IPerformanceReview
77
{
88
public void Appraise(IEmployeeEvaluation employeeEvaluation)
99
{
10-
var employee = employeeEvaluation as Employee;
10+
if (employeeEvaluation is not Employee employee)
11+
{
12+
throw new ArgumentException("Appraise method works only with Employee objects");
13+
}
1114

1215
// We've had a great year, so 10% pay raises for everyone!
1316
// In case that we need different rules for pay raise, we would change IPerformanceReview interface
@@ -20,4 +23,4 @@ public void Appraise(IEmployeeEvaluation employeeEvaluation)
2023
employee.Name,
2124
employee.AnnualSalary);
2225
}
23-
}
26+
}

src/BehavioralPatterns/Visitor/VisitorLibrary/CompanyExample/Visitors/VacationRegulation.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ public class VacationRegulation : IPerformanceReview
77
{
88
public void Appraise(IEmployeeEvaluation employeeEvaluation)
99
{
10-
var employee = employeeEvaluation as Employee;
10+
if (employeeEvaluation is not Employee employee)
11+
{
12+
throw new ArgumentException("Appraise method works only with Employee objects");
13+
}
1114

1215
// And because you all helped have such a great year,
13-
// all my employees get three extra paid time off days each!
16+
// all employees will get three extra paid time off days!
1417
employee.VacationDays += 3;
1518

1619
Console.WriteLine(
@@ -19,4 +22,4 @@ public void Appraise(IEmployeeEvaluation employeeEvaluation)
1922
employee.Name,
2023
employee.VacationDays);
2124
}
22-
}
25+
}

0 commit comments

Comments
 (0)