Introduction to the MVC pattern

Source: Internet
Author: User

Model-View-control (Model-view-controller) The MVC structure is a way to develop a module that separates data storage and processing from the visual representation of the data. The component that stores and processes data is called a model, and it contains the actual contents of the module. The component representing the data is called the view, which handles all the necessary behavior of the module and completes all display of the module. A control is typically a component used to obtain data.


The decomposition of modules into models and views has two distinct advantages:

1. You can use multiple views to share the same model data.

2. Simplify the writing of complex applications, using modules that are scalable and easy to maintain. You can modify the view without affecting the model, and vice versa.

One of the heaviest uses of MVC is that because the model package contains data, the view displays the data, so once a view is connected to the model, it must be synchronized with the model.

The model data changes the notification view, the view accepts the notification, and the affected view area is updated.

This can be accomplished using the observer pattern described in the previous article, which can be implemented in this way if the reader understands the Java Event delegation model.

Another variant of MVC is the combination of controls and views. In this case, the view not only displays the data, but also interacts with the user as an interface to accept input from the user. As shown in the following:

This variant pattern, as the reader does for iOS development, should be well aware of the variant patterns that the iOS application architecture follows in this variant of MVC.

Here is a variant of this model demo, model notification View Update This step, is implemented with the Java event Delegate model, here in order to take account of some readers of this model does not understand, simple introduction:

The event source object triggers an event, and the object that is interested in this event handles it, and the object that is interested in this event is called the listener, which means that the event is delegated to the listener for processing.

<pre name= "code" class= "java" >package com.example.learn;import Android.content.context;import Android.graphics.canvas;import Android.graphics.paint;import Android.util.attributeset;import Android.view.View; public class Circleview extends View implements ActionListener {private Circlemodel model;public Circleview (Context Conte XT, AttributeSet Attrs) {Super (context, attrs);//TODO auto-generated constructor stub}public Circlemodel Getmodel () {RET URN model;} public void Setmodel (Circlemodel model) {This.model = model;if (model! = NULL) {Model.addactionlistener (this); invalidate ();}} @Overridepublic void actionperformed (ActionEvent event) {//TODO auto-generated method Stubinvalidate ();} @Overridepublic void Draw (canvas canvas) {//TODO auto-generated method Stubsuper.draw (Canvas), if (model = = NULL) {return; }int width = getwidth (); int height = getheight (); Paint paint = new paint (); Canvas.drawcircle (WIDTH/2, HEIGHT/2, (float) Model.getradius (), paint);}}

View Component

Package Com.example.learn;import Java.util.arraylist;public class Circlemodel {private static final int default_radius = 20;private Double radius = default_radius;private arraylist<actionlistener> actionlisteners;public double Getradius () {return radius;} public void Setradius (double radius) {This.radius = Radius;processevent (The new ActionEvent (this, actionevent.update, " Radius ")); public void addActionListener (ActionListener listener) {if (actionlisteners = = null) {actionlisteners = new arraylist< Actionlistener> ();} Actionlisteners.add (listener);} public void Removeactionlistener (ActionListener listener) {if (actionlisteners! = null && Actionlisteners.contains (Listener)) {Actionlisteners.remove (listener);}} private void processevent (ActionEvent event) {ArrayList list = null;synchronized (this) {if (actionlisteners = = null) {Retu RN;} List = (ArrayList) actionlisteners.clone ();} for (int i = 0; i < list.size (); i++) {ActionListener listener = (ActionListener) list.get (i); LiStener.actionperformed (event);}}} Class ActionEvent {public static final byte UPDATE = 0x01;public ActionEvent (Object source, byte type, String Eventdesc) { }}interface ActionListener {public void actionperformed (ActionEvent event);}
Model components


Package Com.example.learn;import Android.app.activity;import Android.os.bundle;import android.view.View;import Android.view.view.onclicklistener;import Android.widget.edittext;public class Circlecontroller extends Activity { Private Circlemodel model;private circleview view; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO Auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); view = ( Circleview) Findviewbyid (r.id.circle_view); model = new Circlemodel (); Model.addactionlistener (view); View.setmodel ( model); View Setradius = Findviewbyid (r.id.radius_bn), final EditText Radiuset = (EditText) Findviewbyid (R.id.radius_et); Setradius.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubdouble radius = double.parsedouble (Radiuset.gettext (). toString ()); Model.setradius (RADIUS);}});}
Controller components




Introduction to the MVC pattern

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.