MvcBind

introduce

MvcBind fast development framework, based on MVC + Databinding for the production of lightweight fast development foundation framework. There is only one goal: fast! Fast to work, fast to understand, fast to develop, fast to maintain.

Software architecture

The software architecture specification framework is based on the classic MVC pattern, why not MVP or MVVM? The author also has a lot of project practice of the latter two, and found that the development speed of the latter two is relatively slow (compared with MVC) in the actual use, especially MVP, which adopts interface decoupling, and needs to write more classes and declare more interface methods in the development, and then implement these methods, resulting in poor development efficiency. Of course, there is no denying that MVP and MVVM are better than traditional MVC mode in terms of post-maintenance. At that time, this advantage only exists when MVC is not written in a standardized way. In fact, the maintenance of normalized MVC mode code is not much worse than the latter two. And the fact that most people who are new to Android aren’t really MVC in the strict sense, why is that? Let’s look at the interaction diagram of MVC:

public class TestActivity extends Activity {
    private TextView mTextView;
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = findViewById(R.id.tv_date); mTextView.setText(System.currentTimeMillis()); }}Copy the code

Here is just a simple example. Compared with the above figure, it can be found that most people’s so-called MVC does not even have a model module class. All views, data and logic codes are stacked in activities or fragments. Simply speaking, the MVC in Android should also be composed of layout (View), activity(Control), data class (Model) this three is right, in fact, if you write so, obviously better maintenance than the above code. However, this is not enough, because android layout XML file has few functions, a lot of things can not be done in XML, which causes you have to write in the activity(Control), which will cause the division of view and control confusion, so how to solve this problem? MvcBind adopts the core idea of MvcBind mode, and makes clear the division of labor. When writing a page, there are three modules: Model (data class), Control (Activity or fragment), and View (layout XML + View class). The difference between MvcBind and MvcBind is that the VIEW consists of XML and a separate view control class. The difference between MvcBind and MVP is that MvcBind does not have interface decoupling. MVP interface decoupling mechanism is too redundant, and in terms of the effect mainly depends on the user’s skill, no matter how good the MVP framework is in the hands of beginners, it is a mess, so this library directly removed the INTERFACE decoupling mechanism in MVP, but retains the BASIC idea of MODULE decoupling of MVP. Also, in order to reduce template code, the databinding mechanism has been introduced so that no findView code is required in the View class. First, if we need to create an activity, using this framework you just need to:

After these two steps you will have:

As shown, in just two simple steps you can get MVC with three classes and a layout file, showing the initial code in four files:

public class TestActivityControl extends BaseActivity<TestActivityModel.TestActivityView> {
    @Override
    public void init (Bundle savedInstanceState) {}}public class TestActivityModel extends BaseModel {
    @Override
    public void init (IControl control) {}}public class TestActivityView extends BaseView<ActivityTestBinding> {
    @Override
    public void init (IControl control) { mBind.setControl(control); }}Copy the code
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
>
    <data>
        <variable name="control" type="com.xujl.fastlib.base.IControl"/>
    </data>
    <! -- -->
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical">

    </LinearLayout>
</layout>
Copy the code

In just two steps you can start writing your own business code without having to write as many classes and interfaces as you would write an MVP. As template of course, you do not need to care about how to implement the above function, because this library has provided templates, located in the repository/tools directory, just need to copy to the As corresponding directory to restart, refer to the specific usage instructions.

Install the tutorial

Project address: gitee.com/biyouji_adm…

Directions for use

Basic functions: 1. Download the project source code to local 2. Configure your project root build.gradle file (refer to the downloaded source code) 3. Import Fastlib into your project and add it as a dependency module for your project

Additional functions: 1. Activity/Fragment Template Location: source directory/tool/Fast generation class template Specific instructions refer to the Word document in the folder 2. What is the BIND data class? We all know that dataBinding requires a data entity to bind, and there are usually two ways to do this. One is to use ObservableField to wrap the fields, but this method is very cumbersome, and our entities are usually derived from JSON. Json parsing certainly does not support the ObservableField type, as follows:

 public ObservableField<String> name = new ObservableField<>();
Copy the code

The other is that the entity class inherits from the BaseObservable class, and fields that need to be bound need to be treated as follows:

    private String name;
    @Bindable
    public String getName (a) {
        return name;
    }

    public void setName (String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }
Copy the code

This may not be as convenient as the first method, but it has the advantage of being able to parse json and bind it directly to the view without having to set the value again. But a large number of fields, adding these things over and over again, is still a waste of time, and this is where plug-ins come in. Dbsetter. jar is a plugin for the dbsetter. jar is a plugin for the dbsetter. jar is a plugin for the DbSetter.


Why fast?

1. Why do you do it quickly? Because the familiar MVC pattern is used first, there is no need for too much extra learning cost. The biggest learning cost of this framework should be the understanding of data binding, but this is much simpler than MVP and MVVM. 2 understand the fast framework source code is simple, there are not too many bells and whistles, as long as willing to spend a half a day or so, you can be at a glance at the overall design ideas. 3. Fast development of template based activity/fragment creation and automatic generation of setter/getter bind data class, can save a lot of first development time, do not look down on this part of the creation time, a new project generally 30 activities,10 or so fragments. If you estimate 5 minutes to create activity-related classes and layouts based on the framework, a project will take about 200 minutes to write the template code alone, which is conservative, but the actual time may be much longer. If you think that rapid development is only the tip of the iceberg, you are wrong. The following details how this library can achieve rapid development: No need to write the layout ID, the following code is probably common in various frameworks:

    @Override
    public int initContentView (Bundle savedInstanceState) {
        return R.layout.activity_card_goods_list;
    }
Copy the code

Method name, of course, all kinds of framework may have some discrepancy, but the base is the same, the subclass pass layout id to the base class for loading layout, do it right, but to write more, tired of this code, if you did not use template to automatically generate more vexed, for every new page takes 2 seconds of time to write the code of nutrition, So how does that work in this library?

public class LoadingFragmentView extends BaseView<FragmentLoadingBinding> {
    @Override
    public void init (IControl control) {}}Copy the code

You will notice that no layout ID is returned, but the runtime page can accurately load the layout r.layout. fragment_loading. In general, the implementation of this function is based on two points: 1. Standardized naming. There are strict requirements on naming the THREE MVC modules and layout. For example, LoadingFragmentView must correspond to LoadingFragmentControl, LoadingFragmentModel, r.layout. fragment_loading. Of course, you don’t have to worry about naming errors, because these files are generated by templates. 2. Reflection, with standardized naming, we can easily get the class name in the base class by reflection, and then splice the layout file ID through the class name, and then load the layout! Multiple functional examples for reference



A variety of development mode switch, you happy good






No problem!


public class ActivityModeFragmentControl extends BaseFragment {
    @Override
    public void init (Bundle savedInstanceState) {}}Copy the code

Is the page design too complex, or for various reasons, unable or unwilling to use Databinding? No problem!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" Android :layout_height="wrap_content" Android :text=" this page is not using dataBinding" /> </LinearLayout> public class NoBindingFragmentView extends BaseView { @Override public LayoutBuilder configLayout () { return super.configLayout().setEnableDataBinding(false); } @Override public void init (IControl control) { } }Copy the code

You don’t want to use binding, you don’t want to use MVC, you just want to use the simplest acivity/fragment, right? No problem!

public class SimpleModeFragmentControl extends BaseFragment {
    @Override
    protected BaseView createDefaultView (a) {
        return new BaseView() {
            @Override
            public void init (IControl control) {}@Override
            public LayoutBuilder configLayout (a) {
                return super.configLayout().setEnableDataBinding(false); }}; }@Override
    public void init (Bundle savedInstanceState) {}}Copy the code

Of course, these are the modes supported by the library, but it is highly undesirable to turn off databinding in general.

Third party libraries involved in the project (list only fastlib dependent libraries)

  1. The thread pool RxTask
  2. Refresh SmartRefreshLayout
  3. Message bus EventBus
  4. BaseRecyclerViewAdapterHelper
  5. Rxjava
  6. retrofit
  7. logger
  8. fragmentation
  9. rxpermissions
  10. Debugging tool DoraemonKit
  11. Popover base class BasePopup

Documentation continues to be updated…