Xamarin.Android MVP模式

1、簡介

隨着UI建立技術的功能日益加強,UI層也履行着愈來愈多的職責。爲了更好地細分視圖(View)與模型(Model)的功能,讓View專一於處理數 據的可視化以及與用戶的交互,同時讓Model只關係數據的處理,基於MVC概念的MVP(Model-View-Presenter)模式應運而生。android

在MVP模式裏一般包含4個要素:git

    View:負責繪製UI元素、與用戶進行交互(在Android中體現爲Activity);
    View interface:須要View實現的接口,View經過View interface與Presenter進行交互,下降耦合,方便進行單元測試;
    Model:負責存儲、檢索、操縱數據(有時也實現一個Model interface用來下降耦合);
    Presenter:做爲View與Model交互的中間紐帶,處理與用戶交互的負責邏輯。
所謂的MVP,便是(model-處理業務邏輯(主要是數據讀寫,或者與後臺通訊(其實也是讀寫數據)),view-處理ui控件,presenter-主導器,操做model和view)

2、實例

1.新建一個項目,項目結構以下github

 

2.作一個根據id讀取數據的實例,界面以下數據庫

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <EditText
        android:id="@+id/id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="輸入id"
        android:inputType="number" />
    <EditText
        android:id="@+id/first"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="inter first name"
        android:inputType="text" />
    <EditText
        android:id="@+id/last"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="inter last name"
        android:inputType="text" />
    <Button
        android:id="@+id/save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="save" />
    <Button
        android:id="@+id/load"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="load" />
</LinearLayout>
View Code

3.創建beaniview

 public class UserBean
    {
        private String mFirstName { get; set; }
        private String mLastName { get; set; }
          
        public UserBean(String firstName, String lastName)
        {
            this.mFirstName = firstName;
            this.mLastName = lastName;
        }
    }

4.創建model(處理業務邏輯),先寫接口,後寫實現ide

    public interface IUserModel
    {
        void SetID(int id);

        void SetFirstName(String firstName);

        void SetLastName(String lastName);

        int GetID();

        UserBean Load(int id);// 經過id讀取user信息,返回一個UserBean
    }
 public class UserModel : IUserModel
    {
        public int GetID()
        {
            return 0;
        }

        public UserBean Load(int id)
        {
            // 查數據庫或者聯網獲取id的userbean
            return new UserBean("11", "22");
        }

        public void SetFirstName(string firstName)
        {

        }

        public void SetID(int id)
        {

        }

        public void SetLastName(string lastName)
        {

        }
    }

 

5.創建view(更新ui中view狀態),這裏列出須要操做當前view的方法,也就是接口。單元測試

    public interface IUserView
    {
        int GetID();

        String GetFristName();

        String GetLastName();

        void SetFirstName(String firstName);

        void SetLastName(String lastName);
    }

6.創建presenter(主導器,經過ivew和imodel接口操做model和view),activity能夠把全部邏輯給presenter處理測試

 

 public class UserPresenter
    {
        private IUserView mUserView;
        private IUserModel mUserModel;

        public UserPresenter(IUserView view)
        {
            mUserView = view;
            mUserModel = new UserModel();
        }

        public void SaveUser(int id, String firstName, String lastName)
        {
            mUserModel.SetID(id);
            mUserModel.SetFirstName(firstName);
            mUserModel.SetLastName(lastName);
        }

        public void LoadUser(int id)
        {
            UserBean user = mUserModel.Load(id);
            mUserView.SetFirstName(user.mFirstName); // 經過調用IUserView的方法來更新顯示
            mUserView.SetLastName(user.mLastName);
        }
    }

7.activity中實現iview接口,在其中操做view,實例化一個presenter變量。ui

 public class MainActivity : Activity, IUserView, View.IOnClickListener
    {
        UserPresenter presenter;
        EditText id, first, last;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
             FindViewById<Button>(Resource.Id.save).SetOnClickListener(this);
             FindViewById<Button>(Resource.Id.load).SetOnClickListener(this); ;
            
            id = FindViewById<EditText>(Resource.Id.id);
            first = FindViewById<EditText>(Resource.Id.first);
            last = FindViewById<EditText>(Resource.Id.last);
            presenter = new UserPresenter(this);
        }

        public int GetID()
        {
            return Convert.ToInt32(id.Text.ToString());
        }

        public string GetFristName()
        {
            return first.Text.ToString();
        }

        public string GetLastName()
        {
            return last.Text.ToString();
        }

        public void SetFirstName(string firstName)
        {
            first.Text = firstName;
        }

        public void SetLastName(string lastName)
        {
            last.Text = lastName;
        }

        public void OnClick(View v)
        {
            switch (v.Id)
            {
                case Resource.Id.save:
                    presenter.SaveUser(GetID(), GetFristName(), GetLastName());
                    break;
                case Resource.Id.load:
                    presenter.LoadUser(GetID());
                    break;
            }
        }
    }

7.源碼地址this

 https://github.com/huguodong/XamarinMVP 

裏面又更詳細的實例

相關文章
相關標籤/搜索