public void Test()
        {
            CompositeEmployee CEO = new CompositeEmployee("John", "CEO");

            CompositeEmployee headSales     = new CompositeEmployee("Eve", "Head Sales");
            CompositeEmployee headMarketing = new CompositeEmployee("Tom", "Head Marketing");

            CEO.AddSubOrdinate(headSales);
            CEO.AddSubOrdinate(headMarketing);

            CompositeEmployee member1Sales = new CompositeEmployee("Richard", "Sales");
            CompositeEmployee member2Sales = new CompositeEmployee("Rob", "Sales");

            headSales.AddSubOrdinate(member1Sales);
            headSales.AddSubOrdinate(member2Sales);

            CompositeEmployee member1Marketing = new CompositeEmployee("Laura", "Marketing");
            CompositeEmployee member2Marketing = new CompositeEmployee("Bob", "Marketing");

            headMarketing.AddSubOrdinate(member1Marketing);
            headMarketing.AddSubOrdinate(member2Marketing);

            Console.WriteLine(CEO);
            foreach (var subOrdinate in CEO.SubOrdinates)
            {
                Console.WriteLine("---------" + subOrdinate.ToString());
                foreach (var subOrdinate2 in subOrdinate.SubOrdinates)
                {
                    Console.WriteLine("------------------" + subOrdinate2.ToString());
                }
            }
        }
Exemplo n.º 2
0
        public static void Run()
        {
            var composite = new CompositeEmployee("root", "root");

            composite.AddEmployee(new Employee("Juan", "Math"));
            composite.AddEmployee(new Employee("Jose", "Ciencia"));
            var item = new Employee("Carlos", "Math");

            composite.AddEmployee(item);

            item.PrintEstructura();
            composite.PrintEstructura();
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Visitor Pattern");

            #region Visitor pattern - simple

            NumberCollection numberCollection = new NumberCollection();
            //showing the current list
            numberCollection.DisplayList();

            IncrementNumberVisitor incrementNumberVisitor = new IncrementNumberVisitor();

            //visiting the list
            Console.WriteLine("IncrementNumberVisitor is about to visit the list:");
            numberCollection.Accept(incrementNumberVisitor);

            //showing the current list
            numberCollection.DisplayList();
            Console.WriteLine();
            Console.WriteLine();

            #endregion

            #region Visitor pattern - composite pattern

            #region Mathematics department

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

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

            //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",
                Experience  = 10.2
            };
            Employee csTeacher2 = new Employee()
            {
                Name        = "C. Jones",
                Dept        = "Computer Science",
                Designation = "Lecturer",
                Experience  = 13.5
            };
            Employee csTeacher3 = new Employee()
            {
                Name        = "C. Marium",
                Dept        = "Computer Science",
                Designation = "Lecturer",
                Experience  = 7.3
            };

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

            //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",
                Experience  = 21
            };

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

            List <IEmployee> participants = new List <IEmployee>();
            //for employees who directly reports to Principal

            foreach (IEmployee employee in principal.subordinateList)
            {
                participants.Add(employee);
            }

            //for employees who directly reports to HOD-Maths
            foreach (IEmployee employee in hodMathematic.subordinateList)
            {
                participants.Add(employee);
            }

            //for employees who directly reports to HOD-CS
            foreach (IEmployee employee in hodComputerScience.subordinateList)
            {
                participants.Add(employee);
            }

            Console.WriteLine("Visitor starts visiting our composite structure.");
            VisitorWithCompositePattern.IVisitor visitor = new PromotionCheckerVisitor();

            //principal is already holding the highest position
            //we are not checking if he is eligible or not

            foreach (IEmployee employee in participants)
            {
                employee.Accept(visitor);
            }

            #endregion

            Console.Read();
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            //Creational Patterns
            Console.WriteLine("Creational Patterns");
            #region Singleton
            Console.WriteLine("***Singleton Pattern Demo***\n");

            Console.WriteLine("Trying to create instance s1.");
            Singleton s1 = Singleton.Instance;

            Console.WriteLine("Trying to create instance s2.");
            Singleton s2 = Singleton.Instance;

            if (s1 == s2)
            {
                Console.WriteLine("Only one instance exists.");
            }
            else
            {
                Console.WriteLine("Different instances exists.");
            }
            #endregion

            Console.WriteLine("###########################");

            #region Prototype
            Console.WriteLine("***Prototype Pattern Demo***\n");
            //Base or Original
            BasicCar nano_base = new Nano("Green Nano")
            {
                Price = 100000
            };
            BasicCar ford_base = new Ford("FordYellow")
            {
                Price = 500000
            };
            BasicCar bc1;

            //Nano
            bc1       = nano_base.Clone();
            bc1.Price = nano_base.Price + BasicCar.SetPrice();
            Console.WriteLine($"Car is {bc1.ModelName}, and price is {bc1.Price}");

            //Ford
            bc1       = ford_base.Clone();
            bc1.Price = ford_base.Price + BasicCar.SetPrice();
            Console.WriteLine($"Car is {bc1.ModelName}, and price is {bc1.Price}");
            #endregion

            Console.WriteLine("###########################");

            #region Buuilder
            Console.WriteLine("***Builder Pattern Demo***");
            Director director = new Director();

            IBuilder b1 = new Car("Ford");
            IBuilder b2 = new MotorCycle("Honda");

            //Mking Car
            director.Construct(b1);
            Product p1 = b1.GetVehicle();
            p1.Show();

            //Making Motorcycle
            director.Construct(b2);
            Product p2 = b2.GetVehicle();
            p2.Show();
            #endregion

            Console.WriteLine("###########################");

            #region Factory Method Pattern
            Console.WriteLine("***Factory Pattern Demo***\n");

            // Creating a Tiger Factory
            AnimalFactory tigerFactory = new TigerFactory();
            // Creating a tiger using the Factory Method
            IAnimal aTiger = tigerFactory.CreateAnimal();
            aTiger.AboutMe();
            aTiger.Action();

            // Creating a DogFactory
            AnimalFactory dogFactory = new DogFactory();
            // Creating a dog using the Factory Method
            IAnimal aDog = dogFactory.CreateAnimal();
            aDog.AboutMe();
            aDog.Action();
            #endregion

            Console.WriteLine("###########################");

            #region Abstract Factory Pattern

            Console.WriteLine("***Abstract Factory Pattern Demo***\n");
            //Making a wild dog through WildAnimalFactory
            IAnimalFactory wildAnimalFactory = new WildAnimalFactory();
            IDog           wildDog           = wildAnimalFactory.GetDog();

            wildDog.Speak();
            wildDog.Action();
            //Making a wild tiger through WildAnimalFactory
            ITiger wildTiger = wildAnimalFactory.GetTiger();
            wildTiger.Speak();
            wildTiger.Action();
            Console.WriteLine("******************");
            //Making a pet dog through PetAnimalFactory
            IAnimalFactory petAnimalFactory = new PetAnimalFactory();
            IDog           petDog           = petAnimalFactory.GetDog();
            petDog.Speak();
            petDog.Action();
            //Making a pet tiger through PetAnimalFactory
            ITiger petTiger = petAnimalFactory.GetTiger();
            petTiger.Speak();
            petTiger.Action();
            #endregion

            Console.WriteLine("###########################");

            //Structural Patterns
            Console.WriteLine("Structural Patterns");

            #region Proxy Pattern
            Console.WriteLine("***Proxy Pattern Demo***");
            Proxy px = new Proxy();
            px.DoSomeWork();
            #endregion

            Console.WriteLine("###########################");

            #region Decorator Pattern
            Console.WriteLine("***Decorator Pattern Simulation***");
            ConcreteComponent cc = new ConcreteComponent();

            ConcreteDecoratorEx1 decorator1 = new ConcreteDecoratorEx1();
            decorator1.SetTheComponent(cc);
            decorator1.MakeHouse();

            ConcreteDecoratorEx2 decorator2 = new ConcreteDecoratorEx2();
            //Adding results from decorator1
            decorator2.SetTheComponent(decorator1);
            decorator2.MakeHouse();
            #endregion

            Console.WriteLine("###########################");

            #region Adapter Pattern

            Console.WriteLine("***Adapter Pattern Demo***\n");
            CalculatorAdapter cal = new CalculatorAdapter();
            Triangle          t   = new Triangle(20, 10);
            Console.WriteLine($"Area of Triangle is {cal.GetArea(t)} square unit");

            #endregion

            Console.WriteLine("###########################");

            #region Facade Pattern

            Console.WriteLine("***Facade Pattern Demo***");
            RobotFacade robotFacade1 = new RobotFacade(); // Creating Robots
            robotFacade1.ConstructMilanoRobot();

            RobotFacade robotFacade2 = new RobotFacade(); // Creating Robots
            robotFacade2.ConstructRobonautRobot();

            robotFacade1.DestroyMilanoRobot();            //Destroying Robots
            robotFacade2.DestroyRobonautRobot();
            #endregion

            Console.WriteLine("###########################");

            #region Flyweight Pattern
            Console.WriteLine("***Flyweight Pattern Demo***");
            RobotFactory myFactory = new RobotFactory();
            IRobot       shape     = myFactory.GetRobotFromFactory("Small");
            shape.Print();

            //Creating small robots
            for (int i = 0; i < 2; i++)
            {
                shape = myFactory.GetRobotFromFactory("Small");
                shape.Print();
            }

            int numOfDistinctRobots = myFactory.TotalObjectsCreated;
            Console.WriteLine($"Number of distinct robot objects is {numOfDistinctRobots}");

            //Creating large robots
            for (int i = 0; i < 5; i++)
            {
                shape = myFactory.GetRobotFromFactory("Large");
                shape.Print();
            }

            numOfDistinctRobots = myFactory.TotalObjectsCreated;
            Console.WriteLine($"Distinct robot objects created till now {numOfDistinctRobots}");
            #endregion

            Console.WriteLine("###########################");

            #region Composite Pattern
            #region Mathematics department
            Console.WriteLine("***Composite Pattern***");
            //2 Lecturers work in Mathematics department
            Employee mathLecturer1 = new Employee {
                Name = "M. Joy", Dept = "Mathematics", Designation = "Lecturer"
            };
            Employee mathLecturer2 = new Employee {
                Name = "M. Ronny", Dept = "Mathematics", Designation = "Lecturer"
            };

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

            //Lecturers of Mathematics directly perort to HOD-Maths
            hodMaths.AddEmpployee(mathLecturer1);
            hodMaths.AddEmpployee(mathLecturer2);
            #endregion

            #region Computer Science department
            //3 lecturers work in Computer Sc. department
            Employee cseLecturer1 = new Employee {
                Name = "C. Sam", Dept = "Computer Sciense", Designation = "Lecturer"
            };
            Employee cseLecturer2 = new Employee {
                Name = "C. Jones", Dept = "Computer Sciense", Designation = "Lecturer"
            };
            Employee cseLecturer3 = new Employee {
                Name = "C. Marium", Dept = "Computer Sciense", 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.AddEmpployee(cseLecturer1);
            hodCompSc.AddEmpployee(cseLecturer2);
            hodCompSc.AddEmpployee(cseLecturer3);
            #endregion

            #region Top level management
            //College Principial
            CompositeEmployee principal = new CompositeEmployee {
                Name = "Dr.S.Som", Dept = "Planning-Supervising-Managing", Designation = "Principal"
            };

            //Mead of Department of Math and Computer Sciense apply to Principal
            principal.AddEmpployee(hodMaths);
            principal.AddEmpployee(hodCompSc);
            #endregion

            Console.WriteLine("Details of a Principal are these: ");
            principal.DisplayDetails();

            Console.WriteLine("Details of HOD object are these: ");
            hodCompSc.DisplayDetails();

            Console.WriteLine("Details of individual employee are these: ");
            mathLecturer1.DisplayDetails();

            //Lecturer leaves
            hodCompSc.RemoveEmployee(cseLecturer2);
            Console.WriteLine("After resignstion there are only these leecturers");
            principal.DisplayDetails();

            #endregion
        }
Exemplo n.º 5
0
        // Sparar ner rapport
        protected void btnSendReport_Click(object sender, EventArgs e)
        {
            BeraknaTraktAv();

            setEID();
            countExpenses();
            CheckCar();
            //Skapar en ny rapport för sparning.
            CompositeReport rep = new CompositeReport();
            rep.REPID = repID;
            rep.EID = eid;
            rep.Expenses = totalExpenses;
            rep.MID = missionID;
            rep.Car = car;
            rep.Miles = km;

            //Rapportör
            CompositeEmployee emp = new CompositeEmployee();
            //ChefObjekt
            CompositeEmployee empChef = new CompositeEmployee();
            //Mission för att hämta manager.
            CompositeMission mission = new CompositeMission();
            //Chef
            int chefID = 0;

            using (var client = new Service1Client())
            {
                client.SaveReport(rep);

                SaveExpenses();
                SaveSubsistences();
                SaveDeviations();

                emp = client.GetEmployee(eid);

                mission = client.GetMission(missionID);

                chefID = mission.Manager;
                empChef = client.GetEmployee(chefID);
            }

            bool epostSkickat = Klasser.Epost.Skicka(empChef.Email, "Ny Rapport", "Du har en ny rapport att godkänna från: " + emp.FirstName + " " + emp.LastName);
            if (epostSkickat)
            {
            }
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            #region Singleton
            //Console.WriteLine("***Singleton Pattern Demo***");
            //Console.WriteLine("Trying to create instance s1.");
            //Singleton s1 = Singleton.Instance;
            //Singleton s2 = Singleton.Instance;
            //if (s1 == s2)
            //{
            //    Console.WriteLine("Only on instance exists.");

            //}
            //else
            //{
            //    Console.WriteLine("Different instances exist.");
            //}
            #endregion

            #region Prototype
            //Console.WriteLine("***Prototype Pattern Demo***\n");
            //BasicCar nano_base = new Nano("Green Nano") { Price = 100000 };
            //BasicCar ford_base = new Ford("Ford Yellow") { Price = 500000 };

            //BasicCar bc1;
            ////Nano
            //bc1 = nano_base.Clone();
            //bc1.Price = nano_base.Price + BasicCar.SetPrice();
            //Console.WriteLine($"Car is:{bc1.ModelName},and it's price is Rs.{bc1.Price}");
            ////Ford
            //bc1 = ford_base.Clone();
            //bc1.Price = ford_base.Price + BasicCar.SetPrice();
            //Console.WriteLine($"Car is:{bc1.ModelName},and it's price is Rs.{bc1.Price}");
            #endregion

            #region BuiderPattern
            //Console.WriteLine("***Builder Pattern Demo***\n");
            //Director director = new Director();
            //IBuilder b1 = new Car("Ford");

            //director.Construct(b1);
            //Product p1 = b1.GetVehicle();
            //p1.Show();
            #endregion

            #region FactoryMethodPattern
            Console.WriteLine("***Factory Pattern Demo***\n");

            //Creating a Tiger Factory
            DesignPattern.FactoryMethodPattern.IAnimalFactory tigerFactory = new TigerFactory();
            //Createing a tiger using the factory method
            IAnimal aTiger = tigerFactory.MakeAnimal();
            //aTiger.Speak();
            //aTiger.Action();

            FactoryMethodPattern.IAnimalFactory dogFactory = new DogFactory();
            IAnimal aDog = dogFactory.CreateAnimal();
            aDog.Speak();
            aDog.Action();
            #endregion

            #region Simple Factory
            //Console.WriteLine("*** Simple Factory Pattern Demo***\n");
            //IAnimal preferredType = null;
            //ISimpleFactory simpleFactory = new SimpleFactory();
            //preferredType = simpleFactory.CreateAnimal();
            //preferredType.Speak();
            //preferredType.Action();
            #endregion

            #region AbstractFactory Pattern
            Console.WriteLine("***Abstract Factory Pattern Demo***");
            DesignPattern.AbstractFactoryPattern.IAnimalFactory wildAnimalFactory = new WildAnimalFactory();
            IDog wildDog = wildAnimalFactory.GetDog();
            wildDog.Speak();
            wildDog.Action();
            ITiger wildTiger = wildAnimalFactory.GetTiger();
            wildTiger.Speak();
            wildTiger.Action();
            Console.WriteLine("below is create pet animal ");
            AbstractFactoryPattern.IAnimalFactory petAnimalFactory = new PetAnimalFactory();
            IDog petDog = petAnimalFactory.GetDog();
            petDog.Speak();
            petDog.Action();
            ITiger petTiger = petAnimalFactory.GetTiger();
            petTiger.Speak();
            petTiger.Action();
            #endregion

            #region Proxy Pattern
            Console.WriteLine("***Proxy Pattern Demo***\n");
            Proxy px = new Proxy();
            px.DoSomeWork();

            ICar car = new ProxyCar(new Driver(15));
            car.DriveCar();
            car = new ProxyCar(new Driver(25));
            car.DriveCar();
            #endregion

            Console.WriteLine("***Decorator pattern Demo...\n");
            ConcreteComponent cc = new ConcreteComponent();


            ConcreteDecoratorEx1 decoratorEx1 = new ConcreteDecoratorEx1(cc);
            //  decoratorEx1.SetTheComponent(cc);
            decoratorEx1.MakeHouse();

            #region Adapter Pattern
            CalculatorAdapter cal = new CalculatorAdapter();
            Triangle          t   = new Triangle(20, 10);
            Console.WriteLine($"Area of Triangle is:{cal.GetArea(t)} Square unit");


            Rect r = new Rect(20, 10);
            Console.WriteLine($"Area of Rectangle is :{r.CalculateAreaOfRectangle()}");
            Triangle t0 = new Triangle(20, 10);
            Console.WriteLine($"Area of Triangle is :{t0.CalculateAreaOfTriangle()} Square unit.");
            RectInterface adapter = new TriangleAdapter(t0);
            Console.WriteLine($"Area of Triangle using the triangle adapter is :{adapter.CalculateAreaOfRectangle()} square unit");
            #endregion

            Console.WriteLine("***Flyweight Pattern Demo***");
            RobotFactory factory = new RobotFactory();
            IRobot       shape   = factory.GetRobotFromFactory("Small");
            shape.Print();
            for (int i = 0; i < 2; i++)
            {
                shape = factory.GetRobotFromFactory("Small");
                shape.Print();
            }
            int NumOfDistinctRobots = factory.TotalObjectsCreated;
            Console.WriteLine($"Now,there has {NumOfDistinctRobots} Robots.");

            Console.WriteLine("***Composite Pattern Demo***");
            CompositeEmployee Principal = new CompositeEmployee("Derily(Principal)", "Planning-Supervising-Managing");
            CompositeEmployee hodMaths  = new CompositeEmployee("Mrs.S.Das(HOD-Maths)", "Maths");
            CompositeEmployee hodCompSc = new CompositeEmployee("Mr. V.Sarcar(HOD-CSE)", "Computer Sc.");

            Employee mathTeacher1 = new Employee("Math Teacher-1", "Maths");
            Employee mathTeacher2 = new Employee("Math Teacher-2", "Maths");

            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.");

            hodMaths.Add(mathTeacher1);
            hodMaths.Add(mathTeacher2);

            hodCompSc.Add(cseTeacher1);
            hodCompSc.Add(cseTeacher2);
            hodCompSc.Add(cseTeacher3);

            Principal.Add(hodMaths);
            Principal.Add(hodCompSc);

            Console.WriteLine("\n Testing the structure of a Principal object");
            Principal.PrintStructures();

            Console.Read();
        }