×
Namespaces

Variants
Actions
(Difference between revisions)

MVC in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
grahamhughes (Talk | contribs)
(Created page with 'Category:Java ME Applications built to the MVC paradigm contain three kinds of component: * Models (contain the data and the functionality for manipulating it) * Views (retr…')
 
hamishwillee (Talk | contribs)
m (Hamishwillee - Fix categories)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Java ME]]
+
[[Category:Programming Idioms on Java ME]][[Category:Mobile Design Patterns]][[Category:Architecture/Design]][[Category:Code Snippet]]
Applications built to the MVC paradigm contain three kinds of component:
+
{{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]] -->
 +
|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'') -->
 +
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Required capabilities for code (e.g. ID_CAP_LOCATION, ID_CAP_NETWORKING) -->
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only - not link -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20110109
 +
|author= [[User:Grahamhughes]]
 +
}}
 +
 
 +
== 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 08: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 08:47.
84 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.

×