GWT ã® MVP ãµã³ãã«
GWT ã§ã¯å¤§ããªã¢ããªã±ã¼ã·ã§ã³ãä½ãå ´åã« MVP ããªã¹ã¹ã¡ãªã®ã ããã§ãï¼
MVP ã¯ãããã MVC ã®å¤å½¢ã§ããï¼POSA æ¬ã§å¯ä¸ç¿»è¨³ããã¦ãã Vol.1 ã«ãåºã¦ãã¦æ±ºãã¦æ°ãããã®ã§ã¯ããã¾ããï¼
ä¸ã®ä¸ã (Ajax ããªããªã«ãªãåã®) ã·ã³ãã«ãª Web ã¢ããªã§æ¥½ããã¦ãéï¼ãã£ã¡æ¹é¢ãåçã«é²åãã¦ããã¨ããããªãã¨ã¯ããã¾ããã§ãããï¼
ãããï¼æ¬æ ¼çã«ãªãã㪠Web ã¢ããªã«ãªãã¨ã¾ã 90 年代ã®ãããªè¦ãã¿ãå³ããã®ããªãï¼
ã¨ããã (JW)ï¼ãã®é ã® VB ã VC++ (MFC) ã®ããã«ã¤ãã³ããã³ãã©ã®ã¹ãã²ããã£ã«ãã¼ããªãããã«ï¼GWT ã®ä¸ã®äººé (ï¼) 㯠MVP ããªã¹ã¹ã¡ãã¦ããããã§ï¼è§£èª¬ã¨ãµã³ãã«ããã®è¾ºã«ããã¾ãï¼
ã§ãã§ãï¼ã©ã¡ãããªãã ãå¾®å¦ã§ãï¼ï¼ï¼
ã©ããã§ããï¼ãã¥ã¼ã¨ãã¬ã¼ã³ã¿ã¼ãççµåã«ãããã¿ãããªãã§ããï¼
ã ãã©ï¼å®éã«ã¯ççµåã«ãªã£ã¦ãªãã¦ï¼ä½è¨ãªãã¨ããã¡ãã£ã¦ãï¼
ããã¦ä¸è²«æ§ããªãï¼
Contacts
ãããªãã㧠(ã©ããªããã§ï¼)ï¼æåã®ããã¥ã¡ã³ãã§è§£èª¬ããã¦ãã Contacts
ãµã³ãã«ãè¦ã¦ã¿ã¾ãï¼
ãã®ãµã³ãã«ã¢ããªã«ã¯ 2 ã¤ã®ç»é¢ (ãã¬ã¼ã³ã¿ã¼ã¨ãã¥ã¼ã®ãã¢) ãããã¾ãï¼
é£çµ¡å
ã®ä¸è¦§ç»é¢ã¨ç·¨éç»é¢ã§ãï¼
ããã§ã¯ä¸è¦§ç»é¢ã ãè¦ã¦ããã¾ãï¼
ã¾ãã¯é£çµ¡å ã®ä¸è¦§ã表示ããç»é¢ã®ãã¬ã¼ã³ã¿ã¼å®è£ ã¯ã©ã¹ï¼
package com.google.gwt.sample.contacts.client.presenter; public class ContactsPresenter implements Presenter { public interface Display { HasClickHandlers getAddButton(); HasClickHandlers getDeleteButton(); HasClickHandlers getList(); void setData(List<String> data); int getClickedRow(ClickEvent event); List<Integer> getSelectedRows(); Widget asWidget(); } private final Display display; ï¼ï¼ï¼
ãã®ãã¹ãããåã¨ãã¦å®ç¾©ããã¦ãã Display
ãï¼ContactsPresenter
ã¨ãã¥ã¼ã®éã®ã¤ã³ã¿ãã§ã¼ã¹ã«ãªãã¾ãï¼
ããã¤ãå®è£
ãããã¥ã¼ã§ããã°ï¼ã©ããªå®è£
ã§ã ContactsPresenter
ã¨ã¤ã³ã¿ã©ã¯ã·ã§ã³ã§ããã¨ãããããï¼
ãã㦠ContactsPresenter
㯠Display
ã¤ã³ã¿ãã§ã¼ã¹ããã¦ã£ã¸ã§ãããåã£ã¦ãã¦ã¤ãã³ããã³ãã©ãç»é²ãã¾ãï¼
public void bind() { display.getAddButton().addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { eventBus.fireEvent(new AddContactEvent()); } }); ï¼ï¼ï¼ }
ãµãï¼ï¼ï¼
ã¨ã¦ãæ®å¿µãªããï¼ãã©ããªãã¥ã¼ã®å®è£
ã§ããã¨ããã»ã©ã«ã¯ãã® Display
ï¼ãã¥ã¼ã®å®è£
ãæ½è±¡åã§ãã¦ãã¾ããï¼
ã¾ãï¼é£çµ¡å
ã追å ããã«ã¯ãã¥ã¼ã¯ GWT ã® HasClickHandler
ãå®è£
ããã¦ã£ã¸ã§ããã使ã£ã¦ããªããã°ãªãã¾ããï¼
HasClickHandler
ãå®è£
ãã¦ããã®ã¯ãã¿ã³ã ãã§ãªãï¼ããã«ãªã©ãã¦ã¹ã§ã¯ãªãã¯ã§ãããã®ãªãå¤ãã®ã¦ã£ã¸ã§ããã HasClickHandler
ãå®è£
ãã¦ããããã§ãï¼
ã ããã©ããªã¦ã£ã¸ã§ããã使ã£ã¦ãã¦ããã¬ã¼ã³ã¿ã¼ã¯å½±é¿ãåããªãï¼ï¼ï¼ ããã¯ããã¾ããï¼
ä¾ãã° Ext GWT (2.x) ã®ã¦ã£ã¸ã§ããã¯ç¾å¨ã®ã¨ãã GWT ã®æ¨æºçãªã¤ãã³ãã使ã£ã¦ãã¾ããï¼
Ext GWT 3 ã§å¯¾å¿ããã¨ãã¼ããããã«ã¯åºã¦ã¾ããã©ãï¼
ã¨ããã (JW)ï¼ç¾æç¹ã§ã¯ Ext GWT ã使ã£ããã¥ã¼ã¯ ContactsPresenter
ã¨çµã¿åããããã¨ã¯ã§ããªãã¨ãããã¨ã§ãï¼
ãã®ä»ï¼ã³ã³ããã¹ãã¡ãã¥ã¼ãã·ã§ã¼ãã«ãããã¼ï¼Drag'n'Drop ã§é£çµ¡å
ã追å ã§ããããã«ãããã¨ãã§ããªãã§ãããï¼
ã§ããªããã¨ã¯ãªããã©ï¼ãã®å ´å㯠ContactPresenter.Display
ãä¿®æ£ãï¼ ContactView
ãä¿®æ£ãï¼ãã㦠ContactsPresenter#bind()
ãä¿®æ£ããå¿
è¦ãããã§ãããï¼
追å ãã¿ã³ãè¤æ°ã«ãªã£ãã ãã§ãåãã§ãï¼
ã¤ã¾ãï¼ãã¬ã¼ã³ã¿ã¼ã¨ãã¥ã¼ã¯ã¾ãã£ããççµåã«ãªã£ã¦ããªãã®ã§ãï¼
ããã ã£ãã Display
ãªãã¦ã¤ã³ã¿ãã§ã¼ã¹ãªãã¦ãããããããªãã§ããããï¼
éªéãªã ãã«è¦ãã¾ãï¼
ã¦ã£ã¸ã§ããã«ã¤ãã³ããã³ãã©ãç»é²ããã®ããã¬ã¼ã³ã¿ã¼ã§ããã®ã¯ããããè¦ãã¾ãï¼
Contacts2
ç¶ã㦠Part IIï¼
ãã¡ã㯠XML ã§ã¦ã£ã¸ã§ããã®æ§æãå®ç¾©ãã UIBinder ã使ã£ãå ´åã® MVP ã«ã¤ãã¦ï¼
ãã®ãµã³ãã«ã Contacts2 ã§ãï¼
å®éã«ã¯åè¿° (Part Iï¼) ã® Contacts ã¨ã®å·®åãªã®ã§ï¼ã¢ã¸ã¥ã¼ã«åã Contacts ã®ã¾ã¾ï¼
ã§ãããã§ã¯ Contacts2 ã¨å¼ã³ã¾ãï¼
å
ã® Contacts ã§ã¯ï¼ãã¬ã¼ã³ã¿ã¼å®è£
ã¯ã©ã¹ã¯ã¿ããªå
±éã® Presenter
ã¤ã³ã¿ãã§ã¼ã¹ãå®è£
ãã¦ããã ãã§ãããï¼Contacts2 ã§ã¯åå¥ã®ãã¬ã¼ã³ã¿ã¼ãã¨ã«ã¤ã³ã¿ãã§ã¼ã¹ãå®ç¾©ãã¾ãï¼
ã©ãã«å®ç¾©ãããã¨ããã¨ï¼ï¼ï¼
package com.google.gwt.sample.contacts.client.view; public interface ContactsView<T> { public interface Presenter<T> { void onAddButtonClicked(); void onDeleteButtonClicked(); void onItemClicked(T clickedItem); void onItemSelected(T selectedItem); } ï¼ï¼ï¼
ãªãã¨ãããã¨ãï¼ä»åº¦ã¯åå¥ã®ãã¥ã¼ã®ã¤ã³ã¿ãã§ã¼ã¹ããããã¬ãã«ã®ã¤ã³ã¿ãã§ã¼ã¹ã¨ãã¦å®ç¾©ãã¦ï¼ãã®ãã¹ãããåã¨ãã¦åå¥ã®ãã¬ã¼ã³ã¿ã¼ã®ã¤ã³ã¿ãã§ã¼ã¹ãå®ç¾©ããã®ã§ãï¼
ããã§ãã¬ã¼ã³ã¿ã¼ã®å®è£
ã¯ã©ã¹ã¯ãããªãã¾ãï¼
package com.google.gwt.sample.contacts.client.presenter; public class ContactsPresenter implements Presenter, ContactsView.Presenter<ContactDetails> { private final ContactsView<ContactDetails> view; ï¼ï¼ï¼
ãã¼ã¿ã
ï¼ï¼ï¼
ãããªæãã§ããï¼
- Contacts ã®å ´å
- class XxxPresenter
- interface Display
- class XxxView implements XxxPresenter.Display
- class XxxPresenter
- Contacts2 ã®å ´å
- class XxxPresenter implements XxxView.Presenter
- interface XxxView
- interface XxxPresenter
- class XxxViewImpl implements XxxView
UIBinder ã使ãã ãã§ãããªé¢¨ã«æ§æãå¤ããããå¾ãªãã£ã¦ã®ã¯ï¼Contacts ã® MVP ã®ããããå¦å®ã«èªã£ã¦ãããããªãã§ããããï¼
åè¿°ã® Contacts ãµã³ãã«ã§ã¯ãã¬ã¼ã³ã¿ã¼ã®å®è£
ã¯ã©ã¹ããã¥ã¼ããã¦ã£ã¸ã§ãããåã£ã¦ãã¦ã¤ãã³ããã³ãã©ãç»é²ãã¦ãã¾ããï¼
ããã Contacts2 㯠UIBinder ã使ã£ã¦ãã¦ï¼UIBinder ã§ã¯ãã¥ã¼ã«å®ç¾©ããã¤ãã³ããã³ãã©ãå¼ã³åºãããããã«è£ã§ãããããã£ã¦ããã¾ãï¼
å°æ¥ã® GWT ã§ã¯ UIBinder ã MVP ã«å¯¾å¿ãã¦ï¼ãã¬ã¼ã³ã¿ã¼ã®ã¤ãã³ããã³ãã©ã«ã¢ããã¼ã·ã§ã³ãä»ããã°ãããèªåçã«ç»é²ãã¦ããããããããããã¾ãããï¼ç¾æç¹ã§ã¯ãããªã£ã¦ãã¾ããï¼
ããã§ãã¥ã¼ã®æ¹ã§ãã¬ã¼ã³ã¿ã¼ã®ã¤ãã³ããã³ãã©ãå¼ã³åºãã¦ãããããã«ãªã£ã¦ã¾ãï¼
@UiHandler("addButton") void onAddButtonClicked(ClickEvent event) { if (presenter != null) { presenter.onAddButtonClicked(); } }
ãã¼ï¼ï¼ï¼
UIBinder ã使ããã©ããã¯ãã¥ã¼ã®å®è£
ã®é¸æè¢ã®ä¸ã¤ã«ãããªãã¦ï¼ãããããã§ãã¬ã¼ã³ã¿ã¼ã«å½±é¿ãã¦æ¬²ãããªãã§ãããï¼
ãããªã®ã«ï¼åå¥ã®ãã¬ã¼ã³ã¿ã¼ãã¨ã«ã¤ã³ã¿ãã§ã¼ã¹ãç¨æãããã©ããï¼åå¥ã®ãã¥ã¼ã®ã¤ã³ã¿ãã§ã¼ã¹ãã©ãã«å®ç¾©ãããï¼ã¤ãã³ããã³ãã©ã®ã«ã¼ãã£ã³ã°ãã©ãã§è¡ãããå¤ãã£ã¦ãã¾ãã¨ããã®ã¯ããã ãã¾ããï¼
ã¤ã¼ã UIBinder 使ãã ãã§å¤ãã£ã¦ãã¨ããå¤ããï¼
å®éï¼Contacts2 ã«ã¯é£çµ¡å
ã®ä¸è¦§ç»é¢ã¨ç·¨éç»é¢ãããã®ã§ããï¼UIBinder ã使ã£ã¦ããã®ã¯ä¸è¦§ç»é¢ã ãã§ï¼ç·¨éç»é¢ã¯ä½¿ã£ã¦ãã¾ããï¼
ã£ã¦ãããï¼ç·¨éç»é¢ã®æ¹ã¯ Contacts ãµã³ãã«ã®ã¾ã¾ (Contacts2.zip ã«ã¯å«ã¾ãã¦ããªãã®ã§ Contacts.zip ã®ããã®ã¾ã¾ä½¿ã) ãªã®ã§ï¼ä¸¡æ¹ã®æµåãå
¥ã交ãã£ã¦ã¾ãï¼
ãã¬ã¼ã³ã¿ã¼ã¨ãã¥ã¼ãççµåã«ãããã¦ã¤ã³ã¿ãã§ã¼ã¹ãå®ç¾©ãã¦ãããããªã®ã«ï¼ãã¥ã¼ã§ UIBinder ã使ããã©ããã§ãããªé¢¨ã«å¤§ããªå½±é¿ãããã¨ããã®ã¯ã¾ãã£ããç®çãæããã¦ããªãããã«æãã¾ãï¼
Contacts3
ãããããã¬ã¼ã³ã¿ã¼ã¨ãã¥ã¼ãççµåã«ããå¿
è¦ã¯ããã®ã§ããããï¼
ä¸ã¤ã®ãã¬ã¼ã³ã¿ã¼ã«å¯¾ãã¦ç°ãªã£ãè¤æ°ã®ãã¥ã¼ã使ããããã«ããï¼ï¼ï¼
失ããã®ãä½ããªããªãççµåãç®æãã¦ãããã¨ã¯æãã¾ããï¼å®éã¯ä¸ã§è¦ãããã«ã¦ã£ã¸ã§ããã®å¤æ´ã«ããèãããã¦ããªãããã§ï¼ççµåã¯å®ç¾ãã¦ãã¾ããï¼
ããããç®æããã¨ã«ç¡çãããããããªãã®ããªï¼
ã¢ãã«ã¨ãã¬ã¼ã³ã¿ã¼ï¼ãã¥ã¼ã®ãã¢ãççµåã§ãããã¨ã«ã¯æå³ãããã¨æããã©ï¼ãã¬ã¼ã³ã¿ã¼ã¨ãã¥ã¼ã®é¢ä¿ã¯çã«ãã¦ããããããªãããããªãããªãï¼
ãããããã¨ã¯å½¹å²ãåæ
ãã¦ã³ã¼ããã¹ãããªæ´çãã¦è¦éãããããããã¨ã§ï¼ä¸ã¤ã®ãã¬ã¼ã³ã¿ã¼ã«å¯¾ãã¦è¤æ°ã®ãã¥ã¼ã使ã£ãããããã¨ãããªãã¨æããï¼
ãããªãã㧠(ã©ããªããã§ï¼)ï¼ãã¬ã¼ã³ã¿ã¼ã¨ãã¥ã¼ã®ççµåã諦ãã¦ï¼UIBinder ã使ããã Ext GWT ã使ãããï¼ä¸è²«ããæ¹æ³ã§ãã¬ã¼ã³ã¿ã¼ã¨ãã¥ã¼ãå®è£
ã§ããããã«ãã¦ã¿ã¾ããï¼
ãããåæã« Contacts3 ã¨å¼ã³ã¾ãï¼
ççµåã諦ããã¨ãããã¨ã§ï¼ãã¬ã¼ã³ã¿ã¼ã¨ãã¥ã¼ã®ãã¢ã¯ä¸è®æçã§åãããã±ã¼ã¸ã«ç½®ãã¡ããã¾ãï¼
ãã§ï¼ç»é¢ãããã¯ç»é¢ã®ã°ã«ã¼ããã¨ã«ããã±ã¼ã¸ãåããæ¹ãæ´çã§ãã¦ããããããªãããªï¼
ãã®ãµã³ãã«ã¯ç»é¢ã2ã¤ãããªãããå
¨é¨åãããã±ã¼ã¸ã§ããããã ãã©ï¼ãªãã¨ãªãä¸è¦§ç»é¢ã¯ list
ããã±ã¼ã¸ï¼ç·¨éç»é¢ã¯ edit
ããã±ã¼ã¸ã«åãã¦ã¿ã¾ããï¼
ãã®ä¸è¦§ç»é¢ã®ãã¬ã¼ã³ã¿ã¼ï¼
package com.google.gwt.sample.contacts.client.list; public class ContactsPresenter implements Presenter { private final ContactsView view; ï¼ï¼ï¼
ããã¦ãã¥ã¼ï¼
package com.google.gwt.sample.contacts.client.list; public class ContactsView extends Composite { private ContactsPresenter presenter; public void setPresenter(ContactsPresenter presenter) { this.presenter = presenter; } ï¼ï¼ï¼
ã©ã¡ããåå¥ã®ãã¥ã¼ããã¬ã¼ã³ã¿ã¼ã®ã¤ã³ã¿ãã§ã¼ã¹ã¯ç¨æããï¼ç¸æ¹ã¨ãªãå®è£
ã¯ã©ã¹ãç´æ¥ç¸äºåç
§ãã¦ã¾ãï¼
ã©ã®ãã¥ã¼ã«ãç¸æ¹ã®ãã¬ã¼ã³ã¿ã¼ãåãåã setPresenter(XxxPresenter)
ã¡ã½ãããæããã¾ãï¼
ã¤ãã³ãã®ã«ã¼ãã£ã³ã°ã¯ãã¥ã¼å´ã§ããã¾ãï¼
UIBinder ã使ã£ãä¸è¦§ç»é¢ã®å ´åï¼
@UiHandler("addButton") void onAddButtonClicked(ClickEvent event) { if (presenter != null) { presenter.onAddButtonClicked(); } }
UIBinder ã使ããªãç·¨éç»é¢ã®å ´åï¼
public void setPresenter(final EditContactPresenter presenter) { getSaveButton().addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { presenter.onSaveButtonClicked(); } }); ï¼ï¼ï¼
ãããªãã㧠(ã©ããªããã§ï¼)ï¼ä¸éå端ãªççµåãæ¨ã¦ããã¨ã§ï¼å¤å°ã¯ä¸è²«æ§ãããï¼ç»é¢ã®å¤æ´ã§ãã¤ã³ã¿ãã§ã¼ã¹ã¨å®è£
ã¯ã©ã¹ã®ä¸¡æ¹ãåæã«ä¿®æ£ãããããªãã¨ããªãããã«ãã¦ã¿ã¾ããï¼
ä¿®æ£ãããµã³ãã«å
¨ä½ã¯ãã¡ãã«ç½®ãã¦ã¾ãï¼ æ¶ãã¡ãã£ãï¼
Contacts 㨠Contacts2 ã® MVP ãµã³ãã«ã§æ°ã«ãªã£ãã®ã¯ããã ããããªããã ãã© (Contacts2 ã® ColumnDefinition
辺ããå¾®å¦ãªæ°ã)ï¼ã¾ãã¯ããã ãè¦ç´ãã¦ã¿ã¾ããï¼
ã§ãã¾ãï¼ããã ãå°ããªç»é¢ã§ãã®èª¿åã ã¨ï¼æ¬æ ¼çãªç»é¢ã«ç«ã¡åããã«ã¯ãã£ã¨å¼·åãªæ¦å¨ãå¿
è¦ã ãããªãã¨ããæãï¼
GWT ç¨ã® MVC/MVP ãã¬ã¼ã ã¯ã¼ã¯ããããããããããªã®ã§ï¼èª¿ã¹ãã¨ãããã®ãããããªãï¼
ã£ã¦ãããï¼GWT (Java) ã«éããããï¼ããå æ¸ JavaScript ã«ç«ã¡åããæ¹ã幸ãã«è¿ãããã¨æã£ããããªãã§ããªãï¼
Unit Test
Contacts ããã³ Contacts2 ãåå¥ã®ãã¥ã¼ãã¨ã«ã¤ã³ã¿ãã§ã¼ã¹ãç¨æãã¦ããã®ã¯ï¼åä½ãã¹ããããããããããã¨ããçç±ãããããããã¾ããï¼
å®éï¼Contacts ãµã³ãã«ã«ã¯ GWT ã®ç°å¢ã使ã£ã¦å®éã®ãã¥ã¼ãã¤ã³ã¹ã¿ã³ã¹åãã¦ãã¹ãããããã® ExampleGWTTest
ã¨åæã«ï¼GWT ç°å¢ãç¨æããã«ãã¥ã¼ã®ã¢ãã¯ã使ã£ã¦ãã¹ãããããã® ExampleJRETest
ãç¨æããã¦ãã¾ãï¼
ãã®ãµã³ãã«ã§ã¯ã¢ãã¯ãä½æããããã®ã©ã¤ãã©ãªã¨ã㦠EasyMock ã使ã£ã¦ããã®ã§ããï¼EasyMock ClassExtension ã¯ä½¿ããã¦ãã¾ããï¼
ãã®ããã«ãã¥ã¼ã®ã¤ã³ã¿ãã§ã¼ã¹ããªãã¨ãã¬ã¼ã³ãã¼ã·ã§ã³ãåä½ãã¹ãã§ããªãã®ã§ãããï¼
ãããï¼EasyMock ClassExtension ã使ãã°å®è£
ã¯ã©ã¹ã«å¯¾ããã¢ãã¯ãä½æãããã¨ã ã£ã¦ã§ããããã§ãï¼
ã£ã¦ãããï¼EasyMock3 ã§ã¯ ClassExtension ãçµ±åããã¦åç¬ã§ãã¯ã©ã¹ã«å¯¾ããã¢ãã¯ãä½ããããã«ãªã£ãã®ãï¼
ãããªãã㧠(ã©ããªããã§)ï¼Contacts3 ã® ExampleJRETest
ã§ã¯ EasyMock3 ã使ã£ã¦å®è£
ã¯ã©ã¹ã§ãããã¥ã¼ã®ã¢ãã¯ãä½æããããã«ãã¦ã¾ãï¼
注æç¹ã¨ãã¦ã¯ï¼GWT ã® UIObject
ã¨ããã¯ã©ã¹ã® static
ãã£ã¼ã«ããåæåãããéã« GWT ã®ç°å¢ããªãã¨ãã£ã¦ä¾å¤ãã¹ãã¼ãããªãããã«ããããã®ãã¾ããªããå¿
è¦ã§ãï¼
protected void setUp() { GWTMockUtilities.disarm(); ï¼ï¼ï¼ } protected void tearDown() throws Exception { GWTMockUtilities.restore(); }