Brief introduction to the MVC Framework model and application in your (Android) project
Last Update:2015-04-27
Source: Internet
Author: User
<span id="Label3"></p><p><p>The title is Ali telephone interview questions, always thought that they are very aware of the MVC pattern, the results were asked, the m, V, c The relationship between these are not clear, the most important is, can not be MVC and Android in the various components corresponding, so, interview certainly hung, but the interview is a way to learn, You can tell what big companies are looking for, and what knowledge loopholes they have, such as learning a lot from this INTERVIEW.</p></p><p><p>You can also look at the following content, but also think about the MVC and Android components of the corresponding relationship between the clear, see if there is the same as I smattering mvc.</p></p><p><p>If there is a problem in writing, Welcome to the Discussion. <span style="line-height: 1.5;">reprint Please specify source: http://www.cnblogs.com/John-Chen/p/4458823.html</span></p></p><p><p></p></p><p><p>Learn the MVC pattern read a lot of others summed up the article, there are several good, recommend to Everyone:</p></p><p><p>Front End Android Primer (3): MVC mode:<br>Http://isux.tencent.com/learn-android-from-zero-session3.html<br>Http://isux.tencent.com/learn-android-from-zero-session4.html<br>Http://isux.tencent.com/learn-android-from-zero-session5-html.html</p></p><p><p>The Activity revisited:<br>http://www.therealjoshua.com/2012/07/android-architecture-part-10-the-activity-revisited/</p></p><p><p>Talk about the evolution of UI architecture Design:<br>Http://www.cnblogs.com/winter-cn/p/4285171.html</p></p><p><p>Diagrams of MVC,MVP and MVVM:<br>Http://www.ruanyifeng.com/blog/2015/02/mvcmvp_mvvm.html</p></p><p><p>The practice of the MVP on another framework Model:</p></p><p><p>http://www.imooc.com/wenda/detail/216700</p></p><p><p></p></p><p><p>After figuring out what MVC is and what it does with Android components, and looking at some of the implementations in your project, there are a lot of things that are already being implemented in terms of mvc, but in design and implementation, we didn't think of frame patterns, just based on previous experience and some basic design ideas, So when I was asked about the MVC pattern, I didn't know where the project would be Useful. I don't think you know anything about it. framework patterns and design patterns, The main thing is to find ways to decouple and improve the stability of the Application.</p></p><p><p>The following is an implementation of the project to introduce the MVC framework pattern in android:</p></p><p><p>The project has a track function that records several states: record, pause, stop:</p></p><p><p>、</p></p><p><p>Next I extract the logic of the trajectory control section, and introduce the MVC design idea of this part briefly.</p></p><p><p>Demo Source Address: Https://github.com/John-Chen/BlogSamples/tree/master/MVCDemo</p></p><p><p> </p></p><p><p>View Implementation:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Trackctrlview<span style="color: #0000ff;"><span style="color: #0000ff;">Implements</span></span><span style="color: #000000;"><span style="color: #000000;">view.onclicklistener{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">------------------------Constants------------------------</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">-------------------------fields--------------------------</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">ImageView btnstarttrack, btnstoptrack, btnpausetrack; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Trackctrlviewlistener listener; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Trackrecordinfo trackrecordinfo; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">-----------------------constructors----------------------</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">trackctrlview (activity activity, trackctrlviewlistener listener) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Listener =<span style="color: #000000;"><span style="color: #000000;">listener; Btnstarttrack</span></span>=<span style="color: #000000;"><span style="color: #000000;">(ImageView) Activity.findviewbyid (r.id.btnstarttrack); Btnstoptrack</span></span>=<span style="color: #000000;"><span style="color: #000000;">(ImageView) Activity.findviewbyid (r.id.btnstoptrack); Btnpausetrack</span></span>=<span style="color: #000000;"><span style="color: #000000;">(ImageView) Activity.findviewbyid (r.id.btnpausetrack); Btnstarttrack.setonclicklistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); Btnstoptrack.setonclicklistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); Btnpausetrack.setonclicklistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); Btnpausetrack.setonclicklistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">--------Methods for/from superclass/interfaces-----------</span></span> <span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Notify the Controller of user request</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnClick (View V) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Switch</span></span><span style="color: #000000;"><span style="color: #000000;">(v.getid ()) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">r.id.btnstarttrack:</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(listener! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {listener.trackstatusrequest (trackrecordstatus.recording); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">r.id.btnstoptrack:</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(listener! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {listener.trackstatusrequest (trackrecordstatus.stoped); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">r.id.btnpausetrack:</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(listener! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">){ </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(trackrecordinfo.status = =<span style="color: #000000;"><span style="color: #000000;">Trackrecordstatus.paused) {listener.trackstatusrequest (trackrecordstatus.recording); }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{listener.trackstatusrequest (trackrecordstatus.paused); } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">default</span></span><span style="color: #000000;"><span style="color: #000000;">: </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; } } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">---------------------Methods public----------------------</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">---------------------Methods private---------------------</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">refreshview () {trackrecordstatus trackstatus</span></span>= Trackrecordinfo = =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>?<span style="color: #000000;"><span style="color: #000000;">TrackRecordStatus.Stoped:trackRecordInfo.status; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(trackstatus = =<span style="color: #000000;"><span style="color: #000000;">Trackrecordstatus.recording) {btnstarttrack.setvisibility (view.gone); Btnpausetrack.setvisibility (view.visible); Btnstoptrack.setvisibility (view.visible); Btnpausetrack.setimageresource (r.drawable.btn_track_ctrl_pause); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(trackstatus = =<span style="color: #000000;"><span style="color: #000000;">trackrecordstatus.paused) {btnstarttrack.setvisibility (view.gone); Btnpausetrack.setvisibility (view.visible); Btnstoptrack.setvisibility (view.visible); Btnpausetrack.setimageresource (r.drawable.btn_track_ctrl_resume); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">trackrecordstatus.stoped</span></span><span style="color: #000000;"><span style="color: #000000;">btnstarttrack.setvisibility (view.visible); Btnpausetrack.setvisibility (view.gone); Btnstoptrack.setvisibility (view.gone); } } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">---------------------Getter & Setter-----------------</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">settrackrecordinfo (@Nullable trackrecordinfo Trackrecordinfo) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Trackrecordinfo =<span style="color: #000000;"><span style="color: #000000;">trackrecordinfo; Refreshview (); } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">---------------Inner and Anonymous Classes---------------</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Interface</span></span><span style="color: #000000;"><span style="color: #000000;">trackctrlviewlistener{</span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* User Click button</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">trackstatusrequest (@Nullable trackrecordstatus newstatus); } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">---------------------logical fragments-----------------</span></span><span style="color: #000000;"><span style="color: #000000;">}</span></span></pre><p><p></p></p><p><p>Model Implementation:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Trackrecordinfo {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">------------------------Constants------------------------</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">-------------------------fields--------------------------</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>Gson Gson =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Gson (); </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* should be saved track database id, This demo database operation is not implemented, temporary trackid has been 0</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">trackid; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Trackrecordstatus status; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">-----------------------constructors----------------------</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>Trackrecordinfo (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">trackid, Trackrecordstatus Status) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. TrackID =<span style="color: #000000;"><span style="color: #000000;">trackid; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Status =<span style="color: #000000;"><span style="color: #000000;">status; } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">--------Methods for/from superclass/interfaces-----------</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">---------------------Methods public----------------------</span></span><span style="color: #000000;"><span style="color: #000000;">@NonNull</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span><span style="color: #000000;"><span style="color: #000000;">trackrecordinfo loadtrackrecordinfo (@NonNull context Context) {String pref</span></span>= sputil.getstring (context, sputil.key_track_record_info, "")<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">Textutils.isempty (pref)) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>Gson.fromjson (pref, Trackrecordinfo.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">changetrackrecordinfo (@NonNull Context context, @Nullable trackrecordinfo Info) {sputil.savestring (context, sputil.key_track_record_info, INFO</span></span>==<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>? ""<span style="color: #000000;"><span style="color: #000000;">: Gson.tojson (info)); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">model via message bus, notify view Refresh</span></span>Eventbus.getdefault (). Post (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">eventtrackrecordinfochanged (info)); } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">---------------------Methods private---------------------</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">---------------------Getter & Setter-----------------</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">---------------Inner and Anonymous Classes---------------</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">---------------------logical fragments-----------------</span></span><span style="color: #000000;"><span style="color: #000000;">}</span></span></pre><p><p></p></p><p><p>Controller implementation:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mainactivity<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span>Actionbaractivity<span style="color: #0000ff;"><span style="color: #0000ff;">Implements</span></span><span style="color: #000000;"><span style="color: #000000;">trackctrlview.trackctrlviewlistener{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Trackctrlview trackctrlview; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Trackrecordinfo trackrecordinfo; @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); Trackctrlview</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Trackctrlview (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>,<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); Eventbus.getdefault (). Register (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); Trackrecordinfo</span></span>= Trackrecordinfo.loadtrackrecordinfo (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); Trackctrlview.settrackrecordinfo (trackrecordinfo); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnDestroy () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnDestroy (); Eventbus.getdefault (). Unregister (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">trackstatusrequest (@Nullable trackrecordstatus Newstatus) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(newstatus = =<span style="color: #000000;"><span style="color: #000000;">Trackrecordstatus.recording) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>TrackID = 0;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create a trace in the database and get to the database ID</span></span>Trackrecordinfo =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Trackrecordinfo (trackid, trackrecordstatus.recording); }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(newstatus = =<span style="color: #000000;"><span style="color: #000000;">Trackrecordstatus.paused) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(trackrecordinfo! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {trackrecordinfo.status</span></span>=<span style="color: #000000;"><span style="color: #000000;">newstatus; } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{trackrecordinfo</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; } trackrecordinfo.changetrackrecordinfo (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">, trackrecordinfo); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">oneventmainthread (eventtrackrecordinfochanged event) {trackrecordinfo</span></span>=<span style="color: #000000;"><span style="color: #000000;">event.info; Trackctrlview.settrackrecordinfo (trackrecordinfo); }}</span></span></pre><p><p></p></p><p><p>It's a bit late, and i'll go back to work TOMORROW.</p></p><p><p></p></p><p><p>Brief introduction to the MVC Framework model and application in your (Android) project</p></p></span>