Exemple #1
0
        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();
        }
        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"));
        }
Exemple #3
0
        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;
        }
Exemple #4
0
    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();
    }
Exemple #5
0
    void TypeButtonPress(bool up)
    {
        Audio.PlayGameSoundAtTransform(KMSoundOverride.SoundEffect.BigButtonPress, transform);

        if (solved)
        {
            return;
        }

        Compound[] options;
        switch (dispMode)
        {
        case Compound.State.Solid:
            options = solids;
            break;

        case Compound.State.Liquid:
            options = liquids;
            break;

        case Compound.State.Gas:
            options = gases;
            break;

        default:
            options = solids;     //failsafe def
            break;
        }

        selectedCompoundIndex += up ? 1 : -1;
        if (selectedCompoundIndex < 0)
        {
            selectedCompoundIndex += options.Length;
        }
        if (selectedCompoundIndex >= options.Length)
        {
            selectedCompoundIndex = 0;
        }
        selectedCompound = options[selectedCompoundIndex];

        switch (dispMode)
        {
        case Compound.State.Solid:
            formulaShowSelected = formulaShow[selectedCompoundIndex];
            break;

        case Compound.State.Liquid:
            formulaShowSelected = formulaShow[selectedCompoundIndex + solids.Length];
            break;

        case Compound.State.Gas:
            formulaShowSelected = formulaShow[selectedCompoundIndex + solids.Length + liquids.Length];
            break;
        }

        selectedCompound.Display(TypeText, formulaShowSelected);
    }
Exemple #6
0
        static void Main(string[] args)
        {
            Compound comp = new Compound("Water");

            comp.Display();

            Compound legComp = new AdapterCompound("Benzene");
            legComp.Display();
        }
Exemple #7
0
    void DeployDispenser()
    {
        switch (dispMode)
        {
        case Compound.State.Solid:
            SolidDispenser.gameObject.SetActive(true);
            LiquidDispenser.gameObject.SetActive(false);
            GasDispenser.gameObject.SetActive(false);
            anim.StartAnimation(Animations.Animation.SolidExtend, SolidDispenser, SolidExt);
            PrevIndicator.material = GasIndMat;
            NextIndicator.material = LiquidIndMat;
            PrevIndLight.color     = new Color(1, 1, 0);
            NextIndLight.color     = new Color(0, 0, 1);
            prevMode = Compound.State.Gas;
            nextMode = Compound.State.Liquid;
            selectedCompoundIndex = Random.Range(0, solids.Length);
            selectedCompound      = solids[selectedCompoundIndex];
            formulaShowSelected   = formulaShow[selectedCompoundIndex];
            break;

        case Compound.State.Liquid:
            SolidDispenser.gameObject.SetActive(false);
            LiquidDispenser.gameObject.SetActive(true);
            GasDispenser.gameObject.SetActive(false);
            anim.StartAnimation(Animations.Animation.LiquidExtend, LiquidDispenser, LiquidExt);
            PrevIndicator.material = SolidIndMat;
            NextIndicator.material = GasIndMat;
            PrevIndLight.color     = new Color(1, 0, 0);
            NextIndLight.color     = new Color(1, 1, 0);
            prevMode = Compound.State.Solid;
            nextMode = Compound.State.Gas;
            selectedCompoundIndex = Random.Range(0, liquids.Length);
            selectedCompound      = liquids[selectedCompoundIndex];
            formulaShowSelected   = formulaShow[solids.Length + selectedCompoundIndex];
            break;

        case Compound.State.Gas:
            SolidDispenser.gameObject.SetActive(false);
            LiquidDispenser.gameObject.SetActive(false);
            GasDispenser.gameObject.SetActive(true);
            anim.StartAnimation(Animations.Animation.GasExtend, GasDispenser, GasExt);
            PrevIndicator.material = LiquidIndMat;
            NextIndicator.material = SolidIndMat;
            PrevIndLight.color     = new Color(0, 0, 1);
            NextIndLight.color     = new Color(1, 0, 0);
            prevMode = Compound.State.Liquid;
            nextMode = Compound.State.Solid;
            selectedCompoundIndex = Random.Range(0, gases.Length);
            selectedCompound      = gases[selectedCompoundIndex];
            formulaShowSelected   = formulaShow[solids.Length + liquids.Length + selectedCompoundIndex];
            break;
        }

        selectedCompound.Display(TypeText, formulaShowSelected);
    }
        public static void Test()
        {
            Compound water = new Compound("Water");

            water.Display();

            Compound benzene = new Compound("Benzene");

            benzene.Display();

            Compound alcohol = new Compound("Alcohol");

            alcohol.Display();
        }
Exemple #9
0
        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();
        }
        public void Execute()
        {
            Compound unknown = new Compound("Unknown");

            unknown.Display();

            Compound water = new RichCompoundAdapter("Water");

            water.Display();

            Compound benzene = new RichCompoundAdapter("Benzene");

            benzene.Display();

            Compound ethanol = new RichCompoundAdapter("Ethanol");

            ethanol.Display();
        }
Exemple #12
0
        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);
        }
Exemple #13
0
        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();
        }
Exemple #14
0
        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();
        }
Exemple #15
0
        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 Main(string[] args)
  {
    // Retrieve and display water characteristics
    Compound water = new Compound( "Water" );
    water.Display();

    // Retrieve and display benzene characteristics
    Compound benzene = new Compound( "Benzene" );
    benzene.Display();

    // Retrieve and display alcohol characteristics
    Compound alcohol = new Compound( "Alcohol" );
    alcohol.Display();

    Console.Read();
  }
Exemple #17
0
        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);
            };
        }