public void VisitCompositeElement(CompositeEmployee employee) { //We'll promote them if experience is greater than 15 years bool eligibleForPromotion = employee.Experience > 15 ? true : false; Console.WriteLine("\t\t" + employee.Name + " from " + employee.Dept + " is eligible for promotion? " + eligibleForPromotion); }
static void Main(string[] args) { Console.WriteLine("***Visitor Pattern combined with Composite Pattern Demo***\n"); #region Similar code structure taken from Composite Pattern demo //Prinipal of the college CompositeEmployee Principal = new CompositeEmployee("Dr.S.Som(Principal)", "Planning-Supervising-Managing", 20); //The college has 2 Head of Departments-One from MAths, One from Computer Sc. CompositeEmployee hodMaths = new CompositeEmployee("Mrs.S.Das(HOD-Maths)", "Maths", 14); CompositeEmployee hodCompSc = new CompositeEmployee("Mr. V.Sarcar(HOD-CSE)", "Computer Sc.", 16); //2 other teachers works in Mathematics department Employee mathTeacher1 = new Employee("Math Teacher-1", "Maths", 14); Employee mathTeacher2 = new Employee("Math Teacher-2", "Maths", 6); //3 other teachers works in Computer Sc. department Employee cseTeacher1 = new Employee("CSE Teacher-1", "Computer Sc.", 10); Employee cseTeacher2 = new Employee("CSE Teacher-2", "Computer Sc.", 13); Employee cseTeacher3 = new Employee("CSE Teacher-3", "Computer Sc.", 7); //Teachers of Mathematics directly reports to HOD-Maths hodMaths.Add(mathTeacher1); hodMaths.Add(mathTeacher2); //Teachers of Computer Sc directly reports to HOD-Comp.Sc hodCompSc.Add(cseTeacher1); hodCompSc.Add(cseTeacher2); hodCompSc.Add(cseTeacher3); //Principal is on top of college //HOD -Maths and Comp. Sc directly reports to him Principal.Add(hodMaths); Principal.Add(hodCompSc); Console.WriteLine("\n Testing the overall structure"); //Prints the complete structure Principal.PrintStructures(); #endregion Console.WriteLine("\n***Visitor starts visiting our composite structure***\n"); IVisitor aVisitor = new Visitor(); /*Principal is already holding the highest position. * We are not checking whether he is eligible for promotion or not*/ //Principal.Accept(aVisitor); //For employees who directly reports to Principal foreach (IEmployee e in Principal.Controls) { e.Accept(aVisitor); } //For employees who directly reports to HOD-Maths foreach (IEmployee e in hodMaths.Controls) { e.Accept(aVisitor); } //For employees who directly reports to HOD-Comp.Sc foreach (IEmployee e in hodCompSc.Controls) { e.Accept(aVisitor); } Console.ReadLine(); }