static void Main(string[] args) { // Non-adapted chemical compound Compound unknown = new Compound("Unknown"); unknown.Display(); // Another Non-adapted chemical compound Compound waterWithoutAdapter = new Compound("Water"); waterWithoutAdapter.Display(); // Adapted chemical compounds Compound water = new RichCompound("Water"); water.Display(); Compound benzene = new RichCompound("Benzene"); benzene.Display(); Compound ethanol = new RichCompound("Ethanol"); ethanol.Display(); }
private void btnAdapter_Click(object sender, EventArgs e) { // ------------------------------------------------------------------------ // Adapter Pattern // https://dofactory.com/net/adapter-design-pattern // Frequency of use: 4 - Medium txtOutput.Text = ""; // Non-adapted chemical compound Compound unknown = new Compound("Unknown"); txtOutput.Text += unknown.Display() + Environment.NewLine; // Adapted chemical compounds Compound water = new RichCompound("Water"); txtOutput.Text += water.Display() + Environment.NewLine; Compound benzene = new RichCompound("Benzene"); txtOutput.Text += benzene.Display() + Environment.NewLine; Compound ethanol = new RichCompound("Ethanol"); txtOutput.Text += ethanol.Display() + Environment.NewLine; }
public static void TestCompunds() { var unknown = new Compound("Unknown"); StringAssert.AreEqualIgnoringCase(unknown.Display(true), "Unknown"); // Adapted chemical compounds Compound water = new RichCompound("Water"); water.Display(); var waterProperty = water.Display(true).Split('|'); Assert.True(waterProperty[0].Equals("100") && waterProperty[1].Equals("0") && waterProperty[2].Equals("18.015") && waterProperty[3].Equals("H20")); Compound benzene = new RichCompound("Benzene"); benzene.Display(); var benzeneProperty = benzene.Display(true).Split('|'); Assert.True(benzeneProperty[0].Equals("80.1") && benzeneProperty[1].Equals("5.5") && benzeneProperty[2].Equals("78.1134") && benzeneProperty[3].Equals("C6H6")); Compound ethanol = new RichCompound("Ethanol"); ethanol.Display(); var ethanolProperty = ethanol.Display(true).Split('|'); Assert.True(ethanolProperty[0].Equals("78.3") && ethanolProperty[1].Equals("-114.1") && ethanolProperty[2].Equals("46.0688") && ethanolProperty[3].Equals("C2H5OH")); }
private static void RealWorldCode() { // Non-adapted chemical compound Compound unknown = new Compound("Unknown"); unknown.Display(); // Adapted chemical compounds Compound water = new RichCompound("Water"); water.Display(); Compound benzene = new RichCompound("Benzene"); benzene.Display(); Compound ethanol = new RichCompound("Ethanol"); ethanol.Display(); // Wait for user Console.ReadKey(); }
static void Main(string[] args) { //*** STRUCTURE EXAMPLE ***/ // Create adapter and place a request var target = new Adapter(); target.Request(); //*** REAL WORLD EXAMPLE ***/ // Chemical compound var unknown = new Compound(); unknown.Display(); // Adapted chemical compounds var water = new RichCompound(Chemical.Water); water.Display(); var benzene = new RichCompound(Chemical.Benzene); benzene.Display(); var ethanol = new RichCompound(Chemical.Ethanol); ethanol.Display(); //*** REAL WORLD EXAMPLE ***/ // Ducks and Turkeys var duck = new MallardDuck(); var turkey = new WildTurkey(); IDuck turkeyAdapter = new TurkeyToDuckAdapter(turkey); Console.WriteLine("The Turkey says..."); turkey.Gobble(); turkey.Fly(); Console.WriteLine("The Duck says..."); TestDuck(duck); Console.WriteLine("The TurkeyAdapter says..."); TestDuck(turkeyAdapter); // Test 2: Turkey test drive ITurkey duckAdapter = new DuckToTurkeyAdapter(duck); for (int i = 0; i < 10; i++) { Console.WriteLine("The DuckAdapter says..."); duckAdapter.Gobble(); duckAdapter.Fly(); } }
public void WaterCompoundTest() { Compound water = new RichCompound("Water"); Assert.AreEqual("H2O", water.MolecularFormula); Assert.AreEqual(18.0150, water.MolecularWeight); Assert.AreEqual(0.0f, water.MeltingPoint); Assert.AreEqual(100.0f, water.BoilingPoint); }
public void BenzeneCompoundTest() { Compound benzene = new RichCompound("Benzene"); Assert.AreEqual("C6H6", benzene.MolecularFormula); Assert.AreEqual(78.1134, benzene.MolecularWeight); Assert.AreEqual(5.5f, benzene.MeltingPoint); Assert.AreEqual(80.1f, benzene.BoilingPoint); }
public void EthanolCompoundTest() { Compound ethanol = new RichCompound("Ethanol"); Assert.AreEqual("C2H5OH", ethanol.MolecularFormula); Assert.AreEqual(46.0688, ethanol.MolecularWeight); Assert.AreEqual(-114.1f, ethanol.MeltingPoint); Assert.AreEqual(78.3f, ethanol.BoilingPoint); }
public static void Main() { ICompound water = new RichCompound("Water"); water.Display(); ICompound benzene = new RichCompound("Benzene"); benzene.Display(); ICompound ethanol = new RichCompound("Ethanol"); ethanol.Display(); }
internal static void Main(string[] args) { ICompound water = new RichCompound("Water"); ProcessCompound(water); ICompound benzene = new RichCompound("Benzene"); ProcessCompound(benzene); ICompound ethanol = new RichCompound("Ethanol"); ProcessCompound(ethanol); }
internal static void Main(string[] args) { ICompound water = new RichCompound("Water"); water.Display(); ICompound benzene = new RichCompound("Benzene"); benzene.Display(); ICompound ethanol = new RichCompound("Ethanol"); ethanol.Display(); }
static void Main(string[] args) { ICompound water = new RichCompound("water"); water.Display(); ICompound benzene = new RichCompound("benzene"); benzene.Display(); ICompound ethanol = new RichCompound("ethanol"); ethanol.Display(); ICompound etilen = new RichCompound("etilen"); etilen.Display(); // will display default values of the variables, because there isn't etilen item in the databank }
public static void Run() { Compound water = new RichCompound(CompoundType.Water); Compound benzene = new RichCompound(CompoundType.Benzene); Compound ethanol = new RichCompound(CompoundType.Ethanol); Console.WriteLine("<Adapter Pattern Example>"); Console.WriteLine(); Console.WriteLine(water); Console.WriteLine(benzene); Console.WriteLine(ethanol); Console.WriteLine(); Console.WriteLine("</Adapter Pattern Example>"); Console.WriteLine(); }
public static void Test() { // Non-adapted chemical compound Compound unknown = new Compound(); unknown.Display(); // Adapted chemical compounds Compound water = new RichCompound("Water"); water.Display(); Compound benzene = new RichCompound("Benzene"); benzene.Display(); Compound ethanol = new RichCompound("Ethanol"); ethanol.Display(); }
/// <summary> /// Entry point into console application. /// </summary> static void Main() { Compound unknown = new Compound("Unknown"); unknown.Display(); Compound water = new RichCompound("Water"); water.Display(); Compound benzene = new RichCompound("Benzene"); benzene.Display(); Compound ethanol = new RichCompound("Ethanol"); ethanol.Display(); }
static void Main(string[] args) { // Non-adapted chemical compound Compound unknown = new Compound("Unknown"); unknown.Display(); // Adapted chemical compounds Compound water = new RichCompound("Water"); water.Display(); Compound benzene = new RichCompound("Benzene"); benzene.Display(); Compound ethanol = new RichCompound("Ethanol"); ethanol.Display(); // Wait for user Console.ReadKey(true); }
static void Main(string[] args) { var unknown = new Compound(); unknown.Display(); var water = new RichCompound(Chemical.Water); water.Display(); var ethanol = new RichCompound(Chemical.Ethanol); ethanol.Display(); var benzene = new RichCompound(Chemical.Benzene); benzene.Display(); Console.ReadLine(); }
private static void AdapterDemo() { // Non-adapted chemical compound var unknown = new Compound("Unknown"); unknown.Display(); // Adapted chemical compounds Compound water = new RichCompound("Water"); water.Display(); Compound benzene = new RichCompound("Benzene"); benzene.Display(); Compound ethanol = new RichCompound("Ethanol"); ethanol.Display(); }
static void AdapterTester() { #region sample 1 Target target = new Adapter(); target.Request(); #endregion #region sample 2 var unknown = new Compound("Unknown"); unknown.Display(); // Adapted chemical compounds Compound water = new RichCompound("Water"); water.Display(); Compound benzene = new RichCompound("Benzene"); benzene.Display(); Compound ethanol = new RichCompound("Ethanol"); ethanol.Display(); #endregion }
public static void Run() { var richCompound = new RichCompound("benzene"); richCompound.Display(); }
static void Main(string[] args) { Compound compound = new RichCompound("WATER"); compound.Display(); }
public DesignPatternModule() { Get["/testStatePattern"] = _ => { var traficLight = new TraficLight(); var process = traficLight.StartTheTraficLight(); return(process); }; Get["/testNullObjectPattern"] = _ => { var dog = new Dog(); var dougSound = "Dog Sound: " + dog.MakeSound() + ", "; var unknown = Animal.Null; var noAnimalSound = "No Animal Sound: " + unknown.MakeSound(); return(dougSound + noAnimalSound); }; Get["/testObserverPattern"] = _ => { var observable = new Observable(); var observer = new Observer(); observable.SomethingHappened += observer.HandleEvent; var observerValue = observable.DoSomething(); return(observerValue); }; Get["/testBridgePattern/{currentSource}"] = _ => { var currentSource = (string)_.currentSource; var myCustomTv = new MyCustomTv(); switch (currentSource) { case "1": myCustomTv.VideoSource = new LocalCableTv(); break; case "2": myCustomTv.VideoSource = new CableColorTv(); break; case "3": myCustomTv.VideoSource = new TigoService(); break; } var tvGuide = myCustomTv.ShowTvGuide(); var playVideo = myCustomTv.ShowTvGuide(); return(tvGuide + " / " + playVideo); }; Get["/testVisitorPattern"] = _ => { var popRock = new PopRockMusicVisitor(); var musicLibrary = new MusicLibrary(); var songs = musicLibrary.Accept(popRock); return(songs); }; Get["/testBuilderPattern"] = _ => { var shop = new Shop(); VehicleBuilder builder = new CarBuilder(); shop.Construct(builder); var getBuilderProcess = builder.Vehicle.Show(); return(getBuilderProcess); }; Get["/testInterpreterPattern"] = _ => { const string roman = "MCMXXVIII"; var context = new Context(roman); var tree = new List <Expression> { new ThousandExpression(), new HundredExpression(), new TenExpression(), new OneExpression() }; foreach (var exp in tree) { exp.Interpret(context); } return("Interpreter Input: " + roman + ", Interpreter Output: " + context.Output); }; Get["/testChainOfResponsabilityPattern"] = _ => { var response = ""; var pamela = new Director(); var byron = new VicePresident(); var colin = new President(); pamela.SetSuccessor(byron); byron.SetSuccessor(colin); var p = new Purchase(2034, 350.00, "Assets"); response = pamela.ProcessRequest(p); p = new Purchase(2035, 32590.10, "Project X"); response += " / " + pamela.ProcessRequest(p); p = new Purchase(2036, 90000.00, "Project Y"); response += " / " + pamela.ProcessRequest(p); p = new Purchase(2036, 122100.00, "Project Z"); response += " / " + pamela.ProcessRequest(p); return(response); }; Get["/testIteratorPattern"] = _ => { var collection = new Collection(); collection[0] = new Item("Item 0"); collection[1] = new Item("Item 1"); collection[2] = new Item("Item 2"); collection[3] = new Item("Item 3"); collection[4] = new Item("Item 4"); collection[5] = new Item("Item 5"); collection[6] = new Item("Item 6"); collection[7] = new Item("Item 7"); collection[8] = new Item("Item 8"); var iterator = collection.CreateIterator(); iterator.Step = 2; var response = "Iterating over collection:"; for (var item = iterator.First(); !iterator.IsDone; item = iterator.Next()) { response += item.Name + " / "; } return(response); }; Get["/testAdapterPattern"] = _ => { var response = ""; var unknown = new Compound("Unknown"); response += " / " + unknown.Display(); var water = new RichCompound("Water"); response += " / " + water.Display(); var benzene = new RichCompound("Benzene"); response += " / " + benzene.Display(); var ethanol = new RichCompound("Ethanol"); response += " / " + ethanol.Display(); return(response); }; Get["/testCommandPattern"] = _ => { var response = ""; var user = new User(); response += user.Compute('+', 100) + " / "; response += user.Compute('-', 50) + " / "; response += user.Compute('*', 10) + " / "; response += user.Compute('/', 2) + " / "; response += user.Undo(4) + " / "; response += user.Redo(3); return(response); }; Get["/testFactoryPattern"] = _ => { var response = ""; var documents = new Document[2]; documents[0] = new Resume(); documents[1] = new Report(); foreach (var document in documents) { response += document.GetType().Name + "--"; foreach (var page in document.Pages) { response += " " + page.GetType().Name; } } return(response); }; Get["/testStrategyPattern"] = _ => { var response = ""; var studentRecords = new SortedList(); studentRecords.Add("Samual"); studentRecords.Add("Jimmy"); studentRecords.Add("Sandra"); studentRecords.Add("Vivek"); studentRecords.Add("Anna"); studentRecords.SetSortStrategy(new QuickSort()); response += "Quicksort: " + studentRecords.Sort() + " -- "; studentRecords.SetSortStrategy(new ShellSort()); response += "ShellSort: " + studentRecords.Sort() + " -- "; studentRecords.SetSortStrategy(new MergeSort()); response += "MergeSort: " + studentRecords.Sort(); return(response); }; Get["/testTemplatePattern"] = _ => { var response = ""; AbstractClass aA = new ConcreteClassA(); response += aA.TemplateMethod(); AbstractClass aB = new ConcreteClassB(); response += aB.TemplateMethod(); return(response); }; Get["/testFacadePattern"] = _ => { var response = ""; var mortgage = new Mortgage(); var customer = new Customer("Ann McKinsey"); var eligible = mortgage.IsEligible(customer, 125000); response += customer.Name + " has been " + (eligible ? "Approved" : "Rejected"); return(response); }; Get["/mediatorPattern"] = _ => { var response = ""; var chatroom = new Chatroom(); Participant paul = new Beatle("Paul"); Participant john = new Beatle("John"); Participant yoko = new NonBeatle("Yoko"); Participant ringo = new Beatle("Ringo"); chatroom.Register(paul); chatroom.Register(john); chatroom.Register(yoko); chatroom.Register(ringo); response += yoko.Send("John", "Hi John!") + " "; response += paul.Send("Ringo", "All you need is love") + " "; response += paul.Send("John", "Can't buy me love") + " "; response += john.Send("Yoko", "My sweet love"); return(response); }; Get["/testFlyweightPattern"] = _ => { var response = ""; const string document = "AAZZBBZB"; var chars = document.ToCharArray(); var factory = new CharacterFactory(); var pointSize = 10; foreach (var c in chars) { pointSize++; var character = factory.GetCharacter(c); response += character.Display(pointSize) + " "; } return(response); }; Get["/testMomentoPattern"] = _ => { var response = "Save Sales, Restore Memento"; var s = new SalesProspect { Name = "Noel van Halen", Phone = "(412) 256-0990", Budget = 25000.0 }; var m = new ProspectMemory { Memento = s.SaveMemento() }; s.Name = "Leo Welch"; s.Phone = "(310) 209-7111"; s.Budget = 1000000.0; s.RestoreMemento(m.Memento); return(response); }; Get["/testDoubleDispatchPattern"] = _ => { var response = ""; object x = 5; var dispatch = new DoubleDispatch(); response += dispatch.Foo <int>(x); response += dispatch.Foo <string>(x.ToString()); return(response); }; Get["/testTransactionScriptPattern"] = _ => { var response = ""; response += "Booked Holiday: " + HolidayService.BookHolidayFor(1, new DateTime(2016, 12, 31), new DateTime(2017, 1, 5)) + " - "; response += "Employes Leaving in Holiday: " + string.Join(", ", HolidayService.GetAllEmployeesOnLeaveBetween(new DateTime(2016, 12, 31), new DateTime(2017, 1, 5)).Select(x => x.Name)) + " - "; response += "Employes without Holiday: " + string.Join(", ", HolidayService.GetAllEmployeesWithHolidayRemaining().Select(x => x.Name)); return(response); }; }
static void Main(string[] args) { Console.WriteLine("******Singleton******\n"); LoadBalancer L1 = LoadBalancer.GetLoadBalancer(); LoadBalancer L2 = LoadBalancer.GetLoadBalancer(); LoadBalancer L3 = LoadBalancer.GetLoadBalancer(); LoadBalancer L4 = LoadBalancer.GetLoadBalancer(); if (L1 == L2 && L2 == L3 && L3 == L4) { Console.WriteLine("Same Instance"); } LoadBalancer load = LoadBalancer.GetLoadBalancer(); for (int i = 0; i < 15; i++) { Console.WriteLine(load.Server); } Console.WriteLine("\n******AbstractFactory******\n"); ContinentFactory africa = new AfricaFactory(); AnimalWorld earth = new AnimalWorld(africa); earth.RunFoodChain(); ContinentFactory america = new AmericaFactory(); earth = new AnimalWorld(america); earth.RunFoodChain(); Console.WriteLine("\n******Factory******\n"); Document[] document = new Document[2]; document[0] = new Report(); document[1] = new Resume(); foreach (Document doc in document) { Console.WriteLine(doc.GetType().Name + " contains:"); foreach (Page page in doc.Pages) { Console.WriteLine("\t" + page.GetType().Name); } } Console.WriteLine("\n******Facade******\n"); Mortgage mort = new Mortgage(); Customer customer = new Customer("Chad"); int amount = 100000; bool elligable = mort.IsElligable(customer, amount); Console.WriteLine(customer.Name + " is " + (elligable ? "elligable" : "not elligable")); Console.WriteLine("\n******Decorator******\n"); Book book = new Book("George RR Martin", "A song of ice and fire", 22); book.Display(); Video video = new Video("Jaws", "Spielberg", 92, 18); video.Display(); Borrowable borrowableVideo = new Borrowable(video); borrowableVideo.BorrowItem("Sky"); borrowableVideo.BorrowItem("Inga"); borrowableVideo.Display(); Console.WriteLine("\n******Prototype******\n"); ColorManager colorManager = new ColorManager(); colorManager["red"] = new Color(255, 0, 0); colorManager["green"] = new Color(0, 255, 0); colorManager["blue"] = new Color(0, 0, 255); colorManager["angry"] = new Color(255, 54, 0); colorManager["peace"] = new Color(128, 211, 128); colorManager["flame"] = new Color(211, 34, 20); Color color1 = colorManager["red"].Clone() as Color; Color color2 = colorManager["peace"].Clone() as Color; Color color3 = colorManager["flame"].Clone() as Color; Console.WriteLine("\n******Builder******\n"); VehicleBuilder builder; Shop shop = new Shop(); builder = new ScooterBuilder(); shop.Construct(builder); builder.Vehicle.Show(); builder = new CarBuilder(); shop.Construct(builder); builder.Vehicle.Show(); builder = new MotorcycleBuilder(); shop.Construct(builder); builder.Vehicle.Show(); Console.WriteLine("\n******Adapter******\n"); Compound unknown = new RichCompound("Unknown"); unknown.Display(); Compound water = new RichCompound("Water"); water.Display(); Compound benzene = new RichCompound("Benzene"); benzene.Display(); Compound ethanol = new RichCompound("Ethanol"); ethanol.Display(); Console.WriteLine("\n******Bridge******\n"); Customers customers = new Customers("Chicago"); customers.Data = new CustomersData(); customers.Show(); customers.Next(); customers.Show(); customers.Next(); customers.Show(); customers.Add("Henry Velasquez"); customers.ShowAll(); Console.WriteLine("\n******Composite******\n"); CompositeElement root = new CompositeElement("Picture"); root.Add(new PrimitiveElement("Red Line")); root.Add(new PrimitiveElement("Blue Circle")); root.Add(new PrimitiveElement("Green Box")); CompositeElement comp = new CompositeElement("Two Circles"); comp.Add(new PrimitiveElement("Black Circle")); comp.Add(new PrimitiveElement("White Circle")); root.Add(comp); PrimitiveElement pe = new PrimitiveElement("Yellow Line"); root.Add(pe); root.Remove(pe); root.Display(1); Console.WriteLine("\n******Observer******\n"); IBM ibm = new IBM("IBM", 120.00); ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("Berkshire")); ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; Console.WriteLine("\n******Strategy******\n"); SortedList studentRecords = new SortedList(); studentRecords.Add("Samuel"); studentRecords.Add("Jimmy"); studentRecords.Add("Sandra"); studentRecords.Add("Vivek"); studentRecords.Add("Anna"); studentRecords.SetSortStrategy(new QuickSort()); studentRecords.Sort(); studentRecords.SetSortStrategy(new ShellSort()); studentRecords.Sort(); studentRecords.SetSortStrategy(new MergeSort()); studentRecords.Sort(); /* * Console.WriteLine("\n******Template******\n"); * DataAccessObject daoCategories = new Categories(); * daoCategories.Run(); * * DataAccessObject daoProducts = new Products(); * daoProducts.Run(); */ Console.WriteLine("\n***Chain of Responsibility***\n"); Approver larry = new Director(); Approver sam = new VicePresident(); Approver tammy = new President(); larry.SetSuccessor(sam); sam.SetSuccessor(tammy); Purchase p = new Purchase(2034, 350.00, "Assets"); larry.ProcessRequest(p); p = new Purchase(2035, 32590.10, "Project X"); larry.ProcessRequest(p); p = new Purchase(2036, 122100.00, "Project Y"); larry.ProcessRequest(p); Console.WriteLine("\n******Command******\n"); User user = new User(); user.Compute('+', 100); user.Compute('-', 50); user.Compute('*', 10); user.Compute('/', 2); user.Undo(4); user.Redo(3); Console.ReadKey(); }