On the question I asked earlier in the comments, they pointed out that MVC is being implemented for the android. On the Internet, I also studied this question and found out that Activity as it were, combines View and Controller in oneself. But there are also options with the implementation of MVP, however, it was all presented with options for Java. I decided to bring to your court both versions of one elementary program, which adds two numbers and displays the result.

Please look and rate what is better to choose:

let's start with MVC

this is a model (complex one)

 public class MainModel { public int ResultAdd { get; private set; } public void AddTwoNumbers(int firstNumber, int secondNumber) { ResultAdd = firstNumber + secondNumber; } } 

it is active

 public class MainActivity : Activity { //Model private Model.MainModel _Model; //UI private TextView _FirstNumber; private TextView _SecondNumber; private TextView _Result; private Button _AddButton; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView (Resource.Layout.Main); //Model _Model = new Model.MainModel(); //UI _FirstNumber = FindViewById<TextView>(Resource.Id.editTextFirst); _SecondNumber = FindViewById<TextView>(Resource.Id.editTextSecond); _Result = FindViewById<TextView>(Resource.Id.textViewResult); _AddButton = FindViewById<Button>(Resource.Id.buttonAdd); //Events _AddButton.Click += _AddButton_Click; } private void _AddButton_Click(object sender, System.EventArgs e) { int fn = int.Parse(_FirstNumber.Text); int sn = int.Parse(_SecondNumber.Text); //сложение _Model.AddTwoNumbers(fn, sn); //выводим результат _Result.Text = _Model.ResultAdd.ToString(); } } 

now the MVP model is the same

and this is a presenter

 public class MainPresenter { //все что нам нужно от интерфейса public interface IView { string FirstNumber { get; } string SecondNumber { get; } string ResultNumber { get; set; } event EventHandler AddEvent; } //экземпляр интерфейса private IView _View; //Model private MainModel _Model; //ctor public MainPresenter(IView view) { _View = view; //подписываемся на событие _View.AddEvent += _View_AddEvent; _Model = new MainModel(); } private void _View_AddEvent(object sender, EventArgs e) { int fn = int.Parse(_View.FirstNumber); int sn = int.Parse(_View.SecondNumber); //обращаемся к модели за результатом _Model.AddTwoNumbers(fn, sn); //выводим результат _View.ResultNumber = _Model.ResultAdd.ToString(); } } 

it is active

 public class MainActivity : Activity, MainPresenter.IView { //Presenter private MainPresenter _Presenter; //IView public string FirstNumber { get { return FindViewById<TextView>(Resource.Id.editTextFirst).Text; } } public string SecondNumber { get { return FindViewById<TextView>(Resource.Id.editTextSecond).Text; } } public string ResultNumber { get { return FindViewById<TextView>(Resource.Id.textViewResult).Text; } set { FindViewById<TextView>(Resource.Id.textViewResult).Text = value; } } public event EventHandler AddEvent = delegate { }; //запуск protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView (Resource.Layout.Main); //Presenter _Presenter = new MainPresenter(this); //Events FindViewById<Button>(Resource.Id.buttonAdd).Click += (s, e) => { AddEvent(this, EventArgs.Empty); }; } } 

I like MVP more. But confuses, that "to start" Presenter be from Activity . And it would be necessary from Main() that View was not connected with Presenter . But, as I understand it, the programs for the droid, as if there is no such method, and the Activity designated in the manifest as MainLauncher , MainLauncher , and in the event of screen flip, the last active activity restarts. What do you say about this?

  • one
    In theory, the presenter can be made a singleton and, if necessary, notify him of the calls to the methods of the life cycle of the activity. You can initialize when you first create an activity or when you call onCreate of the Application class - an analog of main - YuriySPb
  • @YuriSPb, thanks for the idea. - Bulson

0