static void Main(string[] args) { ShapeObjectFactory sof = new ShapeObjectFactory(); IShape shape = sof.GetShape("Triangle"); shape.Print(); shape = sof.GetShape("Triangle"); shape.Print(); shape = sof.GetShape("Triangle"); shape.Print(); shape = sof.GetShape("Triangle"); shape.Print(); shape = sof.GetShape("Square"); shape.Print(); shape = sof.GetShape("Square"); shape.Print(); shape = sof.GetShape("Square"); shape.Print(); shape = sof.GetShape("Square"); shape.Print(); shape = sof.GetShape("Square"); shape.Print(); int totalObjectCount = sof.GetTotalObjectsCreated; Console.WriteLine($"The total number of objects created are {totalObjectCount}."); Console.ReadKey(); }
public IShape GetFromName(string name) { ShapeObjectFactory factory = new ShapeObjectFactory(); IShape shape = factory.GetShape("Rectangle"); return(shape); }
static void Main(string[] args) { ShapeObjectFactory sof = new ShapeObjectFactory(); IShape shape = sof.GetShape("Rectangle"); shape.Print(); shape = sof.GetShape("Rectangle"); shape.Print(); shape = sof.GetShape("Rectangle"); shape.Print(); shape = sof.GetShape("Circle"); shape.Print(); shape = sof.GetShape("Circle"); shape.Print(); shape = sof.GetShape("Circle"); shape.Print(); int NumObjs = sof.TotalObjectsCreated; Console.WriteLine("\nTotal No of Objects created = {0}", NumObjs); Console.ReadKey(); }
static void Main() { //Respostas para questão 1 e 2 List <Empresa> Empresas = new List <Empresa>() { new Empresa { Id = 1, Nome = "Encripta" }, new Empresa { Id = 2, Nome = "Nao Encripta" }, }; List <Pessoa> Pessoas = new List <Pessoa>() { new Pessoa { Id = 2, Nome = "Bdriano", Sobrenome = "Daversan", DataNascimento = new DateTime(1976, 04, 02), Empresa = Empresas[1] }, new Pessoa { Id = 3, Nome = "Cdriano", Sobrenome = "Vaversan", DataNascimento = new DateTime(1976, 02, 02), Empresa = Empresas[1] }, new Pessoa { Id = 4, Nome = "Ddriano", Sobrenome = "Naversan", DataNascimento = new DateTime(1976, 01, 02), Empresa = Empresas[1] }, new Pessoa { Id = 1, Nome = "Adriano", Sobrenome = "Caversan", DataNascimento = new DateTime(1976, 07, 02), Empresa = Empresas[0] }, }; var orderedPesssoas = Pessoas.OrderByDescending(Pessoa => Pessoa.Sobrenome).ToList(); //var orderedPesssoas = (from Pessoa in Pessoas.AsParallel().AsOrdered() orderby Pessoa.Sobrenome descending select Pessoa); /* * foreach (var Pessoa in orderedPesssoas) * { * Console.WriteLine("{0} {1} {2} {3}", Pessoa.Nome, Pessoa.Sobrenome, String.Format("{0:dd/MM/yyyy}", Pessoa.DataNascimento), Pessoa.Empresa.Nome); * } */ Console.WriteLine("Questões 1 and 2"); Parallel.ForEach(orderedPesssoas, Pessoa => { Console.WriteLine("{0} {1} {2} {3}", Pessoa.Nome, Pessoa.Sobrenome, String.Format("{0:dd/MM/yyyy}", Pessoa.DataNascimento), Pessoa.Empresa.Nome); }); Console.WriteLine(""); //Questão 3 Console.WriteLine("Questão 3 Iterator"); var weeks = new Weeks(); var iterator = weeks.GetWeeksIterator(); while (iterator.MoveNext()) { Console.WriteLine(iterator.Current); } Console.WriteLine(""); Console.WriteLine("Questão 3 Flyweight"); //Flyweight ShapeObjectFactory sof = new ShapeObjectFactory(); IShape shape = sof.GetShape("Rectangle"); shape.Print(); shape = sof.GetShape("Rectangle"); shape.Print(); shape = sof.GetShape("Rectangle"); shape.Print(); shape = sof.GetShape("Circle"); shape.Print(); shape = sof.GetShape("Circle"); shape.Print(); shape = sof.GetShape("Circle"); shape.Print(); int NumObjs = sof.TotalObjectsCreated; Console.WriteLine("\nTotal No of Objects created = {0}", NumObjs); //Questão 10 Extension Methods Console.WriteLine(""); Console.WriteLine("Questão 4 Extension Methods"); // Import the extension method namespace. string s = "The quick brown fox jumped over the lazy dog."; // Call the method as if it were an // instance method on the type. Note that the first // parameter is not specified by the calling code. int i = s.WordCount(); System.Console.WriteLine("Word count of s is {0}", i); //Questão 10 Console.WriteLine(""); Console.WriteLine("Questão 10 Algoritimo fatorial"); int fatorial = 1; for (int n = 1; n <= 10; n++) { fatorial *= n; Console.WriteLine(n + " fatorial= " + fatorial); } }
private static void Main(string[] args) { #region AbstactFactoryDesignPattern /* Factory factory = new Factory(); * IVehicleFactory bike = factory.GetVehicle(EVehicleType.Bike); * bike.Drive(); * IVehicleFactory scooter = factory.GetVehicle(EVehicleType.Scooter); * scooter.Drive(); * IVehicleFactory car = factory.GetVehicle(EVehicleType.Car); * car.Drive(); * IVehicleFactory bus = factory.GetVehicle(EVehicleType.Bus); * bus.Drive(); * IVehicleFactory lorry = factory.GetVehicle(EVehicleType.Lorry); * lorry.Drive(); * IVehicleFactory train = factory.GetVehicle(EVehicleType.Train); * train.Drive(); * IVehicleFactory ship = factory.GetVehicle(EVehicleType.Ship); * ship.Drive(); * IVehicleFactory helicopter = factory.GetVehicle(EVehicleType.Helicopter); * helicopter.Drive(); */ #endregion AbstactFactoryDesignPattern #region Adapter ITarget Itarget = new EmployeeAdapter(); ThirdPartyBillingSystem client = new ThirdPartyBillingSystem(Itarget); client.ShowEmployeeList(); #endregion Adapter #region Bridge IMessageSender email = new EmailSender(); IMessageSender queue = new MSMQSender(); IMessageSender web = new WebServiceSender(); Message message = new SystemMessage(); message.Subject = "Test Message"; message.Body = "Hi, This is a Test Message"; message.MessageSender = email; message.Send(); message.MessageSender = queue; message.Send(); message.MessageSender = web; message.Send(); UserMessage usermsg = new UserMessage(); usermsg.Subject = "Test Message"; usermsg.Body = "Hi, This is a Test Message"; usermsg.UserComments = "I hope you are well"; usermsg.MessageSender = email; usermsg.Send(); #endregion Bridge #region Builder var vehicleCreator = new VehicleCreator(new HeroBuilder()); vehicleCreator.CreateVehicle(); var vehicle = vehicleCreator.GetVehicle(); vehicle.ShowInfo(); Console.WriteLine("---------------------------------------------"); vehicleCreator = new VehicleCreator(new HondaBuilder()); vehicleCreator.CreateVehicle(); vehicle = vehicleCreator.GetVehicle(); vehicle.ShowInfo(); #endregion Builder #region ChainOfResponsiblity Approver rohit = new Clerk(); Approver rahul = new AssistantManager(); Approver manoj = new Manager(); rohit.Successor = rahul; rahul.Successor = manoj; // Generate and process loan requests var loan = new Loan { Number = 2034, Amount = 24000.00, Purpose = "Laptop Loan" }; rohit.ProcessRequest(loan); loan = new Loan { Number = 2035, Amount = 42000.10, Purpose = "Bike Loan" }; rohit.ProcessRequest(loan); loan = new Loan { Number = 2036, Amount = 156200.00, Purpose = "House Loan" }; rohit.ProcessRequest(loan); #endregion ChainOfResponsiblity #region Command Console.WriteLine("Enter Commands (ON/OFF) : "); string cmd = Console.ReadLine(); Light lamp = new Light(); ICommand switchUp = new FlipUpCommand(lamp); ICommand switchDown = new FlipDownCommand(lamp); Switch s = new Switch(); if (cmd == "ON") { s.StoreAndExecute(switchUp); } else if (cmd == "OFF") { s.StoreAndExecute(switchDown); } else { Console.WriteLine("Command \"ON\" or \"OFF\" is required."); } #endregion Command #region Composite Employee Rahul = new Employee { EmpID = 1, Name = "Rahul" }; Employee Amit = new Employee { EmpID = 2, Name = "Amit" }; Employee Mohan = new Employee { EmpID = 3, Name = "Mohan" }; Rahul.AddSubordinate(Amit); Rahul.AddSubordinate(Mohan); Employee Rita = new Employee { EmpID = 4, Name = "Rita" }; Employee Hari = new Employee { EmpID = 5, Name = "Hari" }; Amit.AddSubordinate(Rita); Amit.AddSubordinate(Hari); Employee Kamal = new Employee { EmpID = 6, Name = "Kamal" }; Employee Raj = new Employee { EmpID = 7, Name = "Raj" }; Contractor Sam = new Contractor { EmpID = 8, Name = "Sam" }; Contractor tim = new Contractor { EmpID = 9, Name = "Tim" }; Mohan.AddSubordinate(Kamal); Mohan.AddSubordinate(Raj); Mohan.AddSubordinate(Sam); Mohan.AddSubordinate(tim); Console.WriteLine("EmpID={0}, Name={1}", Rahul.EmpID, Rahul.Name); foreach (Employee manager in Rahul) { Console.WriteLine("\n EmpID={0}, Name={1}", manager.EmpID, manager.Name); foreach (var employee in manager) { Console.WriteLine(" \t EmpID={0}, Name={1}", employee.EmpID, employee.Name); } } #endregion Composite #region Decorator HondaCity car = new HondaCity(); Console.WriteLine("Honda City base price are : {0}", car.Price); SpecialOffer offer = new SpecialOffer(car); offer.DiscountPercentage = 25; offer.Offer = "25 % discount"; Console.WriteLine("{1} @ Diwali Special Offer and price are : {0} ", offer.Price, offer.Offer); #endregion Decorator #region Facade CarFacade facade = new CarFacade(); facade.CreateCompleteCar(); #endregion Facade #region Flyweight ShapeObjectFactory sof = new ShapeObjectFactory(); IShape shape = sof.GetShape("Rectangle"); shape.Print(); shape = sof.GetShape("Rectangle"); shape.Print(); shape = sof.GetShape("Rectangle"); shape.Print(); shape = sof.GetShape("Circle"); shape.Print(); shape = sof.GetShape("Circle"); shape.Print(); shape = sof.GetShape("Circle"); shape.Print(); int NumObjs = sof.TotalObjectsCreated; Console.WriteLine("\nTotal No of Objects created = {0}", NumObjs); #endregion Flyweight #region FactoryDesignPattern Factory factory = new Factory(); FactoryDesignPattern.IVehicleFactory bike = factory.GetVehicle(EVehicleType.Bike); bike.Drive(); FactoryDesignPattern.IVehicleFactory scooter = factory.GetVehicle(EVehicleType.Scooter); scooter.Drive(); FactoryDesignPattern.IVehicleFactory fcar = factory.GetVehicle(EVehicleType.Car); fcar.Drive(); FactoryDesignPattern.IVehicleFactory bus = factory.GetVehicle(EVehicleType.Bus); bus.Drive(); FactoryDesignPattern.IVehicleFactory lorry = factory.GetVehicle(EVehicleType.Lorry); lorry.Drive(); FactoryDesignPattern.IVehicleFactory train = factory.GetVehicle(EVehicleType.Train); train.Drive(); FactoryDesignPattern.IVehicleFactory ship = factory.GetVehicle(EVehicleType.Ship); ship.Drive(); FactoryDesignPattern.IVehicleFactory helicopter = factory.GetVehicle(EVehicleType.Helicopter); helicopter.Drive(); #endregion FactoryDesignPattern #region Prototype Developer dev = new Developer(); dev.Name = "Rahul"; dev.Role = "Team Leader"; dev.PreferredLanguage = "C#"; Developer devCopy = (Developer)dev.Clone(); devCopy.Name = "Arif"; //Not mention Role and PreferredLanguage, it will copy above Console.WriteLine(dev.GetDetails()); Console.WriteLine(devCopy.GetDetails()); Typist typist = new Typist(); typist.Name = "Monu"; typist.Role = "Typist"; typist.WordsPerMinute = 120; Typist typistCopy = (Typist)typist.Clone(); typistCopy.Name = "Sahil"; typistCopy.WordsPerMinute = 115;//Not mention Role, it will copy above Console.WriteLine(typist.GetDetails()); Console.WriteLine(typistCopy.GetDetails()); #endregion Prototype #region Proxy ProxyClient proxy = new ProxyClient(); Console.WriteLine("Data from Proxy Client = {0}", proxy.GetData()); #endregion Proxy #region Singleton EagerSingleton.Instance.Show(); LazySingleton.Instance.Show(); Singleton.Instance.Show(); #endregion Singleton Console.ReadKey(); }
static void Main(string[] args) { //Behavioral Patterns Console.WriteLine("Behavioral"); // Wait for user Console.ReadKey(); //1 - Command Console.WriteLine("Command"); Console.WriteLine("Enter Commands (ON/OFF) : "); string cmd = Console.ReadLine(); Light lamp = new Light(); ICommand switchUp = new FlipUpCommand(lamp); ICommand switchDown = new FlipDownCommand(lamp); Switch s = new Switch(); if (cmd == "ON") { s.StoreAndExecute(switchUp); } else if (cmd == "OFF") { s.StoreAndExecute(switchDown); } else { Console.WriteLine("Command \"ON\" or \"OFF\" is required"); } // Wait for user Console.ReadKey(); //2 - Chain of responsability Console.WriteLine("Chain of responsability"); Approver rohit = new Clerk(); Approver rahul = new AssistantManager(); Approver manoj = new Manager(); rohit.Successor = rahul; rahul.Successor = manoj; var loan = new Loan { Number = 2034, Amount = 24000.00, Purpose = "Laptop Loan" }; rohit.ProcessRequest(loan); loan = new Loan { Number = 2035, Amount = 42000.10, Purpose = "Bike Loan" }; rohit.ProcessRequest(loan); loan = new Loan { Number = 2036, Amount = 156200.00, Purpose = "House Loan" }; rohit.ProcessRequest(loan); // Wait for user Console.ReadKey(); //3 - Memento Console.WriteLine("Memento"); SalesProspect sp = new SalesProspect(); sp.Name = "Noel van Halen"; sp.Phone = "(412) 256-0990"; sp.Budget = 25000.0; // Store internal state ProspectMemory m = new ProspectMemory(); m.Memento = sp.SaveMemento(); // Continue changing originator sp.Name = "Leo Welch"; sp.Phone = "(310) 209-7111"; sp.Budget = 1000000.0; // Restore saved state sp.RestoreMemento(m.Memento); // Wait for user Console.ReadKey(); //Creational Patterns Console.WriteLine("Creational"); // Wait for user Console.ReadKey(); //1-Singleton Console.WriteLine("Singleton"); Singleton.Instance.Show(); // Wait for user Console.ReadKey(); //2-Prototype Console.WriteLine("Prototype"); Developper dev = new Developper { Name = "Antoine", Role = "Team Leader", PrefferedLanguage = "C#", WordsPerMinute = 42 }; Typist typ = new Typist { Name = "Casper", Role = "Typist", WordsPerMinute = 175 }; Console.WriteLine(typ.GetDetails()); Console.WriteLine(dev.GetDetails()); // Wait for user Console.ReadKey(); //3-AbstractFactory Console.WriteLine("Abstract Factory"); DesignPatterns.Creational.AbstractFactory.Sample.VehiculeFactory honda = new HondaFactory(); VehiculeClient hondaclient = new VehiculeClient(honda, "Regular"); Console.WriteLine("****** Honda ******"); Console.WriteLine(hondaclient.GetBikeName()); Console.WriteLine(hondaclient.GetScooterName()); VehiculeClient hondaclient2 = new VehiculeClient(honda, "Sports"); Console.WriteLine(hondaclient.GetBikeName()); Console.WriteLine(hondaclient.GetScooterName()); DesignPatterns.Creational.AbstractFactory.Sample.VehiculeFactory hero = new HondaFactory(); VehiculeClient heroclient = new VehiculeClient(hero, "Regular"); Console.WriteLine("****** Hero ******"); Console.WriteLine(heroclient.GetBikeName()); Console.WriteLine(heroclient.GetScooterName()); VehiculeClient heroclient2 = new VehiculeClient(hero, "Sports"); Console.WriteLine(heroclient.GetBikeName()); Console.WriteLine(heroclient.GetScooterName()); // Wait for user Console.ReadKey(); //4- Factory Method Console.WriteLine("Factory Method"); DesignPatterns.Creational.FactoryMethod.Sample.VehiculeFactory factory = new ConcreteVehiculeFactory(); IFactory scooter = factory.GetVehicule("Scooter"); scooter.Drive(10); IFactory bike = factory.GetVehicule("Bike"); bike.Drive(20); // Wait for user Console.ReadKey(); //5- Builder Console.WriteLine("Builder"); var vehicleCreator = new VehicleCreator(new HeroBuilder()); vehicleCreator.CreateVehicle(); var vehicle = vehicleCreator.GetVehicle(); vehicle.ShowInfo(); Console.WriteLine("---------------------------------------------"); vehicleCreator = new VehicleCreator(new HondaBuilder()); vehicleCreator.CreateVehicle(); vehicle = vehicleCreator.GetVehicle(); vehicle.ShowInfo(); // Wait for user Console.ReadKey(); //Structural Console.WriteLine("Structural"); // Wait for user Console.ReadKey(); //1 - Proxy Console.WriteLine("Proxy"); ProxyClient proxy = new ProxyClient(); Console.WriteLine("Data from Proxy Client = {0}", proxy.GetData()); // Wait for user Console.ReadKey(); //2 - Flyweight Console.WriteLine("Flyweight"); ShapeObjectFactory sof = new ShapeObjectFactory(); IShape shape = sof.GetShape("Rectangle"); shape.Print(); shape = sof.GetShape("Rectangle"); shape.Print(); shape = sof.GetShape("Rectangle"); shape.Print(); shape = sof.GetShape("Circle"); shape.Print(); shape = sof.GetShape("Circle"); shape.Print(); shape = sof.GetShape("Circle"); shape.Print(); int NumObjs = sof.TotalObjectsCreated; Console.WriteLine("\nTotal No of Objects created = {0}", NumObjs); // Wait for user Console.ReadKey(); //3 - Facade Console.WriteLine("Facade"); CarFacade facade = new CarFacade(); facade.CreateCompleteCar(); // Wait for user Console.ReadKey(); //4 - Bridge Console.WriteLine("Bridge"); IMessageSender email = new EmailSender(); IMessageSender queue = new MSMQSender(); IMessageSender web = new WebServiceSender(); Message message = new SystemMessage(); message.Subject = "Test Message"; message.Body = "Hi, This is a Test Message"; message.MessageSender = email; message.Send(); message.MessageSender = queue; message.Send(); message.MessageSender = web; message.Send(); UserMessage usermsg = new UserMessage(); usermsg.Subject = "Test Message"; usermsg.Body = "Hi, This is a Test Message"; usermsg.UserComments = "I hope you are well"; usermsg.MessageSender = email; usermsg.Send(); // Wait for user Console.ReadKey(); //5 - Adapter Console.WriteLine("Adapter"); ITarget Itarget = new EmployeeAdapter(); ThirdPartyBillingSystem client = new ThirdPartyBillingSystem(Itarget); client.ShowEmployeeList(); // Wait for user Console.ReadKey(); //6 - Decorator Console.WriteLine("Decorator"); HondaCity car = new HondaCity(); Console.WriteLine("Honda City base price are : {0}", car.Price); SpecialOffer offer = new SpecialOffer(car); offer.DiscountPercentage = 25; offer.Offer = "25 % discount"; Console.WriteLine("{1} @ Diwali Special Offer and price are : {0} ", offer.Price, offer.Offer); // Wait for user Console.ReadKey(); }