2011-09-06 44 views
2

爲什麼有些人喜歡設定命令監聽風格偏好懷疑

.setCommandListener(this) 

.setCommandListener(new CommandListener(){}) 

更頻繁地使用?我應該在什麼情況下使用第二個,爲什麼? 我認爲這只是一個風格問題或是否存在特定問題?

回答

1

如果使用「this」,則必須實現偵聽器到類中,然後可以在偵聽器的已實現方法中訪問類的字段。

如果您使用第二個(新Listener ...),那麼如果您不需要訪問班級中的許多其他內容,它可能會更具可讀性。

0

setCommandListener(this)顯然更容易閱讀「玩具編碼」。我認爲這就是爲什麼我看到它在許多入門級教程中使用,而作者只是不想太深入。

它也看起來像初學者程序員只是盲目地複製這個反模式從教程,沒有給它額外的想法。

對於更復雜的代碼,儘管setCommandListener(new CommandListener(){/*..*/})在我的經驗中更容易維護和閱讀。

還要注意在這兩種情況下是you can access fields of class,只是後者需要使用Qualified this

//import javax.microedition.midlet.*; 
//import javax.microedition.lcdui.*; 

abstract class ListenerTest extends MIDlet { 
    protected Display display; 

    protected void startApp() { 
     display = Display.getDisplay(this); 
     Form form = new Form("welcome"); 
     form.addCommand(new Command("go", Command.OK, 1)); 
     form.setCommandListener(new CommandListener() { 
      public void commandAction(Command c, Displayable d) { 
       // qualified this - see JLS 15.8.4 
       ListenerTest.this.cmdAction(c, d); 
      } 
     }); 
     // display "welcome" screen with "go" command 
     display.setCurrent(form); 
    } 

    protected void pauseApp() { } 

    protected void destroyApp(boolean unconditional) { 
     notifyDestroyed(); 
    } 

    protected abstract void displayNext(); 

    private void cmdAction(Command c, Displayable d) { 
     // invoke from listener to display next screen 
     displayNext(); 
    } 
} // ListenerTest 

class NextTest extends ScreenTest { 

    protected void displayNext() { 
     Form form = new Form("bye-bye"); 
     form.addCommand(new Command("EXIT", Command.EXIT, 1)); 
     form.setCommandListener(new CommandListener() { 
      public void commandAction(Command c, Displayable d) { 
       notifyDestroyed(); 
      } 
     }); 
     // display "bye-bye" screen with "exit" command 
     display.setCurrent(form); 
    } 
} // NextTest 

BTW我提到上面的方法也更安全?它可以保證您對特定屏幕的期望值是您設置的。

說,如果你做一個簡單的重寫setCommandListener(本)並運行它,你會發現奇怪的行爲 - 命令「走出去」,現在將退出這個MIDlet而不是顯示下一屏幕:

// don't do that 
    abstract class ListenerTest extends MIDlet implements CommandListener { 
     protected Display display; 

     protected void startApp() { 
      display = Display.getDisplay(this); 
      Form form = new Form("welcome"); 
      form.addCommand(new Command("go", Command.OK, 1)); 
      form.setCommandListener(this); 
      // display "welcome" screen with "go" command 
      display.setCurrent(form); 
     } 

     protected void pauseApp() { } 

     protected void destroyApp(boolean unconditional) { 
      notifyDestroyed(); 
     } 

     protected abstract void displayNext(); 

     public void commandAction(Command c, Displayable d) { 
      // invoke from listener... really??? check the subclass 
      displayNext(); 
     } 
    } // ListenerTest 

    class NextTest extends ScreenTest implements CommandListener { 

     protected void displayNext() { 
      Form form = new Form("bye-bye"); 
      form.addCommand(new Command("EXIT", Command.EXIT, 1)); 
      form.setCommandListener(this); 
      // display "bye-bye" screen with "exit" command 
      display.setCurrent(form); 
     } 

     public void commandAction(Command c, Displayable d) { 
      // you may not notice but... 
      notifyDestroyed(); 
      // ...this actually overrides superclass implementation 
     } 
    } // NextTest