Construct an artistic mobile development framework (KJAVA) based on the MVC + Observer Model (II)
Author: Sun Dongfeng
Date: 03/12/2007
In the previous lecture, we used the MVC + observer pattern to design the mobile development framework. In this section, we will use a "Media Player" as an example to implement this framework.
First, you can use the UML modeling tool to design the following classes:
This class diagram corresponds to the Framework Design described in the previous section.
The following is a specific coding based on this class graph:
Import javax. microedition. lcdui. graphics;
Public class controls {
Boolean isactive = false;
Public controls ()
{
}
Public void draw (Graphics g)
{
}
}
/**
*
* @ Author Jerry. Sun
* @ Date 2007-11-29 12:43:09 pm
*/
Public class MediaPlayerControl implements MediaPlayerObserver {
Public MediaPlayerView m_view;
Public MediaPlayerModel m_model;
Public MediaPlayerControl ()
{
M_view = new MediaPlayerView ();
M_model = new MediaPlayerModel (this );
}
Public void merge Update ()
{
M_view.CanvasStatusChange ();
}
}
/**
*
* @ Author jerry. sun
* @ Date 2007-11-29 12:40:31 pm
*/
Public class MediaPlayerModel implements Runnable {
Public MediaPlayerControl m_control;
Queue tempEventsQueue;
Public MediaPlayerModel (MediaPlayerControl)
{
M_control = control;
TempEventsQueue = MediaPlayerView. eventsQueue;
}
Public void parseEventsQueue ()
{
Object event = tempEventsQueue. get ();
Int eventID = 0; // event. id;
Switch (eventID)
{
Case 0:
Break;
Case 1:
Break;
Case 2:
Break;
Default:
Break;
}
M_control.1_update ();
}
Public void run (){
// TODO Auto-generated method stub
ParseEventsQueue ();
}
}
/**
*
* @ Author jerry. sun
* @ Date 2007-11-29 12:43:34 pm
*/
Public interface MediaPlayerObserver {
Public void merge Update ();
}
Import javax. microedition. lcdui. Canvas;
Import javax. microedition. lcdui. Graphics;
/**
*
* @ Author jerry. sun
* @ Date 2007-11-29 12:43:43 pm
*/
Public class MediaPlayerView extends Canvas implements Runnable {
Static Queue controlsQueue;
Static Queue eventsQueue;
Public MediaPlayerView ()
{
ControlsQueue = new Queue ();
If (! InitControlsQueue ())
System. out. println ("Maybe the ControlsQueue is full! ");
}
Public boolean initControlsQueue ()
{
Return controlsQueue. put (new Controls ())
& ControlsQueue. put (new Controls ());
// Continue...
}
Protected void CanvasStatusChange ()
{
}
Protected void paint (Graphics g ){
// TODO Auto-generated method stub
For (int I = 0; I {
Controls tempCon = (Controls) controlsQueue. get ();
If (tempCon! = Null & tempCon. isActive)
{
TempCon. draw (g );
} Else
{
System. out. println ("The" + tempCon + "is not activity! ");
}
}
}
Public void run (){
// TODO Auto-generated method stub
}
}
/**
*
* @ Author jerry. sun
* @ Date 2007-11-29 12:43:57 pm
*/
Public class Queue {
/** Maximum queue length */
Public static final int MAX_SIZE = 32;
Private Object queueArr [] = null;
Private int queueHeadId = 0;
Private int queueTailId = 0;
Private int queueLength = 0;
/** Initialization */
Private void init (){
QueueHeadId = 0;
QueueTailId = 0;
QueueArr = new Object [MAX_SIZE];
For (int ii = 0; II queuearr [II] = NULL;
}
}
/** Obtain the subscript of the next node */
Private Static int nextid (int id ){
Return (ID + 1) % max_size;
}
/** Obtain the subscript of the previous node */
Private Static int preid (int id ){
Return (ID + max_size-1) % max_size;
}
/** Add an OBJ to the queue. If the queue is full, false is returned. True is returned normally */
Public synchronized Boolean put (Object OBJ ){
If (isfull ())
Return false;
Queuearr [queuetailid] = OBJ;
Queuetailid = nextid (queuetailid );
Queuelength ++;
Return true;
}
/**
* Extracts an OBJ from the queue. If the queue is empty, null is returned.
* The retrieved OBJ pop-up queue
*/
Public synchronized object get (){
If (isEmpty ())
Return null;
Object obj = queueArr [queueHeadId];
QueueHeadId = nextID (queueHeadId );
QueueLength --;
Return obj;
}
/**
* Extracts an obj from the queue. If the queue is empty, null is returned.
* The retrieved obj does not bring up a queue.
*/
Public synchronized Object getWithoutPop ()
{
If (isEmpty ())
Return null;
Object obj = queueArr [queueHeadId];
Return obj;
}
/**
* Number of Obj in the current queue
*/
Public int length (){
Return queueLength;
}
/**
* Whether the queue is full
*/
Public boolean isFull (){
Return queueLength = queueArr. length;
}
/**
* Whether the queue is an empty queue
*/
Public boolean isEmpty (){
Return queueLength = 0;
}
/** Creates a new instance of Queue */
Public Queue (){
Init ();
}
}
In fact, in the implementation of a software project, the preliminary design is the most time-consuming, once the framework of the program can be clearly given. The entire project has completed at least 60%, and the remaining coding is relatively simple. This is why architects are more valuable than programmers! Well, I spoke a few things out of my mind, hoping to inspire everyone.
In this section, the Framework interface of the entire program is provided. In the remaining sections, these framework interfaces are used to integrate the entire project. Some specific design details need to be explained later. For example, how can we design the front-end UI to minimize the "Coupling Degree?
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? PostId = 1914508