1:

架构性宣言: MIDP 2.0 中的工厂设计模式

如何使用 MIDP 的 Generic Connection Framework

​http://www-128.ibm.com/developerworks/cn/java/wi-arch22/​

级别: 初级

​Mikko Kontio​​, 产品经理, Softera

2006 年 3 月 13 日

这个月将学习有关 MIDP 2.0 的更多知识,同 Mikko 一起观察 Mobile Information Device Profile (MIDP) 的通用连接器框架 —— 工厂设计模式。

手机开发人员通常使用 Generic Connection Framework 在 Mobile Information Device Profile (MIDP) 中创建和维护顺利的连接。好的架构师都知道该框架背后真正的动力是不可缺少的工厂设计模式。工厂设计模式是面向对象编程所必需的,它构成了应用程序开发人员使用的大多数强大框架的基础 —— 包括 MIDP。在本月的架构性声明 专栏中,我将介绍工厂模式的三种变体,然后展示如何在 MIDP 2.0 通用连接框架中工厂支持连接处理。

工厂模式

工厂设计模式是面向对象编程中最常用的设计模式之一。它又被称为创建性模式,因为它被用来创建其他类。在应用程序预见不到自己要创建的对象类型时,就会使用工厂解决方案。在这些情况下,可以使用工厂模式作为创建对象的基础,不需要确切地了解将要创建哪些对象。

工厂 实际上是一组模式的名称,这组模式的目的是创建类。每个变体都指定一个不同的创建方法。这些模式变体是:

  • 静态工厂(Static Factory)
  • 工厂方法(Factory Method)
  • 抽象工厂(Abstract Factory)

首先,我先一般性地查看一下工厂模式解决常见应用程序设计问题的方式。然后将演示工厂模式如何开始对 MIDP 2.0 中大量连接类型的连接进行处理。

 

其余部分请到IBM站点上看吧。

 

2:

《MVC设计模式在客户端的应用》英文文档下载地址:

​http://www.j2medev.com/Soft/ShowSoftDown.asp?UrlID=1&SoftID=346​


3:

《应用MVC模式解决J2ME导航问题》

​http://www.j2medev.com/Article/ShowArticle.asp?ArticleID=28​

摘要:开发MIDlet的程序员也许经常会被界面的导航问题所困扰,尤其界面比较多的时候,通常有七、八个界面就会很让人头疼了。本文讲述如何应用MVC设计模式解决这类的问题。

    MVC 设计模式已经非常的成熟并在WEB Application的开发中广泛使用,apache的开源项目struts就是典型的例子。MVC的本质就是是逻辑和显示分开,通过控制器进行协调。通常我们会感到控制器比较的肥大,这个是个有争议的问题。MIDP的用户界面开发是比较简单的,只有那么20几个类。但是由于导航一般只能通过 Command来实现,所以界面增多的情况下,如果没有有效的组织那么程序写起来非常的乱,最致命的是这样的程序可读性差、扩展性差、可维护性差。

    应用MVC解决这个问题的关键是提供一个桥梁作用的控制器,它通常要有一个MIDlet作为参数。

 

4:

层叠式菜单模式的流程流转示例:

Figure 1. A city guide application using Cascading Menu

code:

  public void commandAction(Command command, Displayable displayable)
  {
    if ( command.equals( List.SELECT_COMMAND ) && displayable instanceof MenuList )
    {
      MenuList list = (MenuList) displayable;
      int i = list.getSelectedIndex();
      MenuElement item = (MenuElement) children.elementAt( i );
      Object next = menuMap.get( item );

      if ( next instanceof MenuElement )
      {
        list.showMenu( (MenuElement) next );

      } else if ( next instanceof MDisplayable && next instanceof Displayable )
      {
        ((MDisplayable)next).onDisplay( item );
        list.getDisplay().setCurrent( (Displayable) next );

      } else if ( next instanceof Displayable )
      {
        list.getDisplay().setCurrent( (Displayable) next );
      }
    }
  }

更多代码参见

javascript:void(0)

中的PatternShow\src\pattern\menu。

 

5:

向导式对话框模式 幻灯片模式流程流转参见:

流程流转图样看:


Figure 3. A sample wizard application


/**

   * this commandAction is only invoked by WDialog

   * This function control the flow of dialogs when user press Next or Back

   * button. This function also invoke onEnter() and onLeave() on WDialog. If a dialog

   * return WDialog.REJECT on either onEnter() or onLeave(), WizardEngine will not

   * switch screen.

   * @param command

   * @param displayable

   */

  public void commandAction(Command command, Displayable displayable)

  {

    WDialog cur_dialog = (WDialog)displayable;

    if ( command == WDialog.NEXT_COMMAND )

    {

      int i1 = dialogs.indexOf( displayable );

      if ( i1 < dialogs.size() - 1 )

      {

        WDialog next_dialog = (WDialog)dialogs.elementAt( i1 + 1 );

        if ( cur_dialog.onLeave( WDialog.FORWARD ) != WDialog.OK )

           return;

        if ( next_dialog.onEnter( WDialog.FORWARD ) != WDialog.OK )

           return;

        display.setCurrent( next_dialog );

      }

    } else if ( command == WDialog.BACK_COMMAND )

    {

      int i1 = dialogs.indexOf( displayable );

      if ( i1 > 0 )

      {

        WDialog prev_dialog = (WDialog)dialogs.elementAt( i1 - 1 );

        if ( cur_dialog.onLeave( WDialog.BACKWARD ) != WDialog.OK )

           return;

        if ( prev_dialog.onEnter( WDialog.BACKWARD ) != WDialog.OK )

           return;

        display.setCurrent( prev_dialog );

      }

    }

  }

更多代码参见

​javascript:void(0)​

中的PatternShow\src\pattern\wizard。

 

6:

 


Figure 6. Example slide show sequence


Listing 6. Slide show implementation


    // Create a slide show engine with Display object as parameter.

    // 'this' pointer refers to current MIDlet.

    engine = new SlideEngine( Display.getDisplay(this ) );


    // Create 3 slides. All the slides are formed here.

    // You can use something else.

    Form f1 = new Form( "Slide 1");

    f1.append( "This is slide number 1" );


    Form f2 = new Form( "Slide 2");

    f2.append( "This is slide number 2" );


    Form f3 = new Form( "Slide 3");

    f3.append( "This is slide number 3" );


    // Add the slides to engine, each slide stays on screen

    // for 2 seconds.

    engine.addSlide( f1, 2000 );

    engine.addSlide( f2, 2000 );

    engine.addSlide( f3, 2000 );


    // Start the slide show

    engine.startShow();

更多代码参见

​javascript:void(0)​

中的PatternShow\src\pattern\slide。

 

7:

分页List模式

流程流转:

其实就是一个List,但他有很多很多项,以至于需要用户手动翻页查看。

用户选择More菜单就是往后翻,选择Prev就是向前翻。

Listing 5. PagableList sample usage


   PagableList pagablelist = new PagableList( "Paging", List.IMPLICIT);

  

   // 'this' refer to the current MIDlet

   Display.getDisplay(this).setCurrent(pagablelist);

  

   for ( int i=0; i< 100; i++ )

   {

      pagablelist.append( "Item  #"+i, null );

   }

更多代码参见

​javascript:void(0)​

中的PatternShow\src\pattern\pagination。

后面四个模式来自于《Big designs for small devices

Four J2ME design patterns simplify interactive content development

​http://www.javaworld.com/javaworld/jw-12-2002/jw-1213-j2medesign.html​​》。