Esempio n. 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("***Composite Pattern Demo ***");
            //Prinipal of the college
            CompositeEmployee Principal = new CompositeEmployee("Dr.S.Som(Principal)", "Planning-Supervising-Managing");
            //The college has 2 Head of Departments-One from MAths, One from Computer Sc.
            CompositeEmployee hodMaths  = new CompositeEmployee("Mrs.S.Das(HOD-Maths)", "Maths");
            CompositeEmployee hodCompSc = new CompositeEmployee("Mr. V.Sarcar(HOD-CSE)", "Computer Sc.");

            //2 other teachers works in Mathematics department
            Employee mathTeacher1 = new Employee("Math Teacher-1", "Maths");
            Employee mathTeacher2 = new Employee("Math Teacher-2", "Maths");

            //3 other teachers works in Computer Sc. department
            Employee cseTeacher1 = new Employee("CSE Teacher-1", "Computer Sc.");
            Employee cseTeacher2 = new Employee("CSE Teacher-2", "Computer Sc.");
            Employee cseTeacher3 = new Employee("CSE Teacher-3", "Computer Sc.");

            //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);

            //Printing the leaf-nodes and branches in the same way.
            //i.e. in each case, we are calling PrintStructures() method
            Console.WriteLine("\n Testing the structure of a Principal object");
            //Prints the complete structure
            Principal.PrintStructures();

            Console.WriteLine("\n Testing the structure of a HOD object:");
            Console.WriteLine("Teachers working at Computer Science department:");
            //Prints the details of Computer Sc, department
            hodCompSc.PrintStructures();

            //Leaf node
            Console.WriteLine("\n Testing the structure of a leaf node:");
            mathTeacher1.PrintStructures();

            //Suppose, one computer teacher is leaving now from the organization.
            hodCompSc.Remove(cseTeacher2);
            Console.WriteLine("\n After CSE Teacher-2 resigned, the organization has following members:");
            Principal.PrintStructures();

            Console.ReadKey();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Composite Pattern");

            #region Mathematics department

            //2 lecturers work in Mathematics department
            Employee mathTeacher1 = new Employee()
            {
                Name        = "M. Joy",
                Dept        = "Mathematic",
                Designation = "Lecturer"
            };
            Employee mathTeacher2 = new Employee()
            {
                Name        = "M. Roony",
                Dept        = "Mathematic",
                Designation = "Lecturer"
            };

            //the college has a Head of Department in Mathematics
            CompositeEmployee hodMathematic = new CompositeEmployee()
            {
                Name        = "Mrs. S.Das",
                Dept        = "Maths",
                Designation = "HOD-Maths"
            };

            //lecturers of mathematics directly reports to HOD-Maths
            hodMathematic.AddEmployee(mathTeacher1);
            hodMathematic.AddEmployee(mathTeacher2);

            #endregion

            #region Computer Science departments

            //3 lecturers work in Computer Science department
            Employee csTeacher1 = new Employee()
            {
                Name        = "C. Sam",
                Dept        = "Computer Science",
                Designation = "Lecturer"
            };
            Employee csTeacher2 = new Employee()
            {
                Name        = "C. Jones",
                Dept        = "Computer Science",
                Designation = "Lecturer"
            };
            Employee csTeacher3 = new Employee()
            {
                Name        = "C. Marium",
                Dept        = "Computer Science",
                Designation = "Lecturer"
            };

            //the college has a Head of Department in Computer Science
            CompositeEmployee hodComputerScience = new CompositeEmployee()
            {
                Name        = "Mr. V.Sarcar",
                Dept        = "Computer Science",
                Designation = "HOD-CS"
            };

            //lecturers of Computer Science directly report to HOD-CS
            hodComputerScience.AddEmployee(csTeacher1);
            hodComputerScience.AddEmployee(csTeacher2);
            hodComputerScience.AddEmployee(csTeacher3);

            #endregion

            #region Top level management

            //the collage also has a Principal
            CompositeEmployee principal = new CompositeEmployee()
            {
                Name        = "Dr. S.Som",
                Dept        = "Planning-Supervising-Managing",
                Designation = "Principal"
            };

            //hods of Maths and CS directly report to Principal
            principal.AddEmployee(hodMathematic);
            principal.AddEmployee(hodComputerScience);

            #endregion

            //printing the leaf-nodes and branches in the same way
            //in each case we are calling DisplayDetails() method

            Console.WriteLine("Details of a Principal object is as follows:");
            //prints the complete structure
            principal.DisplayDetails();

            Console.WriteLine("Details of a HOD object is as follows:");
            //print details of Computer Science department
            hodComputerScience.DisplayDetails();

            //leaf node
            Console.WriteLine("Details of an individual employee (leaf node) is as follows:");
            mathTeacher1.DisplayDetails();

            //suppose, one Computer Science lecturer (C. Jones) is leaving now from the organization

            hodComputerScience.RemoveEmployee(csTeacher2);

            Console.WriteLine("After the resignation of C. Jones, the organization has the following members:");
            principal.DisplayDetails();

            Console.Read();
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("***Composite Pattern Demo. ***");

            #region Mathematics department
            //2 lecturers work in Mathematics department
            Employee mathTeacher1 = new Employee {
                Name = "M.Joy", Dept = "Mathematic", Designation = "Lecturer"
            };
            Employee mathTeacher2 = new Employee {
                Name = "M.Roony", Dept = "Mathematics", Designation = "Lecturer"
            };

            //The college has a Head of Department in Mathematics
            CompositeEmployee hodMaths = new CompositeEmployee {
                Name = "Mrs.S.Das", Dept = "Maths", Designation = "HOD-Maths"
            };

            //Lecturers of Mathematics directly reports to HOD-Maths
            hodMaths.AddEmployee(mathTeacher1);
            hodMaths.AddEmployee(mathTeacher2);
            #endregion

            #region Computer Science department
            //3 lecturers work in Computer Sc. department
            Employee cseTeacher1 = new Employee {
                Name = "C.Sam", Dept = "Computer Science", Designation = "Lecturer"
            };
            Employee cseTeacher2 = new Employee {
                Name = "C.Jones", Dept = "Computer Science.", Designation = "Lecturer"
            };
            Employee cseTeacher3 = new Employee {
                Name = "C.Marium", Dept = "Computer Science", Designation = "Lecturer"
            };

            //The college has a Head of Department in Computer science
            CompositeEmployee hodCompSc = new CompositeEmployee {
                Name = "Mr. V.Sarcar", Dept = "Computer Sc.", Designation = "HOD-Computer Sc."
            };

            //Lecturers of Computer Sc. directly reports to HOD-CSE
            hodCompSc.AddEmployee(cseTeacher1);
            hodCompSc.AddEmployee(cseTeacher2);
            hodCompSc.AddEmployee(cseTeacher3);
            #endregion

            #region Top level management
            //The college also has a Principal
            CompositeEmployee principal = new CompositeEmployee {
                Name = "Dr.S.Som", Dept = "Planning-Supervising-Managing", Designation = "Principal"
            };

            //Head of Departments's of Maths and Computer Science directly reports to Principal.
            principal.AddEmployee(hodMaths);
            principal.AddEmployee(hodCompSc);
            #endregion

            /*
             * Printing the leaf-nodes and branches in the same way.
             * i.e. in each case, we are calling DisplayDetails() method.
             */
            Console.WriteLine("\nDetails of a Principal object is as follows:");
            //Prints the complete structure
            principal.DisplayDetails();

            Console.WriteLine("\nDetails of a HOD object is as follows:");
            //Prints the details of Computer Science department
            hodCompSc.DisplayDetails();

            //Leaf node
            Console.WriteLine("\nDetails of an individual employee(leaf node) is as follows:");
            mathTeacher1.DisplayDetails();

            /*
             * Suppose, one Computer Science lecturer(C.Jones)
             * is leaving now from the organization.
             */
            hodCompSc.RemoveEmployee(cseTeacher2);
            Console.WriteLine("\nAfter the resignation of C.Jones, the organization has the following members:");
            principal.DisplayDetails();
            //Wait for user
            Console.ReadKey();
        }