"Add one" Project Summary-android use (3) MVC Mode

Source: Internet
Author: User

MVC (Model-View-Controller): M Refers to the logical Model, V refers to the View Model, and C refers to the Controller. A logical model can be used for multiple view Models
The purpose of using MVC is to separate the implementation code of M and V to facilitate expansion and facilitate future management.
From the developer's point of view, MVC completely separates the logic layer of the application from the interface. The biggest benefit is that the interface designer can directly participate in interface development, programmers can focus on the logic layer.
Although it can be achieved theoretically, in practice, it still seems that it cannot be completely separated...



In Android, we can also say that the popular MVC framework is adopted. In Android:

1) View layer (View): interface description is generally performed using XML files, which can be easily introduced during use. However, after being written in xml, it must be declared and instantiated in acitsag, it is a little troublesome. Consider whether you can implement matching by using similar annotations, or write a class to get the nodes of xml and then automatically encapsulate them. Of course, this is just an idea and will be implemented later.

2) control layer (Controller): the control layer of Android usually has many acitables and needs to be processed through the business logic layer of Activity delivery Model, another reason for doing so is that the response time of Acitivity in Android is 5 s. If time-consuming operations are put here, the program will be easily recycled.

3) Model layer: database operations and network operations should all be processed in the Model. Of course, operations such as business computing must also be placed in this layer.


This is my directory structure:

Code Description:

Model: divides the model package and dao package,

The model package mainly encapsulates objects and some simple object logic.

The dao package mainly refers to database operations and network operations.

MODEL:

public class User {private int id;private String realname;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getRealname() {return realname;}public void setRealname(String realname) {this.realname = realname;}public User(int id, String realname) {super();this.id = id;this.realname = realname;}public User() {super();}}


DAO:

Public class VoteDao extends BaseDao {public String login (String mac) {String [] para = {"user. mac "}; String [] data = {mac}; return getData (Cons. URL_LOGIN, para, data); // getData is a self-encapsulated network metrics class }}


View layer (View ):

For Android, It is the XML layout file. It is unwise to use code to implement android Layout

Control layer (Controller ):

The main task of the control layer of the activity Package and Android is usually on the shoulders of many acitables, which must be handled by the business logic layer of the Activity delivery Model. But all the control layer logic is written in the activity and it feels too bloated. A single activity contains hundreds of lines of code, which is inconvenient to maintain. Therefore, it is divided into a ctrler package.

Ctrler package: activity Package mainly implements logic processing with the view layer. ctrler package is more used for business logic processing with the model layer.

Adapter: Needless to say, implemented by androidAn important link between data and UI (View,Intuitively expresses the relationship among Data, Adapter, and View.

Activity: (mainly implement login from the server and get the notification to initialize listview)

Three steps (the layout is the same, three steps ):

I. initView (); view layout of the initial xml

2. getData (); get data

3. initAdapter (); initial data

Public class MainActivity extends Activity {public static SharedPreferences sharedPreferences; private ListView; private MenuAdapter nAdapter; // notification private List
 
  
Notices; private List
  
   
ShowingNotices = new ArrayList
   
    
(); Private Handler handler; private User user; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_main); sharedPreferences = PreferenceManager. getdefasharsharedpreferences (this); handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {switch (msg. what) {case OperCo Ns. INIT: initAdapter (); break; case OperCons. SHOW_TIPS: if (notices = null) MyToast. makeText (getApplicationContext (), "failed to get notification, please check network", Toast. LENGTH_SHORT); elseMyToast. makeText (getApplicationContext (), "no notification", Toast. LENGTH_SHORT); break; default: break ;}}; initView ();} private void initView () {// The list below listView = (ListView) findViewById (R. id. my_list); nAdapter = new MenuAdapter (getApplicationContext (), R. layout. ne Ws_item); listView. setAdapter (nAdapter); // cache, initialize String result = MainActivity. sharedPreferences. getString ("notice", null); if (result! = Null &&! "[]". Equals (result) {notices = JsonUtil. jsonToObjectList (result, Notice. class); if (notices! = Null & notices. size ()> 0) handler. sendEmptyMessage (OperCons. INIT);} user = VoteCtrler. getInstance (). login (); getNotice ();} private void getNotice () {new Thread () {@ Overridepublic void run () {notices = VoteCtrler. getInstance (). getNotice (); if (notices! = Null & notices. size ()> 0) handler. sendEmptyMessage (OperCons. INIT); elsehandler. sendEmptyMessage (OperCons. SHOW_TIPS); super. run ();}}. start ();} private void initAdapter () {showingNotices. clear (); showingNotices. addAll (notices); nAdapter. clear (); MyAndroidUtil. editXml ("notice", showingNotices); for (Notice notice: notices) {nAdapter. add (notice. getTitle ());}}}
   
  
 


Ctrler: (responsible for business logic. It obtains data through this class and returns the data to the activity to process the view logic)

Public class VoteCtrler {private static VoteCtrler instance; private VoteDao voteDao; public VoteCtrler () {voteDao = new VoteDao ();} public synchronized static VoteCtrler getInstance () {if (instance = null) instance = new VoteCtrler (); return instance;} public User login () {// more business logic, complete String result = voteDao here. login ("xxx"); if (result! = Null) {User user = JsonUtil. jsonToObject (result, User. class); return user;} return null;} public List
 
  
GetNotice () {String result = voteDao. getNotice (); if (result! = Null) return JsonUtil. jsonToObjectList (result, Notice. class); return null ;}}
 


Config is the configuration of some common data of the project.

Util is a tool class.

The above are my personal opinions on the mvc mode in android, mainly because at the beginning, all logic is often written directly in the activity, resulting in the activity being bloated and inconvenient to maintain, as a result, the code in the activity is too concentrated, so the project structure is forcibly changed to this.

If you have a better project structure, you can tell me that small team development can only rely on your own exploration...

Related Article

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.