Example #1
0
        private void MenuItemClickedOnVirtualVertex(object sender, EventArgs args)
        {
            var vertex = sender as VertexControl;
            // to do working with model if necessary
            var edges = this.graphArea.EdgesList.Select(x => x.Value)
                        .Where(x => x.Target == vertex || x.Source == vertex);
            var deleteEdgesCommand = new CompositeCommand();

            foreach (var edge in edges)
            {
                var anotherVertex = edge.Target == vertex ? edge.Source : edge.Target;
                void removingEdge() => this.graphArea.RemoveEdge(edge.GetDataEdge <EdgeViewModel>());

                void removingAnotherVertex()
                {
                    if (IsVertexVirtual(anotherVertex))
                    {
                        this.graphArea.RemoveVertex(anotherVertex.GetDataVertex <NodeViewModel>());
                    }
                }

                var command1 = new Toolbar.Command(removingEdge);
                var command2 = new Toolbar.Command(removingAnotherVertex);
                deleteEdgesCommand.Add(command1);
                deleteEdgesCommand.Add(command2);
            }
            (deleteEdgesCommand as EditorPluginInterfaces.ICommand).Execute();
            this.graphArea.RemoveVertex(vertex.GetDataVertex <NodeViewModel>());
        }
Example #2
0
        /// <summary>
        /// Allows decouple a sender from a receiver. The sender will talk to the receive through a command. Commands can be undone and persisted.
        ///
        /// With Command its easy to build an independent framework that anyone can use. By creating a custom service which implements a base interface,
        /// any command can be run with the same method, depending on the service.
        /// </summary>
        static void Command()
        {
            // One command attached to an action.
            var service = new CustomerService();
            var command = new AddCustomerCommand(service);
            var button  = new Command.fx.Button(command);

            button.Click();

            // Multiple commands attached to an action.
            var composite = new CompositeCommand();

            composite.Add(new ResizeCommand());
            composite.Add(new BlackAndWhiteCommand());
            composite.Execute();

            // Using the undo mechanism. Multiple commands are implementing the same interface(s) and using the same history.
            var history  = new Command.editor.History();
            var document = new Command.editor.HtmlDocument();

            document.Content = "Hello";

            var boldCommand = new BoldCommand(document, history);

            boldCommand.Execute();

            Console.WriteLine(document.Content);

            var undoCommant = new UndoCommand(history);

            undoCommant.Execute();

            Console.WriteLine(document.Content);
        }
Example #3
0
        public void RunDefaultImageEnchancement()
        {
            var composite = new CompositeCommand();

            composite.Add(new ResizeCommand());
            composite.Add(new BlackAndWhiteFilterCommand());
            composite.Execute();
        }
Example #4
0
        public static ICommand Then(this ICommand left, ICommand right)
        {
            var command = new CompositeCommand();

            command.Add(left);
            command.Add(right);
            return(command);
        }
Example #5
0
        static void TestCompositeCommand()
        {
            var composite = new CompositeCommand();

            composite.Add(new ResizeCommand());
            composite.Add(new BlackAndWhiteCommand());

            composite.Execute();
        }
Example #6
0
        public static void Behavioral_CommandComposite()
        {
            // This is a composite command
            var composite = new CompositeCommand();

            composite.Add(new ResizeCommand());
            composite.Add(new BlackAndWhiteCommand());
            composite.Execute();
        }
Example #7
0
        public void TestComposite_PassParameter()
        {
            bool   command1Called = false;
            string command1Result = "";
            string checkValue     = "";

            var composite = new CompositeCommand();

            composite.Add(new DelegateCommand <string>((value) =>
            {
                command1Result = value;
                command1Called = true;
            },
                                                       (value) =>
            {
                checkValue = value;
                return(true);
            }));

            if (composite.CanExecute("Ok"))
            {
                composite.Execute("Ok");
            }

            Assert.IsTrue(command1Called);
            Assert.AreEqual("Ok", command1Result);
            Assert.AreEqual("Ok", checkValue);
        }
Example #8
0
        public void TestComposite_ExecuteAllCommands()
        {
            bool command1Called = false;
            bool command2Called = false;

            var composite = new CompositeCommand();

            composite.Add(new DelegateCommand(() =>
            {
                command1Called = true;
            }));
            composite.Add(new DelegateCommand(() =>
            {
                command2Called = true;
            }));

            composite.Execute(null);

            Assert.IsTrue(command1Called);
            Assert.IsTrue(command2Called);
        }
Example #9
0
        private void MenuItemClickedOnVertex(object sender, EventArgs e)
        {
            var vertex = this.currentVertex.GetDataVertex <NodeViewModel>();
            var edges  = this.Graph.DataGraph.Edges.ToArray();

            var edgesToRestore = this.graphArea.EdgesList.ToList()
                                 .Where(x => x.Key.Source == vertex || x.Key.Target == vertex)
                                 .Select(x => Tuple.Create(x.Key.Edge, this.ConvertIntoWinPoint(x.Key.RoutingPoints)));

            var command = new CompositeCommand();

            foreach (var edge in edges)
            {
                if (edge.Source == vertex || edge.Target == vertex)
                {
                    command.Add(new RemoveEdgeCommand(this.model, edge.Edge));
                }
            }

            command.Add(new RemoveNodeCommand(this.model, vertex.Node));
            this.controller.Execute(command);
            this.DrawGraph();
        }
Example #10
0
        public void TestComposite_Check_ReturnsFalse()
        {
            bool command1Called = false;
            bool command2Called = false;
            bool isCheck1       = false;
            bool isCheck2       = false;

            var composite = new CompositeCommand();

            composite.Add(new DelegateCommand(() =>
            {
                command1Called = true;
            }, () =>
            {
                isCheck1 = true;
                return(false);
            }));
            composite.Add(new DelegateCommand(() =>
            {
                command2Called = true;
            }, () =>
            {
                isCheck2 = true;
                return(false);
            }));

            if (composite.CanExecute(null))
            {
                composite.Execute(null);
            }

            Assert.IsFalse(command1Called);
            Assert.IsFalse(command2Called);
            Assert.IsTrue(isCheck1);
            Assert.IsFalse(isCheck2);
        }
Example #11
0
        public void TestComposite_WithParameter_ExecuteAllCommands()
        {
            bool   command1Called = false;
            bool   command2Called = false;
            string command1Result = "";

            var composite = new CompositeCommand();

            composite.Add(new DelegateCommand <string>((value) =>
            {
                command1Result = value;
                command1Called = true;
            }));
            composite.Add(new DelegateCommand(() =>
            {
                command2Called = true;
            }));

            composite.Execute("Ok");

            Assert.IsTrue(command1Called);
            Assert.IsTrue(command2Called);
            Assert.AreEqual("Ok", command1Result);
        }
Example #12
0
        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();
        }