Here's a silly example of a tree structure where every node does a different kind of an action, but exactly one node (any node) has to do some common work at the start and end of a project.
public abstract class Employee
{
public void StartProject()
{
AnnounceProjectToNewspapers();
DoActualWork();
PutProductOnMarket();
}
protected abstract void DoActualWork();
private void AnnounceProjectToNewspapers() { }
private void PutProductOnMarket() { }
}
public class Engineer : Employee
{
protected override void DoActualWork()
{
// Build things.
}
}
public class Salesman : Employee
{
protected override void DoActualWork()
{
// Design leaflets.
}
}
public class Manager : Employee
{
protected override void DoActualWork()
{
// Make gantt charts.
// Also delegate.
foreach (var subordinate in subordinates)
// ...but then compiler stops you.
subordinate.DoActualWork();
}
private List<Employee> subordinates;
}
The problem is, you can't call the protected method DoActualWork()
on a base class.
Two solutions I see are:
- Make
DoActualWork()
public. But this would allow anyone to call it withoutAnnounceProjectToNewspapers()
orPutProductOnmarket()
. - Make
DoActualWork()
internal. But this would prevent other assemblies from using the "management system".
Is there a standard work-around people use to avoid this limitation?