Ejemplo n.º 1
0
        private static void Main()
        {
            // Application_A
            var service = new CustomerService();
            var command = new AddCustomerCommand(service);
            var button  = new Button(command);

            button.Click();

            // Application_B
            var composite = new CompositeCommand();

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

            composite.Execute();

            // Application_C
            var history  = new History();
            var document = new HtmlDocument
            {
                Content = "Hello World"
            };

            var boldCommand = new BoldCommand(document, history);

            boldCommand.Execute();
            System.Console.WriteLine(document.Content); // <b>Hello World</b>

            var undoCommand = new UndoCommand(history);

            undoCommand.Execute();
            System.Console.WriteLine(document.Content); // Hello World
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        static void TestUndoableCommand()
        {
            var history  = new Command.Editor.History();
            var document = new Command.Editor.HtmlDocument();

            document.Content = "Hello World";

            var boldCommand = new BoldCommand(document, history);

            boldCommand.Execute();

            System.Console.WriteLine(document.Content);

            document.Content = "This is Command Pattern";
            boldCommand      = new BoldCommand(document, history);
            boldCommand.Execute();

            System.Console.WriteLine(document.Content);

            var undoCommand = new UndoCommand(history);

            undoCommand.Execute();

            System.Console.WriteLine(document.Content);

            undoCommand.Execute();
            System.Console.WriteLine(document.Content);
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var history  = new History();
            var document = new HtmlDocument();

            document.Content = "Hello World";
            var boldCommand = new BoldCommand(document, history);

            boldCommand.Execute();
            Console.WriteLine(document.Content);
            var undoCommand = new UndoCommand(history);

            undoCommand.Execute();
            Console.WriteLine(document.Content);
            Console.ReadKey();

            //TODO: add more commands here
            var service = new CustomerService();
            var command = new AddCustomerCommand(service);
            var button  = new Button(command);

            button.Click();
            Console.ReadKey();

            var composite = new CompositeCommand();

            composite.Add(new ResizeCommand());
            composite.Add(new BlackAndWhiteCommand());
            composite.Execute();
            Console.ReadKey();
        }
Ejemplo n.º 5
0
        private static void UndoableCommandPattern()
        {
            var document = new Document {
                Content = "Some content"
            };

            Console.WriteLine("Before");
            Console.WriteLine(document.Content);

            var history     = new CommandHistory();
            var boldCommand = new BoldCommand(document, history);
            var button      = new Button(boldCommand);

            button.Click();

            Console.WriteLine("After");
            Console.WriteLine(document.Content);


            Console.WriteLine("After Undo");
            var undoCommand = new UndoCommand(history);

            undoCommand.Execute();

            Console.WriteLine(document.Content);
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            var addCustomerCommand = new AddCustomerCommand(new CustomerService());
            var button             = new Button(addCustomerCommand);

            button.Click();

            // Because we are representing each individual task using a command object,
            // we can combine these commands inside a composite object and re-execute them later on.
            var compositeCommand = new CompositeCommand();

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

            compositeCommand.Execute();

            var history      = new Solution.UndoableCommands.History();
            var htmlDocument = new HtmlDocument();

            htmlDocument.Content = "Hello World!";

            var boldCommand = new BoldCommand(htmlDocument, history);

            boldCommand.Execute();

            Console.WriteLine(htmlDocument.Content);

            //boldCommand.Unexecute();
            var undoCommand = new Solution.UndoableCommands.UndoCommand(history);

            undoCommand.Execute();

            Console.WriteLine(htmlDocument.Content);

            Console.WriteLine("--- Exercise ---");

            var exeHistory  = new Exercise.End.History();
            var videoEditor = new VideoEditor();

            var setTextCommand = new SetTextCommand("Hello World!", videoEditor, exeHistory);

            setTextCommand.Execute();

            Console.WriteLine(videoEditor.Text);

            //setTextCommand.Undo();
            var exeUndoCommand = new Exercise.End.UndoCommand(exeHistory);

            exeUndoCommand.Execute();

            Console.WriteLine(videoEditor.Text);

            Console.ReadLine();
        }
Ejemplo n.º 7
0
        public CommandPatternMain()
        {
            var boldCommand = new BoldCommand(_htmlDocument, _history);
            var undoCommand = new UndoCommand(_history);

            _htmlDocument.Content = "Hello World!";

            Console.WriteLine(_htmlDocument.Content);


            boldCommand.Execute();
            Console.WriteLine(_htmlDocument.Content);

            undoCommand.Execute();
            Console.WriteLine(_htmlDocument.Content);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Properties initializer
 /// </summary>
 private void Init()
 {
     notes            = new ObservableCollection <NoteModel>();
     notesForLV       = new ObservableCollection <NoteModel>();
     SaveCommand      = new SaveCommand(this);
     AddCommand       = new AddCommand(this);
     EditCommand      = new EditCommand(this);
     CancelCommand    = new CancelCommand(this);
     DeleteCommand    = new DeleteCommand(this);
     ExitCommand      = new ExitCommand(this);
     FontIncrease     = new FontIncrease(this);
     FontDecrease     = new FontDecrease(this);
     BoldCommand      = new BoldCommand(this);
     ItalicCommand    = new ItalicCommand(this);
     UnderlineCommand = new UnderlineCommand(this);
     EditMode         = false;
     ReadOnly         = true;
 }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            History      history  = new History();
            HtmlDocument document = new HtmlDocument();

            document.Content = "Hello World";

            BoldCommand boldCommand = new BoldCommand(document: document, history);

            boldCommand.Execute();
            Console.WriteLine(document.Content);

            UndoCommand undoCommand = new UndoCommand(history);

            undoCommand.Execute();

            Console.WriteLine(document.Content);
        }
Ejemplo n.º 10
0
        public static void Run()
        {
            var history  = new CommandsHistory();
            var document = new HtmlDocument();

            document.Content = "Hello World!";

            var boldCommand = new BoldCommand(document, history);

            boldCommand.Execute();

            Console.WriteLine(document.Content);

            var undoCommand = new UndoCommand(history);

            undoCommand.Execute();

            Console.WriteLine(document.Content);
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            // decoupling the customer service from the button
            // otherwise all code has to be retained in the button

            var service = new CustomerService();
            var command = new AddCustomerCommand(service);
            var button  = new Button(command);

            button.Click();

            var composite = new CompositeCommand();

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


            var history  = new History();
            var document = new HtmlDocument {
                Content = "Hello World"
            };

            var bold = new BoldCommand(document, history);

            bold.Execute();
            Console.WriteLine(document.Content);

            var undoCommand = new UndoCommand(history);

            undoCommand.Execute();

            Console.WriteLine(document.Content);
        }
Ejemplo n.º 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();
        }