static void Main(string[] args) { 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."); } Console.ReadKey(); }
static void Main(string[] args) { 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(); switch (cmd) { default: Console.WriteLine("Command \"ON\" or \"OFF\" is required."); break; case "ON": s.StoreAndExecute(switchUp); break; case "OFF": s.StoreAndExecute(switchDown); break; } Console.WriteLine("Press any key to exit!"); Console.ReadKey(); }
public static void Test() { var light = new Light(); var flipUpCommand = new FlipUpCommand(light); var flipDownCommand = new FlipDownCommand(light); var @switch = new Switch(); @switch.StoreAndExecute(flipUpCommand); @switch.StoreAndExecute(flipDownCommand); }
public void FlipUpCommandShouldSetLightStateToOn() { var expectedStatus = false; Light light = new Light(); InterpreterPattern.ICommand commandA = new FlipDownCommand(light); LightSwitch lightSwitch = new LightSwitch(); lightSwitch.SetAndExecuteCommand(commandA); light.State.Should().Be(expectedStatus); }
static void CommandCalling() { Console.WriteLine("Enter Commands (ON/OFF) : "); string input = "ON"; //Console.ReadLine(); Light light = new Light(); ICommand switchUp = new FlipUpCommand(light); ICommand switchDown = new FlipDownCommand(light); Switch s = new Switch(); if (input == "ON") { s.StoreAndExecute(switchUp); } else if (input == "OFF") { s.StoreAndExecute(switchDown); } else { Console.WriteLine("Command \"ON\" or \"OFF\" is required."); } }
public void TestCommand(string[] args) { var lamp = new Light(); ICommand switchUp = new FlipUpCommand(lamp); ICommand switchDown = new FlipDownCommand(lamp); var s = new Switch(); string arg = args.Length > 0 ? args[0].ToUpper() : null; if (arg == "ON") { s.StoreAndExecute(switchUp); } else if (arg == "OFF") { s.StoreAndExecute(switchDown); } else { Console.WriteLine("Argument \"ON\" or \"OFF\" is required."); } }
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(); }
public ActionResult Index(int?searchString) { var viewModel = new ViewModels.DepartmentViewModel(); viewModel.Books = db.Books.Where(b => b.Year > searchString); ViewBag.Title = "Home Page"; //Prototype PrototypeCalling(); //Decorator DecoratorCalling(); //Adapter AdapterCalling(); //Composite CompositeCalling(); //Proxy ProxyCalling(); //Iterator IteratorCalling(); //State StateCalling(); //Command CommandCalling(); //Mediator MediatorCalling(); return(View(viewModel)); void CommandCalling() { Console.WriteLine("Enter Commands (ON/OFF) : "); string input = "ON"; //Console.ReadLine(); Light light = new Light(); ICommand switchUp = new FlipUpCommand(light); ICommand switchDown = new FlipDownCommand(light); CRUDWebApp.DesignPatterns.Switch s = new CRUDWebApp.DesignPatterns.Switch(); if (input == "ON") { s.StoreAndExecute(switchUp); } else if (input == "OFF") { s.StoreAndExecute(switchDown); } else { Debug.WriteLine("Command \"ON\" or \"OFF\" is required."); //Console.log("Command \"ON\" or \"OFF\" is required."); } } void StateCalling() { Steak steak = new Steak("T-Bone"); steak.AddTemp(120); steak.AddTemp(15); steak.AddTemp(15); steak.RemoveTemp(10); steak.RemoveTemp(15); steak.AddTemp(20); steak.AddTemp(20); steak.AddTemp(20); } void IteratorCalling() { // The client code may or may not know about the Concrete Iterator // or Collection classes, depending on the level of indirection you // want to keep in your program. var collection = new WordsCollection(); collection.AddItem("First"); collection.AddItem("Second"); collection.AddItem("Third"); Debug.WriteLine("Straight traversal:"); foreach (var element in collection) { Debug.WriteLine(element); } Debug.WriteLine("\nReverse traversal:"); collection.ReverseDirection(); foreach (var element in collection) { Debug.WriteLine(element); } } void ProxyCalling() { var secureNestProxy = new SecureNestProxy(new RealNest()); secureNestProxy.Access("Stegosaurus"); secureNestProxy.Access("TRex"); } void CompositeCalling() { var plants = new List <IPlant>(); var branchI = new Branch(new List <IPlant>() { new Leaf(), new Leaf() }); var branchII = new Branch(new List <IPlant>() { new Leaf(), new Leaf(), new Leaf(), new Leaf() }); plants.Add(new Branch( new List <IPlant>() { branchI, branchII } )); //branch with two branches plants.Add(new Leaf()); //one leaf plants.Add(new Branch(new List <IPlant>() { new Leaf(), new Leaf(), new Leaf(), new Leaf(), new Leaf() })); //one branch with leafs plants.Add(new Leaf()); //one leaf foreach (IPlant plant in plants) { plant.Eat(); } } void AdapterCalling() { var bagOfPeelableFruit = new List <IPeelable>(); bagOfPeelableFruit.Add(new Orange()); bagOfPeelableFruit.Add(new Banana()); bagOfPeelableFruit.Add(new SkinnableTOPelableAdapter(new Apple())); bagOfPeelableFruit.Add(new SkinnableTOPelableAdapter(new Pear())); foreach (var fruit in bagOfPeelableFruit) { fruit.Peel(); } } void DecoratorCalling() { IPizza pizza = new Pizza(); IPizza cheeseDecorator = new CheeseDecorator(pizza); IPizza tomatoDecorator = new TomatoDecorator(cheeseDecorator); IPizza onionDecorator = new OnionDecorator(tomatoDecorator); Debug.WriteLine(tomatoDecorator.getPizzaType()); } void PrototypeCalling() { Developer dev = new Developer(); dev.Name = "Ann"; dev.Role = "Team Leader"; dev.PreferredLanguage = "C#"; Developer devCopy = (Developer)dev.Clone(); devCopy.Name = "Anna"; //Not mention Role and PreferredLanguage, it will copy above Debug.WriteLine(dev.GetDetails()); Debug.WriteLine(devCopy.GetDetails()); Typist typist = new Typist(); typist.Name = "Beta"; typist.Role = "Typist"; typist.WordsPerMinute = 120; Typist typistCopy = (Typist)typist.Clone(); typistCopy.Name = "Betty"; typistCopy.WordsPerMinute = 115;//Not mention Role, it will copy above Debug.WriteLine(typist.GetDetails()); Debug.WriteLine(typistCopy.GetDetails()); } void MediatorCalling() { ConcessionsMediator mediator = new ConcessionsMediator(); NorthConcessionStand leftKitchen = new NorthConcessionStand(mediator); SouthConcessionStand rightKitchen = new SouthConcessionStand(mediator); mediator.NorthConcessions = leftKitchen; mediator.SouthConcessions = rightKitchen; leftKitchen.Send("Can you send some popcorn?"); rightKitchen.Send("Sure thing, Kenny's on his way."); rightKitchen.Send("Do you have any extra hot dogs? We've had a rush on them over here."); leftKitchen.Send("Just a couple, we'll send Kenny back with them."); } }