2
\$\begingroup\$

I'm learning about the MVP and ICommand design patterns as I'd like to use them in my own projects. I've cobbled this very simple WinForm application together to hopefully demonstrate that I'm using the patterns in the right way.

I was wondering if someone who is more familiar with both patterns could eye ball this project I stuck on GitHub to check that I'm at least heading in the right direction. https://github.com/sulphy/PatternExample

The project simply consist of a view, its associated presenter and a message box command that I attach to a button on the view. The objects are instantiated in program.cs.

Program.cs

  var formObject = new ExampleView();
  var commandObject = new SimpleMessageBoxCommand(formObject);
  var presenterObject = new ExamplePresenter(formObject, commandObject);
  Application.Run(formObject);

ExampleView.cs

  public partial class ExampleView : Form, IExampleView
    {
        public ExampleView()
        {
            InitializeComponent();
        }

        public Button SubmitButton()
        {
            return this.btnSubmit;
        }

        public TextBox ViewTextBox()
        {
            return this.txtFreetext;
        }

        public void SetViewTitle(string titleText)
        {
            this.Text = titleText;
        }
    }

    public interface IExampleView
    {
        Button SubmitButton();
        TextBox ViewTextBox();
        void SetViewTitle(string titleText);
    }

ExamplePresenter:

 public class ExamplePresenter
    {
        private IExampleView exampleView;
        private ICommand command;

        public ExamplePresenter(IExampleView exampleView, ICommand command)
        {
            this.exampleView = exampleView;
            this.command = command;

            exampleView.SetViewTitle( "An Example Form");

            exampleView.SubmitButton().Click += new EventHandler(SubmitButton_OnClick);
        }

        public void SubmitButton_OnClick(object sender, EventArgs e)
        {
            command.Execute();
        }
    }

SimpleMessageBoxCommand.cs

  public class SimpleMessageBoxCommand : ICommand
    {
        private IExampleView exampleView;

        public SimpleMessageBoxCommand(IExampleView exampleView)
        {
            this.exampleView = exampleView;
        }

        public void Execute()
        {
            var textString = exampleView.ViewTextBox().Text;
            MessageBox.Show(textString);
            exampleView.ViewTextBox().Focus();
        }
    }
\$\endgroup\$
  • \$\begingroup\$ You need to include the code in the question. A link to a repository isn't enough. \$\endgroup\$ – t3chb0t Nov 20 '17 at 19:46
  • \$\begingroup\$ Thanks for the pointer, I've edited my post to include the main code snippets. \$\endgroup\$ – Sulphy Nov 20 '17 at 19:52
  • \$\begingroup\$ Welcome to Code Review! The current question title, which does little more than describe tha tags, applies to too many questions on this site to be useful. Please edit to the site standard, which is to state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles. \$\endgroup\$ – Toby Speight Nov 21 '17 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.