我们开发过程中,经常用到MVC,MVP,MVVM等架构。MVC是基础,所以我们先来学习下什么是MVC模式。
我们开发过程中,经常用到MVC,MVP,MVVM等架构。MVC是基础,所以我们先来学习下什么是MVC模式。
MVC分为三部分
M(model)V(view)C(controller)
Model:负责数据读取,业务逻辑,状态保存等。
View:数据展示,用户交互
Controller:View层和Model层通过controller来进行连接。
MVC使用了策略模式。我们可以在Controller当中更换我们的model和view,来达到显示不同的数据和展示不同的UI。
在android当中,activity常常充当了controller和view。
我们写一个例子,用activity充当controller,用fragment来做view。我们可以使用策略模式来更换model和view。
Model
package com.yuanxuzhen.testandroid.mvc.bean;
import android.os.Parcel;
import android.os.Parcelable;
/**
* @创建人 YuanXuZhen
* @创建时间 10/11/21
* @描述
*/
public class UserInfo implements Parcelable {
public String name;
public long id;
public String sex;
public int age;
public UserInfo(String name, long id, String sex, int age) {
this.name = name;
this.id = id;
this.sex = sex;
this.age = age;
}
protected UserInfo(Parcel in) {
name = in.readString();
id = in.readLong();
sex = in.readString();
age = in.readInt();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeLong(id);
dest.writeString(sex);
dest.writeInt(age);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<UserInfo> CREATOR = new Creator<UserInfo>() {
@Override
public UserInfo createFromParcel(Parcel in) {
return new UserInfo(in);
}
@Override
public UserInfo[] newArray(int size) {
return new UserInfo[size];
}
};
@Override
public String toString() {
return "UserInfo{" +
"name='" + name + '\'' +
", id=" + id +
", sex='" + sex + '\'' +
", age=" + age +
'}';
}
}
?
package com.yuanxuzhen.testandroid.mvc;
import com.yuanxuzhen.testandroid.mvc.bean.UserInfo;
/**
* @创建人 YuanXuZhen
* @创建时间 10/11/21
* @描述
*/
public interface BaseModel {
UserInfo getUserInfo();
}
package com.yuanxuzhen.testandroid.mvc.model;
import com.yuanxuzhen.testandroid.mvc.BaseModel;
import com.yuanxuzhen.testandroid.mvc.bean.UserInfo;
/**
* @创建人 YuanXuZhen
* @创建时间 10/11/21
* @描述
*/
public class YuanpingModel implements BaseModel {
@Override
public UserInfo getUserInfo() {
UserInfo info = new UserInfo("Yuanping", 2, "female", 32);
return info;
}
}
package com.yuanxuzhen.testandroid.mvc.model;
import com.yuanxuzhen.testandroid.mvc.BaseModel;
import com.yuanxuzhen.testandroid.mvc.bean.UserInfo;
/**
* @创建人 YuanXuZhen
* @创建时间 10/11/21
* @描述
*/
public class YuanxuzhenModel implements BaseModel {
@Override
public UserInfo getUserInfo() {
UserInfo info = new UserInfo("Yuanxuzhen", 1, "male", 35);
return info;
}
}
view
package com.yuanxuzhen.testandroid.mvc.view;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.yuanxuzhen.base.BaseActivity;
import com.yuanxuzhen.base.ThreadUtils;
import com.yuanxuzhen.testandroid.databinding.ActivityUserInfoBinding;
import com.yuanxuzhen.testandroid.mvc.BaseModel;
import com.yuanxuzhen.testandroid.mvc.model.YuanpingModel;
import com.yuanxuzhen.testandroid.mvc.model.YuanxuzhenModel;
public class UserInfoActivity extends BaseActivity {
ActivityUserInfoBinding binding;
BaseModel mModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityUserInfoBinding.inflate(getLayoutInflater());
mModel = new YuanpingModel();
setContentView(binding.getRoot());
binding.getRoot().post(new Runnable() {
@Override
public void run() {
Bundle bundle = new Bundle();
bundle.putParcelable("user_info", mModel.getUserInfo());
showFragment(UserFragment2.class,
UserFragment1.class.getName(),
false,
bundle
);
}
});
}
}
?
package com.yuanxuzhen.testandroid.mvc.view;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.yuanxuzhen.testandroid.R;
import com.yuanxuzhen.testandroid.databinding.FragmentUser1Binding;
import com.yuanxuzhen.testandroid.mvc.bean.UserInfo;
/**
* A simple {@link Fragment} subclass.
* Use the {@link UserFragment1#newInstance} factory method to
* create an instance of this fragment.
*/
public class UserFragment1 extends Fragment {
private static final String USER_INFO = "user_info";
// TODO: Rename and change types of parameters
private UserInfo userInfo;
public UserFragment1() {
// Required empty public constructor
}
public static UserFragment1 newInstance(UserInfo data) {
UserFragment1 fragment = new UserFragment1();
Bundle args = new Bundle();
args.putParcelable(USER_INFO, data);
fragment.setArguments(args);
return fragment;
}
private UserInfo mUserInfo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mUserInfo = getArguments().getParcelable(USER_INFO);
}
}
FragmentUser1Binding binding;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
binding = FragmentUser1Binding.inflate(inflater);
binding.tvUserinfo.setText(mUserInfo.toString());
return binding.getRoot();
}
}
?
package com.yuanxuzhen.testandroid.mvc.view;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import com.yuanxuzhen.testandroid.databinding.FragmentUser1Binding;
import com.yuanxuzhen.testandroid.databinding.FragmentUser2Binding;
import com.yuanxuzhen.testandroid.mvc.bean.UserInfo;
/**
* A simple {@link Fragment} subclass.
* Use the {@link UserFragment2#newInstance} factory method to
* create an instance of this fragment.
*/
public class UserFragment2 extends Fragment {
private static final String USER_INFO = "user_info";
// TODO: Rename and change types of parameters
private UserInfo userInfo;
public UserFragment2() {
// Required empty public constructor
}
public static UserFragment2 newInstance(UserInfo data) {
UserFragment2 fragment = new UserFragment2();
Bundle args = new Bundle();
args.putParcelable(USER_INFO, data);
fragment.setArguments(args);
return fragment;
}
private UserInfo mUserInfo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mUserInfo = getArguments().getParcelable(USER_INFO);
}
}
FragmentUser2Binding binding;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
binding = FragmentUser2Binding.inflate(inflater);
binding.tvUserinfo.setText(mUserInfo.toString());
return binding.getRoot();
}
}
Controller
package com.yuanxuzhen.testandroid.mvc.view;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.yuanxuzhen.base.BaseActivity;
import com.yuanxuzhen.base.ThreadUtils;
import com.yuanxuzhen.testandroid.databinding.ActivityUserInfoBinding;
import com.yuanxuzhen.testandroid.mvc.BaseModel;
import com.yuanxuzhen.testandroid.mvc.model.YuanpingModel;
import com.yuanxuzhen.testandroid.mvc.model.YuanxuzhenModel;
public class UserInfoActivity extends BaseActivity {
ActivityUserInfoBinding binding;
BaseModel mModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityUserInfoBinding.inflate(getLayoutInflater());
mModel = new YuanpingModel();
setContentView(binding.getRoot());
binding.getRoot().post(new Runnable() {
@Override
public void run() {
Bundle bundle = new Bundle();
bundle.putParcelable("user_info", mModel.getUserInfo());
showFragment(UserFragment2.class,
UserFragment1.class.getName(),
false,
bundle
);
}
});
}
}
我们可以在Controller部分随意的更换model与view。达到view和model的分离。
gitee地址
https://gitee.com/creat151/yuanxuzhen_test_android.git
|