MVC the common architecture of getting started with Android

Source: Internet
Author: User

Android Basics-Framework pattern MVC in wirelessly practice

This article contains the following content:

  • Introduction to MVC
  • The practice of MVC
Introduction to MVC

MVC (Model View Controller), an abbreviation for the Models View Controllers (control), a software design pattern for organizing code to be separated by a functional module and a data module

  • Model: The modeling layer, which handles the loading or storage of data
  • View: Views layer, responsible for the display of interface data, interaction with the user
  • Controller: Control layer, responsible for the processing of logic business

Model Diagrams for MVC:

The basic process of MVC design execution:

First, the view accepts the user input request, then passes the request to the controller, the controller calls a model to process the user's request, under the control of the controller, and then gives the processed result to a view to format the output to the user. In addition, view is a direct access to the model for data processing.

An understanding of MVC:

In MVC, view is directly accessible to the model. As a result, the view contains the model information and inevitably includes some business logic. In the MVC model, more attention is paid to the constant of the model, while there are several different displays of the model, and the view. So, in the MVC model, models do not depend on view, but view is dependent on model. Not only that, because there are some business logic implemented in the view, it is difficult to change the view, at least those business logic is not reusable

MVC Benefits:

  • Low coupling resistance
  • High reusability
  • Low life cycle costs
  • Deploy fast
  • High maintainability
  • Advantageous Software Engineering Management

MVC drawbacks:

  • No definite definition.
  • Not suitable for small, medium-sized applications
  • Increase the complexity of the system structure and implementation
  • An overly tight connection between a view and a controller
  • View inefficient access to model data
  • General Advanced Interface tool or constructor does not support mode
The practice of MVC

After we understand what MVC is, we use a small case to get a deeper understanding

We use the ListView to demonstrate our MVC pattern, the directory structure:

Entity class: Contains the name and picture information of the book

public class Book {    //title    private String name;    Picture of the book    private int image;    Public book (String name, int image) {        this.name = name;        This.image = image;    }    Public String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }    public int getImage () {        return image;    }    public void setimage (int image) {        this.image = image;    }}

  

Model classes: Typically operations on a local database or access to network data through a network request we simulated a local database in model and provided a method for adding and removing changes.

  

public class Bookmodel {    private static list<book> List = new arraylist<> ();    /**     * Emulate Local database */    static {        List.add (new book ("Java from beginner to proficient", R.drawable.java));        List.add ("Android from Beginner to Proficient", r.drawable.android);        List.add (new book ("Java from beginner to proficient", R.drawable.java));        List.add ("Android from Beginner to Proficient", r.drawable.android);    }    /**     * Add book     * @param name     * @param image     *    /public void Addbook (String name, int image) {        Li St.add (new book (name, image));    }    /**     * Delete Books */public    void Deletebook () {        list.remove (list.size ()-1);    }    /**     * Query Database All books     * @return     *    /Public list<book> query () {        return List;    }}

Controller layer: According to the model layer method, plus our business logic processing, externally provide methods and expose the interface
See delete This method, determine whether the list is empty (business logic), using Mode.deletebook () (model layer method), via Listener.oncomplete () (Exposed interface)

public class Bookcontroller {private Bookmodel mode;    Public Bookcontroller () {mode = new Bookmodel (); }/** * Add book * @param listener */public void Add (Onaddbooklistener listener) {Mode.addbook ("Jav        Aweb from beginner to Proficient ", r.drawable.javaweb);        if (listener! = null) {listener.oncomplete (); }}/** * Delete book * @param listener */public void Delete (Ondeletebooklistener listener) {if (mod        E.query (). IsEmpty ()) {return;        }else{Mode.deletebook ();        } if (listener! = null) {listener.oncomplete ();    }}/** * Query all Books * @return */public list<book> query () {return mode.query ();    }/** * Add a successful callback interface */public interface Onaddbooklistener {void OnComplete ();    }/** * Delete the successful callback interface */public interface Ondeletebooklistener {void OnComplete (); }}

View Layer: We manipulate the controller to get the list data populated into the ListView, and can add books and delete books

public class Bookactivity extends Appcompatactivity implements View.onclicklistener {private Bookcontroller Bookcontro    Ller;    Private ListView Lv_book;    Private list<book> List;    Private Bookadapter adapter;    Private Button Bt_add, Bt_delete;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_book);        Lv_book = (ListView) Findviewbyid (r.id.lv);        Bt_add = (Button) Findviewbyid (R.id.bt_add);        Bt_delete = (Button) Findviewbyid (r.id.bt_delete);        Bt_add.setonclicklistener (this);        Bt_delete.setonclicklistener (this);        Bookcontroller = new Bookcontroller ();        List = Bookcontroller.query ();        adapter = new Bookadapter (this, list);    Lv_book.setadapter (adapter); } @Override public void OnClick (View v) {switch (V.getid ()) {//Add Book button case R.id.bt_ad D:bookcontroller.add (New BOOKCOntroller.onaddbooklistener () {@Override public void OnComplete () {                    Adapter.notifydatasetchanged ();                }                });            Break                    Delete Book button case R.id.bt_delete:bookcontroller.delete (new Bookcontroller.ondeletebooklistener () { @Override public void OnComplete () {Adapter.notifydatasetcha                    Nged ();                }                });        Break }    }}

  

Disclaimer: This article is reproduced in http://blog.csdn.net/qq_30379689/article/details/52909656

MVC the common architecture of getting started with Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.