Professional Documents
Culture Documents
Architectural Manifesto: The MVC Design Pattern in MIDP Development
Architectural Manifesto: The MVC Design Pattern in MIDP Development
Architectural Manifesto: The MVC Design Pattern in MIDP Development
com/developerworks/wireless/library/wi-arch6/
Level: Introductory
Mikko Kontio (mikko.kontio@enfo.fi), Technology manager, Enfo Solutions
09 Sep 2004
Learn how to use the Model-View-Controller pattern in your MIDP user interface development. Start
with an overview of the structure of the MVC pattern, then see how it can be applied in a real-life case.
Introduction
In everyday software development you often encounter the same problems again and again. Rather than solve the
same problem each time, it makes sense to document the solution and re-use it. Design patterns are essentially
documented, generalized solutions to common problems in application development.
It has been said that there are two phases to implementing a design pattern: understanding it, and then using it. In
this article, you'll be introduced to the Model-View-Controller (MVC) pattern and learn how to make it part of
your MIDP user interface development. You'll start with an overview of the MVC pattern structure, then see how it
can be applied in a real-life case.
What is MVC?
Some people categorize MVC as an architectural style, while others (such as myself) think of it as a design pattern.
Whereas architectural styles tend to dictate the structure of the whole application, design patterns focus on solving
one or two particular problems. The MVC pattern solves the problem of updating an application's views (UI
screens) as its data changes.
The MVC pattern consists of three parts: model, view, and controller. The model represents the data and business
logic of the application. In an accounting application, the model would be the part of the application responsible
for fetching data from the lower layers or components, handling the data, making any necessary calculations, and
notifying all the views registered to that particular data of any changes.
The view is the actual screen that displays data and related commands to the user. Some applications offer multiple
views of a given model, or even of the same data. Many of the popular spreadsheet applications present different
kinds of charts representing a single block of data.
The controller receives user actions and dispatches them to the model. You can assign a single class to be the
controller or divide the controller's responsibilities among several classes.
Figure 1 is a simple diagram of the MVC pattern. The view contains the graphical user interface, which is the part
of the application the user can see. When the user alters the data on the screen and then initiates an action, that
action goes to the controller. The controller determines what kind of action has been requested and calls the
appropriate interfaces of the model. The model can consist of several components, classes, or packages, depending
on the technology that implement the application's business logic.
Notice that the view has registered itself to the model. The model notifies the view of data changes in its various
components. This way, the view doesn't have to poll the model about changes in the application data.
From this point, you'll concentrate on the code, which should amply demonstrate the three components of the MVC
pattern, as well as their use in building an MIDP interface.
The controller
The MVCMIDlet class shown in Listing 1 is a typical MIDlet class. It has all the mandatory methods, and it also
implements the CommandListener interface. Every time the user updates the value of the TextField the
controller updates the model. The MIDlet class controls all of the commands and the setting of the command
listener.
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.io.*;
public MVCMIDlet() {
v = new View(model);
model.start();
display = Display.getDisplay(this);
v.addCommand(exitCommand);
v.addCommand(updateCommand);
v.setCommandListener(this);
display.setCurrent(v);
}
The model
The Model class simulates an actual model that would be used by multiple users. As users change the state of the
model (by changing its values), the model notifies its registered views. The run() method is the heart of the
simulator; as you can see, it simply increases the value of the count every 50 milliseconds.
package com.kontio;
import java.util.*;
public Model(){
}
The view
The View class is inherited from the MIDP 2.0 Form class. It has one TextField and one StringItem. The
StringItem shows the value of the model and the TextField offers a field to update the value. Naturally, this
isn't the only way to implement MVC, but it is one possibility.
In conclusion
Design patterns let you effectively and efficiently solve common problems in application development. The MVC
design pattern is a proven solution for applications where the user interface needs to show up-to-date information
and polling the server isn't an option.
In MVC, the user interface (called the view) registers itself as a listener to certain parts of the application's
underlying business and functional logic, as represented by the model. The model then notifies all registered views
whenever there is a change in the data. Completing the cycle, the controller receives user actions and dispatches
them to the model.
In this month's column I've introduced you to the structure of MVC and walked you through an implementation of
MVC in an MIDP environment. While very simple, the example application demonstrates the three components of
the Model-View-Controller pattern and shows how they can work together as the underlying framework of an
MIDP interface.
Resources
Learn more about MIDP development with Mikko's Designing MIDP applications (developerWorks,
February 2004).
This column demonstrates the simplicity of the Form class, but MIDP 2.0 offers far more advanced features
for user interface development. Mikko's Custom GUI development with MIDP 2.0 (developerWorks, May
2003) shows you how to build custom GUI elements with the extendable CustomItem class.
For a more in-depth introduction to MIDP development, delve into John Muchow's J2ME 101 series
(developerWorks, November 2003), a comprehensive four-part introduction to Java 2 Platform, Micro
Edition (J2ME) and the Mobile Information Device Profile.
David Gallardo's Design Patterns 101 (developerWorks, January 2002) tutorial introduces the theory and
practice of pattern-based development.
IBM's Thin Client Framework (TCF) is a lightweight programming framework for Java client applications. In
the two-part Introduction to Thin Client Framework (developerWorks, January 2003), developers Barry
Feigenbaum and Peter Bahrs show you how TCF raises the Model-View-Controller design pattern to the level
of an application architecture.
Visit the Sun Microsystems developer network to learn more about MIDP 2.0 and J2ME.
Don't miss a single installment of Mikko's Architectural manifesto column. See the column series page for a
complete listing of previous installments in this series.
See the developerWorks Wireless technology zone for a continuously updated collection of articles and
tutorials on mobile application design and development.
Mikko Kontio works as a production manager for the leading-edge Finnish software company, Softera. He holds a
master's degree in computer science and is the author and co-author of several books, the latest being Professional
Mobile Java with J2ME, published by IT Press. Mikko can be reached at mikko.kontio@softera.fi.