public async Task <IActionResult> Create([Bind("Id,Name,JobDescription,Number,Department,HourlyPay,Bonus,EmployeeTypeId")] EmployeeSystemDesignPatterns.Models.EmployeeModels.Employee employee) { if (ModelState.IsValid) { //For Simple Factory //EmployeeManagerFactory empFactory = new EmployeeManagerFactory(); //var empManager = empFactory.createFactory(employee.EmployeeTypeId); //employee.Bonus = empManager.getBonus(); //employee.HourlyPay = empManager.getPay(); //For Factory Method Design Pattern EmployeeManagerFactory empFactory = new EmployeeManagerFactory(); var baseEmployeeFactory = empFactory.createEmployeeTypeFactory(employee); baseEmployeeFactory.ComputeAllowances(); //For Abstract Factory Method Pattern EmployeeSystemFactory employeeSystemFactory = new EmployeeSystemFactory(); var createdFactory = employeeSystemFactory.Create(employee); EmployeeSystemManager manager = new EmployeeSystemManager(createdFactory); employee.ComputerDetails = manager.getSystemDetails(); _context.Add(employee); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } // ViewBag.EmployeeTypeID = new SelectList(_context.EmployeeTypes, "EmployeeTypesId", "EmpType", employee.EmployeeTypeId); return(View(employee)); //return View(employee); }
static void Main(string[] args) { //Create the employee object. Employee employee = new Employee() { Name = "Sanjay", JobDescription = "IT Professional", Department = "IT", EmployeeTypeID = 1 }; //Create the abstract factory object. IComputerFactory factory = new EmployeeSystemFactory().Create(employee); //use the abstract factory object while creating the client and then //get the system details. EmployeeSystemManager manager = new EmployeeSystemManager(factory); employee.ComputerDetails = manager.GetSystemDetails(); //store the employee into the database. using (DesignPatternDBContext context = new DesignPatternDBContext()) { context.Employees.Add(employee); context.SaveChanges(); } Console.WriteLine("Employee inserted successfully."); Console.ReadKey(); }
public ActionResult Create([Bind(Include = "Id,Name,JobDesc,Number,Department,HourlyPay,Bonus,EmployeeTypeId")] Employee employee) { if (ModelState.IsValid) { //EmployeeManagerFactory empFactory = new EmployeeManagerFactory(); //IEmployeeManager empManager = empFactory.GetEmployeeManager(employee.EmployeeTypeId); //if (empManager != null) //{ // employee.Bonus = empManager.GetBonus(); // employee.HourlyPay = empManager.GetHourlyPay(); //} BaseEmployeeFactory employeeFactory = new EmployeeManagerFactory().CreateFactory(employee); employeeFactory.ApplySalary(); IComputerFactory computerFactory = new EmployeeSystemFactory().Create(employee); EmployeeSystemManager manager = new EmployeeSystemManager(computerFactory); employee.ComputerDetails = manager.GetSystemDetails(); db.Employees.Add(employee); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.EmployeeTypeId = new SelectList(db.EmployeeTypes, "Id", "Types", employee.EmployeeTypeId); return(View(employee)); }
static void Main(string[] args) { // Create the Employee Object Employee employee = new Employee() { Name = "Pranaya", JobDescription = "Manager", Department = "HR", EmployeeTypeID = 1 }; // Create the abstract factory object IComputerFactory factory = new EmployeeSystemFactory().Create(employee); // use the abstract factory object while creating the client and // then get the system details EmployeeSystemManager manager = new EmployeeSystemManager(factory); employee.ComputerDetails = manager.GetSystemDetails(); // Store the Employee into the database using (DesignPatternDBEntities dBEntities = new DesignPatternDBEntities()) { dBEntities.Employees.Add(employee); dBEntities.SaveChanges(); } Console.WriteLine("Employee data inserted"); Console.ReadKey(); }
public ActionResult Create([Bind(Include = "Id,Name,JobDescription,Number,Department,HourlyPay,Bonus,EmployeeTypeId")] Employee employee) { if (ModelState.IsValid) { #region SimpleFactory //EmployeeManagerFactory empFactory = new EmployeeManagerFactory(); //IEmployeeManager empManager = empFactory.GetEmployeeManager(employee.EmployeeTypeId); //employee.HourlyPay = empManager.GetPay(); //employee.Bonus = empManager.GetBonus(); #endregion BaseEmployeeFactory empFactory = new EmployeeManagerFactory().CreateFactory(employee); empFactory.ApplySalary(); //abstract factory IComputerFactory factory = new EmployeeSystemFactory().Create(employee); EmployeeSystemManager manager = new EmployeeSystemManager(factory); employee.ComputerDetails = manager.GetSystemDetails(); db.Employees.Add(employee); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.EmployeeTypeId = new SelectList(db.Employee_Type, "Id", "EmployeeType", employee.EmployeeTypeId); return(View(employee)); }
public async Task <IActionResult> Edit(int id, Employee employee) { if (id != employee.Id) { return(NotFound()); } if (ModelState.IsValid) { try { IComputerFactory factory = new EmployeeSystemFactory().Create(employee); EmployeeSystemManager manager = new EmployeeSystemManager(factory); employee.ComputerDetails = manager.GetSystemDetails(); db.Update(employee); await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(employee.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["DepartmentId"] = new SelectList(db.Departments, "Id", "DepartmentName", employee.DepartmentId); ViewData["EmployeeTypeId"] = new SelectList(db.EmployeeTypes, "Id", "EmployeeTypeName", employee.EmployeeTypeId); return(View(employee)); }
static void Main(string[] args) { #region factory method pattern //contract employee var employee = new FactoryMethod.Employee { //EmployeeTypeID = 1, EmployeeTypeID = 2, Bonus = 100, HourlyPay = 40, HouseAllowance = 40, MedicalAllowance = 30 }; BaseEmployeeFactory empFactory = new EmployeeManagerFactory().CreateFactory(employee); empFactory.ApplySalary(); Console.WriteLine($"Employee: {employee.EmployeeTypeID}{Environment.NewLine} " + $"House Allowance: {employee.HouseAllowance}{Environment.NewLine} " + $"Medical Allowance: {employee.MedicalAllowance}"); #endregion #region Abstract Factory Method var abstractFactoryEmployee = new Employee { }; IComputerFactory factory = new EmployeeSystemFactory().Create(abstractFactoryEmployee); EmployeeSystemManager manager = new EmployeeSystemManager(factory); abstractFactoryEmployee.ComputerDetails = manager.GetSystemDetails(); Console.WriteLine($"Computer Details: {abstractFactoryEmployee.ComputerDetails}{Environment.NewLine} " + $"Job Description: {abstractFactoryEmployee.JobDescription}"); #endregion #region Singleton Pattern /* * Assuming Singleton is created from employee class * we refer to the GetInstance property from the Singleton class */ DesignPatterns.Singleton.Singleton fromEmployee = DesignPatterns.Singleton.Singleton.GetInstance; fromEmployee.PrintDetails("From Employee"); /* * Assuming Singleton is created from student class * we refer to the GetInstance property from the Singleton class */ DesignPatterns.Singleton.Singleton fromStudent = DesignPatterns.Singleton.Singleton.GetInstance; fromStudent.PrintDetails("From Student"); #endregion #region Decorator Pattern ICar car = new Suzuki(); CarDecorator decorator = new OfferPrice(car); Console.WriteLine(string.Format("Make :{0} Price:{1} " + "DiscountPrice : {2}" , decorator.Make, decorator.GetPrice().ToString(), decorator.GetDiscountedPrice().ToString())); #endregion Console.ReadLine(); }
public void GetEmployeeAbstractFactoryData(Employee emp) { BaseAbstractFactory a = new FactoryMethod().CreateFactory(emp); a.ApplySalary(); IComputerFactory computerFactory = new EmployeeSystemFactory().CreateFactory(emp); EmployeeSystemManager employeeSystemManager = new EmployeeSystemManager(computerFactory); emp.ComputerDetails = employeeSystemManager.GetSystemDetails(); Console.WriteLine("BONUS : " + emp.Bonus.ToString() + " ;PAY " + emp.Salary.ToString() + " ;House " + emp.HouseAllowance.ToString() + " ;Medical " + emp.MedicalAllowance.ToString() + " ;ComputerDetails " + emp.ComputerDetails.ToString()); }
public async Task <IActionResult> Create([Bind("Id,Name,JobDescription,Number,Department,HourlyPay,Bonus,EmployeeTypeId,HouseAllowances,MedicalAllowances,ComputerDetails")] Employee employee) { if (ModelState.IsValid) { IComputerFactory computerFactory = new EmployeeSystemFactory().Create(employee); EmployeeSystemManager employeeSystemManager = new EmployeeSystemManager(computerFactory); employee.ComputerDetails = employeeSystemManager.GetSystemDetails(); _context.Add(employee); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["EmployeeTypeId"] = new SelectList(_context.EmployeeType, "Id", "EmployeeType1", employee.EmployeeTypeId); return(View(employee)); }
public ActionResult Edit([Bind(Include = "Id,Name,JobDescription,Number,Department,HourlyPay,Bonus,EmployeeTypeID,HouseAllowance,MedicalAllowance")] Employee employee) { if (ModelState.IsValid) { BaseEmployeeFactory empFactory = new EmployeeManagerFactory().CreateFactory(employee); empFactory.ApplySalary(); IComputerFactory factory = new EmployeeSystemFactory().Create(employee); EmployeeSystemManager manager = new EmployeeSystemManager(factory); employee.ComputerDetails = manager.GetSystemDetails(); db.Entry(employee).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(employee)); }
public async Task <IActionResult> Create(Employee employee) { if (ModelState.IsValid) { IComputerFactory factory = new EmployeeSystemFactory().Create(employee); EmployeeSystemManager manager = new EmployeeSystemManager(factory); employee.ComputerDetails = manager.GetSystemDetails(); db.Add(employee); await db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["DepartmentId"] = new SelectList(db.Departments, "Id", "DepartmentName", employee.DepartmentId); ViewData["EmployeeTypeId"] = new SelectList(db.EmployeeTypes, "Id", "EmployeeTypeName", employee.EmployeeTypeId); return(View(employee)); }
public ActionResult Create([Bind(Include = "Id,Name,JobDescription,Number,Department,HourlyPay,Bonus,EmployeeTypeID")] Employee employee) { if (ModelState.IsValid) { BaseEmployeeFactory empFactory = new Factory.FactoryMethod.EmployeeManagerFactory().CreateFactory(employee); empFactory.ApplySalary(); IComputerFactory factory = new EmployeeSystemFactory().Create(employee); EmployeeSystemManager manager = new EmployeeSystemManager(factory); employee.ComputerDetails = manager.GetSystemDetails(); db.Employees.Add(employee); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.EmployeeTypeID = new SelectList(db.Employee_Type, "Id", "EmployeeType", employee.EmployeeTypeID); return(View(employee)); }
public ActionResult Create([Bind(Include = "Id,Name,JobDescription,Number,Department,HourlyPay,Bonus,EmployeeTypeID")] Employee employee) { if (ModelState.IsValid) { IComputerFactory factory = new EmployeeSystemFactory().Create(employee); EmployeeSystemManager manager = new EmployeeSystemManager(factory); employee.ComputerDetails = manager.GetSystemDetails(); Factory.EmployeeManagerFactory empFactory = new Factory.EmployeeManagerFactory(); //Factory Method Pattern //BaseEmployeeFactory empfactory = new EmployeeManagerFactory().CreateFactory(employee); //empfactory.ApplySalary(); //Factory Pattern //Factory.EmployeeManagerFactory empFactory = new Factory.EmployeeManagerFactory(); //IEmployeeManager empManager = empFactory.GetEmployeeManager(employee.EmployeeTypeID); //employee.Bonus = empManager.GetBonus(); //employee.HourlyPay = empManager.GetPay(); //if(employee.EmployeeTypeID==1) //{ // employee.HourlyPay = 8; // employee.Bonus = 10; //} //else if(employee.EmployeeTypeID==2) //{ // employee.HourlyPay = 12; // employee.Bonus = 5; //} db.Employees.Add(employee); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.EmployeeTypeID = new SelectList(db.Employee_Type, "Id", "EmployeeType", employee.EmployeeTypeID); return(View(employee)); }
public void MainMethodFactory() { //empTypep as Parmanent Employee or Contract Employee - 1,2 #region Simple factory design ////->The Object needs to be extended to subclasses ////->The Classes doesn't know what exact sub-classes it has to create ////->The Product implementation tend to change over time and the client remains unchanged //int empType = 1; //EmployeeManagerFactory empMgrFactory = new EmployeeManagerFactory(); //var objFactory = empMgrFactory.GetEmployeeManager(empType); //var bonus = objFactory.GetBonus(); //var pay = objFactory.GetPay(); //Console.WriteLine("Employee Type: {0} has bonus: {1} and pay: {2}", empType, bonus, pay); #endregion #region Factory Method //EmployeeModel emp = new EmployeeModel(); //emp.EmployeeId = 2; //BaseEmployeeFactory employeeFactory = new DesignPattern.FactoryDesign.FactoryMethod.EmployeeManagerFactory() // .CreateFactory(emp); //employeeFactory.ApplySalary(); //Console.WriteLine("Employee Type: {0} has bonus: {1} and pay: {2}", emp.EmployeeId, emp.Bonus, emp.Pay); #endregion #region Abstract Factory EmployeeModel emp = new EmployeeModel(); emp.EmployeeId = 1; emp.JobDescription = "Manger"; IComputerFactory factory = new EmployeeSystemFactory().Create(emp); EmployeeSystemManager manager = new EmployeeSystemManager(factory); var res = manager.GetSysteDetails(); Console.WriteLine(res); #endregion }
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(); }