Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

(Difference between revisions)

MVC in Java ME

From Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot addition of Template:ArticleMetaData)
hamishwillee (Talk | contribs)
m (Hamishwillee - Fix categories)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{ArticleMetaData
+
[[Category:Programming Idioms on Java ME]][[Category:Mobile Design Patterns]][[Category:Architecture/Design]][[Category:Code Snippet]]
 +
{{Abstract|This article discusses how to implement a generic Model-View-Controller (MVC) architectural pattern using Java}}
 +
{{ArticleMetaData <!-- v1.3 -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
+
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->  
+
|capabilities= <!-- Required capabilities for code (e.g. ID_CAP_LOCATION, ID_CAP_NETWORKING) -->
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
+
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
|capabilities=<!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
+
|translated-by= <!-- [[User:XXXX]] -->
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
+
|translated-from-title= <!-- Title only - not link -->
|id= <!-- Article Id (Knowledge base articles only) -->
+
|translated-from-id= <!-- Id of translated revision -->
|language=<!-- Language category code for non-English topics - e.g. Lang-Chinese -->
+
|review-by= <!-- After re-review: [[User:username]] -->
|review-by=<!-- After re-review: [[User:username]] -->
+
|review-timestamp= <!-- After re-review: YYYYMMDD -->
|review-timestamp=<!-- After re-review: YYYYMMDD -->
+
|update-by= <!-- After significant update: [[User:username]]-->
|update-by=<!-- After significant update: [[User:username]]-->
+
|update-timestamp= <!-- After significant update: YYYYMMDD -->
|update-timestamp=<!-- After significant update: YYYYMMDD -->
+
|creationdate= 20110109
|creationdate=20110109
+
|author= [[User:Grahamhughes]]
|author=[[User:Grahamhughes]]
+
}}
}}[[Category:Java ME]]
+
 
Applications built to the MVC paradigm contain three kinds of component:
+
== Model-View-Controller ==
 +
 
 +
Applications built to the [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Model-View-Controller] (MVC) architectural pattern contain three kinds of component:
  
 
* Models (contain the data and the functionality for manipulating it)
 
* Models (contain the data and the functionality for manipulating it)

Latest revision as of 05:47, 13 August 2013

This article discusses how to implement a generic Model-View-Controller (MVC) architectural pattern using Java

Article Metadata
Article
Created: grahamhughes (09 Jan 2011)
Last edited: hamishwillee (13 Aug 2013)

Contents

[edit] Model-View-Controller

Applications built to the Model-View-Controller (MVC) architectural pattern contain three kinds of component:

  • Models (contain the data and the functionality for manipulating it)
  • Views (retrieve data from the Models and display it)
  • Controllers (attach to the user interface so that updates can be sent to the Models)

Every time a Model is changed, update notifications must be sent to all Views that are interested in that Model, so that they can retrieve the updated information and refresh themselves.

[edit] The View

Views need to implement an interface like the following, so that they can receive update messages from Models.

/**
* A class must implement the View interface, in order to be notified when a Model changes.
* Roughly equivalent to java.util.Observer.
* @author Graham Hughes
*/

public interface View {
/**
* Called when "model" is changed.
* @param model the Model that has changed
* @param arg a model-specific notification object (passed to the model's notifyViews() method)
*/

public void update(Model model, Object arg);
}

[edit] The Model

Models need to extend a common super class (as below), which defines how they interact with Views. All Models need to maintain a list of interested Views, need a "changed" status, and need the ability to inform all interested Views when they have changed.

import java.util.Vector;
import java.util.Enumeration;
 
/**
* The abstract super class for any Model to which a View can attach.
* Roughly equates to java.util.Observable.
* @author Graham Hughes
*/

public abstract class Model {
private Vector views;
private boolean changed;
 
public Model() {
views = new Vector();
}
 
/** Add a View to this Model's set of views, if it is not already.
* @param v View to add
* @throws NullPointerException if v is null
*/

public synchronized void addView(View v) {
if (v == null) {
throw new NullPointerException();
}
if (!views.contains(v)) {
views.addElement(v);
}
}
 
/** Delete a View from this Model's set of views. */
public synchronized void deleteView(View v) {
views.removeElement(v);
}
 
/** Remove all Views from this Model. */
public synchronized void deleteViews() {
views.removeAllElements();
}
 
/** Notify this Model's Views if it has changed, and clear the changed status. Views are notified in no specific order. */
public synchronized void notifyViews(Object arg) {
if (hasChanged()) {
for (Enumeration en = views.elements(); en.hasMoreElements();) {
((View) en.nextElement()).update(this, arg);
}
clearChanged();
}
}
 
/** Equivalent to notifyViews(null). */
public void notifyViews() {
notifyViews(null);
}
 
/** @return true if this Model has been changed. */
public synchronized boolean hasChanged() {
return changed;
}
 
/** Set this Model into the "changed" state. */
protected synchronized void setChanged() {
changed = true;
}
 
/** Clear this Model's "changed" state. */
protected synchronized void clearChanged() {
changed = false;
}
}

[edit] The Controller

Since Controllers only send messages to Models, and don't receive them, they require no special code as part of this framework.

[edit] Example

public class MVCExample {
public static void main(String[] args) {
// create the Model
MyModel m = new MyModel();
// create and attach a View
MyView v = new MyView(m);
// update the Model (act as a Controller)
m.setMessage("fish for dinner!");
}
}
 
// The Model
class MyModel extends Model {
private String message;
 
public void setMessage(String newMessage) {
message = newMessage;
setChanged();
notifyViews();
}
 
public String getMessage() {
return message;
}
}
 
// The View
class MyView implements View {
public MyView(MyModel model) {
model.addView(this);
}
 
public void update(Model m, Object arg) {
System.out.println(((MyModel) m).getMessage());
}
}
This page was last modified on 13 August 2013, at 05:47.
87 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×