예제 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("----------Memento Pattern----------");
            #region Memento Pattern
            Person person = new Person();
            person.Name    = "Mehmet KONUK";
            person.Phone   = "+48-535575891";
            person.Address = "Warsaw";

            MemoryStore memory = new MemoryStore();
            memory.Memento = person.SaveMemento();

            person.Name    = "Deli Bekir";
            person.Phone   = "+48-123123123";
            person.Address = "warsaw";


            person.RestoreMemento(memory.Memento);
            #endregion
            Console.WriteLine("----------Mediator Pattern----------");
            #region Mediator Pattern

            Chatroom chatroom = new Chatroom();

            Participant p1 = new Co_Participant("p1");
            Participant p2 = new Co_Participant("p2");
            Participant p3 = new Co_Participant("p3");
            Participant p4 = new Co_Participant("p4");
            Participant p5 = new NonCo_Participant("p5");

            chatroom.Register(p1);
            chatroom.Register(p2);
            chatroom.Register(p3);
            chatroom.Register(p4);
            chatroom.Register(p5);

            p5.Send("p4", "Hi hello!");
            p2.Send("p3", "Hi to all");
            p3.Send("p1", "I am here");
            p2.Send("p4", "this is ...");
            p4.Send("p5", "Lets chat");

            #endregion
            Console.WriteLine("----------Poll Pattern----------");
            #region Pool Pattern
            EmployeeFactory employeeFactory = new EmployeeFactory();

            Employee emp1 = employeeFactory.GetEmployee();
            Employee emp2 = employeeFactory.GetEmployee();
            Employee emp3 = employeeFactory.GetEmployee();
            Employee emp4 = employeeFactory.GetEmployee();

            Console.WriteLine("Employee 1 details :" + emp1.FirstName + " , " + emp1.LastName);
            Console.WriteLine("Employee 2 details :" + emp2.FirstName + " , " + emp2.LastName);
            Console.WriteLine("Employee 3 details :" + emp3.FirstName + " , " + emp3.LastName);
            Console.WriteLine("Employee 4 details :" + emp4.FirstName + " , " + emp4.LastName);
            #endregion
            Console.ReadKey();
        }
예제 #2
0
        public void GetEmployee_ContractTypeHourlySalaryEmployee_ReturnEmployeeHourly()
        {
            // Arrange
            var employeeRaw = new EmployeeRaw();

            employeeRaw.contractTypeName = "HourlySalaryEmployee";

            // Act
            var employee = _employeeFactory.GetEmployee(employeeRaw);

            // Assert
            Assert.IsInstanceOf(typeof(EmployeeHourly), employee);
        }
예제 #3
0
        public async Task <EmployeeDto> GetEmployee(int id)
        {
            var employees = await _employeeRepository.GetEmployees();

            var anualEmployee = employees.Where(x => x.Id == id).Select(y => _employeeFactory.GetEmployee(y)).ToList().FirstOrDefault();

            return(_mapper.Map <EmployeeDto>(anualEmployee));
        }
예제 #4
0
        public List <DTO.Employee> GetEmployees()
        {
            List <DTO.Employee> employees = new List <DTO.Employee>();

            foreach (var employee in DataBase.Employees)
            {
                employees.Add(EmployeeFactory.GetEmployee(employee));
            }

            return(employees);
        }
예제 #5
0
        public DTO.Employee GetEmployee(int document)
        {
            var employee = DataBase.Employees.SingleOrDefault(r => r.Document == document);

            if (employee != null)
            {
                return(EmployeeFactory.GetEmployee(employee));
            }

            return(null);
        }
        /// <summary>
        /// Get all the employees.
        /// </summary>
        /// <returns>List of the employees.</returns>
        public List <Employee> GetEmployees()
        {
            var externalEmployees = employeeRepository.GetEmployees();
            var employess         = new List <Employee>();

            foreach (var externalEmployee in externalEmployees)
            {
                employess.Add(EmployeeFactory.GetEmployee(externalEmployee));
            }

            return(employess);
        }
예제 #7
0
        /// <see cref="Employees.Core.Employee.IEmployeeRepository.GetEmployee(int)"
        public TO.Employee GetEmployee(int id)
        {
            //Get employees list from data source
            var employees = EmployeeDAO.GetEmployees();

            //Find employe by id
            var employee = employees.FirstOrDefault(e => e.Id.Equals(id));

            //Create a concrete employee by contract type
            var emp = employeeFactory.GetEmployee(employee.ContractTypeName);

            emp.Id               = employee.Id;
            emp.Name             = employee.Name;
            emp.ContractTypeName = employee.ContractTypeName;
            emp.RoleId           = employee.RoleId;
            emp.RoleName         = employee.RoleName;
            emp.RoleDescription  = employee.RoleDescription;
            emp.HourlySalary     = employee.HourlySalary;
            emp.MonthlySalary    = employee.MonthlySalary;

            return(emp);
        }
예제 #8
0
        public static void Main()
        {
            EmployeeFactory ef = new EmployeeFactory();

            Employee e1 = ef.GetEmployee();

            Console.WriteLine(e1.FirstName + " " + e1.LastName);

            Employee e2 = ef.GetEmployee();

            Console.WriteLine(e2.FirstName + " " + e2.LastName);

            Employee e3 = ef.GetEmployee();

            Console.WriteLine(e3.FirstName + " " + e3.LastName);

            Employee e4 = ef.GetEmployee();

            Console.WriteLine(e4.FirstName + " " + e4.LastName);

            Console.ReadLine();
        }
예제 #9
0
        private Employee MapEmployee(JToken employee)
        {
            string   contractTypeName = employee["contractTypeName"].ToString();
            Employee concreteEmployee = _factory.GetEmployee(contractTypeName);

            concreteEmployee.Id               = employee["id"].ToObject <int>();
            concreteEmployee.Name             = employee["name"].ToString();
            concreteEmployee.RoleName         = employee["roleName"].ToString();
            concreteEmployee.MonthlySalary    = employee["monthlySalary"].ToObject <int>();
            concreteEmployee.RoleId           = employee["roleId"].ToObject <int>();
            concreteEmployee.MonthlySalary    = employee["monthlySalary"].ToObject <int>();
            concreteEmployee.HourlySalary     = employee["hourlySalary"].ToObject <int>();
            concreteEmployee.ContractTypeName = contractTypeName;
            return(concreteEmployee);
        }
예제 #10
0
        public EmployeeModel ToModel(EmployeeEntity employee)
        {
            var orderMapper = new OrderMapper();

            var factory           = new EmployeeFactory();
            var convertedEmployee = factory.GetEmployee(employee.Speciality);

            convertedEmployee.Id        = employee.Id;
            convertedEmployee.Age       = employee.Age;
            convertedEmployee.Firstname = employee.Firstname;
            convertedEmployee.Lastname  = employee.Lastname;
            convertedEmployee.Orders    = employee.Orders
                                          .Select(orderEntity => orderMapper.ToModel(orderEntity))
                                          .ToList();

            return(convertedEmployee);
        }
예제 #11
0
        public void CreateMontlyContractEmployeeDto()
        {
            var monthlySalary = 20;
            var myEmployeeDto = new EmployeeDto()
            {
                id               = "1",
                name             = "Diego",
                contractTypeName = "HourlySalaryEmployee",
                monthlySalary    = monthlySalary,
                hourlySalary     = 0
            };
            var myFactoryEmployeeDto = EmployeeFactory.GetEmployee(myEmployeeDto);

            Assert.IsNotNull(myFactoryEmployeeDto);
            Assert.AreEqual(myFactoryEmployeeDto, typeof(MonthlyContractEmployeeDto));
            Assert.IsNotNull(myFactoryEmployeeDto.annualSalary);
            Assert.AreEqual(myFactoryEmployeeDto.annualSalary, monthlySalary * 12);
        }
예제 #12
0
        public static List <EmployeeDto> GetEmployees()
        {
            var list = new List <EmployeeDto>();

            using (
                var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Add("User-Agent", userAgent);
                var response = httpClient.GetStringAsync(url).Result;
                list = JArray.Parse(response).ToObject <List <EmployeeDto> >();
                foreach (var employee in list)
                {
                    employee.annualSalary = EmployeeFactory.GetEmployee(employee).annualSalary;
                }
            }

            return(list);
        }
예제 #13
0
        static void Main(string[] args)
        {
            EmployeeFactory employeeFactory = new EmployeeFactory();
            IEmployee       employee;
            string          option, selectedType = "";

            while (true)
            {
                PrintMenu();

                option = Console.ReadLine();

                switch (option)
                {
                case "1":
                    selectedType = "Permanent";
                    break;

                case "2":
                    selectedType = "Probation";
                    break;

                case "3":
                    selectedType = "Contract";
                    break;

                default:
                    return;
                }

                employee = employeeFactory.GetEmployee(selectedType);

                if (employee != null)
                {
                    Console.WriteLine($"\n{selectedType} employees get:");
                    Console.WriteLine($"{employee.SickLeave()} sick leave days,");
                    Console.WriteLine($"{employee.PaidLeave()} paid leave days, and");
                    Console.WriteLine($"{employee.PublicHolidays()} public holidays.");
                }
            }
        }
예제 #14
0
        /// <summary>
        /// Get the employee.
        /// </summary>
        /// <param name="id">Id of the employee to get.</param>
        /// <returns>An employee.</returns>
        public Employee GetEmployee(int id)
        {
            var externalEmployee = employeeRepository.GetEmployees().FirstOrDefault(e => e.Id == id);

            return(EmployeeFactory.GetEmployee(externalEmployee));
        }
예제 #15
0
        static void Main(string[] args)
        {
            Console.WriteLine("Enter 1 for Factory Pattern. \n" +
                              "Enter 2 for Observer Pattern.");
            int choice = Convert.ToInt32(Console.ReadLine());

            switch (choice)
            {
            case 1:
                var ef = new EmployeeFactory();
                Console.WriteLine(ef.GetEmployee(1).GetBonus());
                Console.ReadLine();
                break;

            case 2:
                var observable = new TemperatureMonitor();
                var observer   = new TemperatureReporter();
                observer.Subscribe(observable);
                observable.GetTemperature();
                break;

            case 3:
                var editor  = new Editor();
                var history = new Momento.History();

                editor.SetContent("a");
                history.Push(editor.CreateState());
                editor.SetContent("b");
                history.Push(editor.CreateState());
                editor.SetContent("c");
                history.Push(editor.CreateState());
                editor.Restore(history.Pop());
                editor.Restore(history.Pop());

                Console.WriteLine(editor.GetContent());
                break;

            case 4:

                Canvas canvas = new Canvas();
                canvas.SelectTool(new BrushTool());
                canvas.MouseDown();
                canvas.MouseUp();
                break;

            case 5:

                BrowseHistory browseHistory = new BrowseHistory();
                browseHistory.Push("www.google.com");
                browseHistory.Push("www.yahoo.com");
                browseHistory.Push("www.reddif.com");
                browseHistory.Push("www.youtube.com");

                IIterator <string> iterator = browseHistory.CreateIterator();
                while (iterator.HasNext())
                {
                    var url = iterator.Current();
                    Console.WriteLine(url);
                    iterator.next();
                }
                break;

            case 6:
                //The difference between State and Strategy pattern is that in state pattern there is only a single state of the object and the behaviour is determined by the implementation injected.
                //In strategy pattern there could be multiple behaviours in form of multiple properties inside class such as IFilter & ICompression. The implementation injected further changes the behaviour.
                PhotoProcessor photoProcessor = new PhotoProcessor(new BnW(), new JPEG());
                photoProcessor.ProcessPhoto();
                break;

            case 7:     //template
                AbstractPreFlightCheckList flightChecklist = new F16PreFlightCheckList();
                flightChecklist.runChecklist();

                break;

            case 8:     //command
                var service = new CustomerService();
                var command = new AddCustomerCommand(service);
                var button  = new Command.Button(command);
                button.click();

                var composite = new CompositeCommand();
                composite.Add(new ResizeCommand());
                composite.Add(new BlackAndWHiteCommand());
                var button2 = new Command.Button(composite);
                button2.click();

                var commandHisotry = new Command.Undo.History();

                var doc = new Command.Undo.HtmlDocument();
                doc.SetContent("Hello World");
                var boldCommand = new BoldCommand(doc, commandHisotry);
                boldCommand.Execute();
                Console.WriteLine(doc.GetContent());

                var undoCommand = new UndoCommand(commandHisotry);
                undoCommand.Execute();
                Console.WriteLine(doc.GetContent());

                break;

            case 9:     //Observer
                DataSource dataSource = new DataSource();
                dataSource.AddObserver(new Chart());
                dataSource.AddObserver(new SpreadSheet(dataSource));
                dataSource.SetValue("value changed");
                break;

            case 10:     //Mediator //the pattern is applied to encapsulate or centralize the interactions amongst a number of objects
                var dialog = new ArticlesDialogBox();
                dialog.SimulateUsserInteraction();
                break;

            case 11:     //Chain of Responsibility
                //autehnticator --> logger --> compressor --> null
                var compressor    = new Compressor(null);
                var logger        = new Logger(compressor);
                var authenticator = new Authenticator(logger);
                var server        = new WebServer(authenticator);
                server.handle(new HttpRequest()
                {
                    UserName = "******", Password = "******"
                });
                break;

            case 12:     //Visitor
                var document = new Visitor.HtmlDocument();
                document.Add(new HeadingNode());
                document.Add(new AnchorNode());
                document.Execute(new HighlighOperation());
                break;

            case 13:     // Composite
                var shape1 = new Shape();
                var shape2 = new Shape();
                var group1 = new Group();
                group1.Add(shape1);
                group1.Add(shape2);
                var group2 = new Group();
                var shape3 = new Shape();
                group2.Add(shape3);
                group1.Add(group2);
                group1.render();
                break;

            case 14:     //Adapter
                Image       image       = new Image();
                ImageViewer imageViewer = new ImageViewer(image);
                imageViewer.Apply(new SepiaFilter());
                imageViewer.Apply(new FancyAdapter(new FancyFilter()));
                break;

            case 15:     //Decorator
                var cloudStream  = new CloudStream();
                var encryptData  = new EncryptStream(cloudStream);
                var compressData = new CompressStream(encryptData);
                compressData.write("some random data");
                break;

            case 16:     //Facade
                NotificationService notificationService = new NotificationService();
                notificationService.Send("Hello..", "17.0.0.1");
                break;

            case 17:     //Flyweight
                PointService pointService = new PointService(new PointFactory());
                var          points       = pointService.getPoints();
                foreach (var p in points)
                {
                    p.draw();
                }
                break;

            case 18:     //Bridge
                AdvancedRemoteControl remote = new AdvancedRemoteControl(new SonyTv());
                remote.setChannel(1);
                break;

            case 19:     //Proxy
                Library       lib       = new Library();
                List <string> bookNames = new List <string>()
                {
                    "a", "b", "c"
                };
                foreach (var book in bookNames)
                {
                    lib.eBooks.Add(book, new EBookProxy(book));
                }
                lib.OpenEbook("a");
                break;

            case 20:     //Factory Method
                FactoryMethod.Employee emp          = new FactoryMethod.Employee();
                BaseEmployeeFactory    permanentEmp = new PermanentEmployeeFactory(emp);
                permanentEmp.ApplySalary();
                Console.WriteLine(emp.HouseAllowance);
                break;

            case 21:     //Abstract Factory
                AbstractFactory.Employee emp1 = new AbstractFactory.Employee();
                emp1.EmployeeTypeID = 1;
                emp1.JobDescription = "Manager";
                EmployeeSystemFactory esf = new EmployeeSystemFactory();
                var computerFactory       = esf.Create(emp1);
                Console.WriteLine($"{computerFactory.GetType()}, {computerFactory.Processor()}, {computerFactory.SystemType()}, {computerFactory.Brand()}");
                break;

            case 22:     //Builder
                Builder.IToyBuilder toyBuilder  = new Builder.PremiumToyBuilder();
                Builder.ToyDirector toyDirector = new Builder.ToyDirector(toyBuilder);
                toyDirector.CreateFullFledgedToy();
                Console.WriteLine(toyDirector.GetToy());
                break;

            case 23:     //Fluent Builder
                //Difference of implementation is visible in Director class.
                FluentBuilder.IToyBuilder toyBuilder1  = new FluentBuilder.PremiumToyBuilder();
                FluentBuilder.ToyDirector toyDirector1 = new FluentBuilder.ToyDirector(toyBuilder1);
                toyDirector1.CreateFullFledgedToy();
                Console.WriteLine(toyDirector1.GetToy());
                break;

            case 24:    //Object Pool
                ObjectPool <OneExpensiveObjToCreate> objPool = new ObjectPool <OneExpensiveObjToCreate>();
                OneExpensiveObjToCreate obj = objPool.Get();
                objPool.Release(obj);
                break;
            }

            Console.ReadLine();
        }