preface
Unlike the server, Android does not have a fixed organizational structure. Given the componentization of engineering, it is necessary for us to master common programming routines, such as MVC, MVP, THE ultimate purpose of MVVM is to cultivate our code splitting ability and engineering modularity ability, today I will show you my understanding of Android code used code routines
MVC MVP MVVM project structure diagram
MVC MVP MVVM schematic diagram
MVC and MVP understanding
- We think that MVC is wrong. The concept of MVC was put forward in Java and experienced deeply in Strus2. In client development, The separation of V and C is not clear, but MVP Presenter and Model View achieve a real sense of isolation, Android MVC is not strictly MVC, but MVP is the Java concept of MVC
- The MVP removed the controller or scheduler
- MVC is taking the View out
- The essential directory structure is the same, one scheduling source, one display source, and one data manipulation source
Model
public class DataCenter {
public static String[] getData() {
return new String[]{"111"."222"}; }}Copy the code
view
public class MVCActivity extends AppCompatActivity {
private IView mDataView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDataView = (IView) findViewById(R.id.show_data);
String[] data = DataCenter.getData();
mDataView.showData(data[0], data[1]);
}
public interface IView {
void showData(String datanum0, String datanum1); }}Copy the code
controller
public class DataView extends ConstraintLayout implements MVCActivity.IView {
TextView title0;
TextView title1;
public DataView(Context context) {
super(context);
}
@Override
protected void onFinishInflate(a) {
super.onFinishInflate();
title0 = (TextView) findViewById(R.id.tv_title0);
title1 = (TextView) findViewById(R.id.tv_title1);
}
@Override
public void showData(String datanum0, String datanum1) { title0.setText(datanum0); title1.setText(datanum1); }}Copy the code
- MVC is the same as MVP,MVP is the same as MVC
Presenter
public class Presenter {
IView mIView;
public Presenter(IView view) {
this.mIView = view;
}
public void load(a) {
final String[] data = DataCenter.getData();
mIView.showData(data[0], data[1]);
}
public interface IView {
void showData(String data0, String data1); }}Copy the code
View
public class MVPActivity extends AppCompatActivity implements Presenter.IView {
private TextView mTitle0;
private TextView mTitle1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] data = DataCenter.getData();
mTitle0 = (TextView) findViewById(R.id.tv_title0);
mTitle1 = (TextView) findViewById(R.id.tv_title1);
new Presenter(this).load();
}
@Override
public void showData(String datum0, String datum1) { mTitle0.setText(datum0); mTitle1.setText(datum1); }}Copy the code
-
Architectural advantages and implementation of MVC and MVP:
The independence of logic does not interfere with each other, does not entangle each other, and the essential difference is that the Controller and the Prensenter are removed
-
MVVM
-
MVVM added a two-way binding MVP program in which the data is divided into the following three categories:
- External data: database, network data, data in local files
- Memory data: Memory data, such as Java variables
- Presentation data: Data presented on a page
-
Two-way binding is to update external data and memory data in real time. When my memory data is updated, my interface data is also updated
-
MVVM can also associate and update external memory data, in-memory data, and presentation data. DataBinding is a best practice
-
MVC and MVP architecture attributes are relatively strong,MVVM is like a library, to achieve timely update of data, he is essentially a listener model
@RequiresApi(api = Build.VERSION_CODES.KITKAT) public class ViewModel { StringAttr datum0 = new StringAttr(); StringAttr datum1 = new StringAttr(); ViewModel(ViewBinder binder, EditText editText0, EditText editText1) { binder.bind(editText0, datum0); binder.bind(editText1, datum1); } public void load(a) { String[] data = DataCenter.getData(); datum0.setValue(data[0]); datum1.setValue(data[1]); } static class StringAttr { public interface OnChangeListener{ void onChange(String newValue); } private String value; OnChangeListener mListener; public String getValue(a){ return value; } public void setOnChangeListener(OnChangeListener listener) { this.mListener = listener; } public void setValue(String value){ this.value = value; System.out.println("I changed it passively:"+value); if(mListener! =null){ mListener.onChange(value); }}}}Copy the code
view
@RequiresApi(api = Build.VERSION_CODES.KITKAT) public class ViewBinder { public void bind(final EditText editText, final ViewModel.StringAttr datum) { editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}@Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}@Override public void afterTextChanged(Editable editable) { if(! Objects.equals(editable.toString(), datum.getValue())) { datum.setValue(editable.toString()); }}}); datum.setOnChangeListener(new ViewModel.StringAttr.OnChangeListener() { @Override public void onChange(String newValue) { if(! Objects.equals(newValue, editText.getText().toString())) { editText.setText(newValue); }}}); }}Copy the code
-
ViewModel
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public class MVVMActivity extends AppCompatActivity {
private EditText mTitle0;
private EditText mTitle1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] data = DataCenter.getData();
mTitle0 = (EditText) findViewById(R.id.tv_title0);
mTitle1 = (EditText) findViewById(R.id.tv_title1);
new ViewModel(newViewBinder(), mTitle0, mTitle1).load(); }}Copy the code