扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:中国IT实验室 来源:中国IT实验室 2007年9月23日
关键字:
在本页阅读全文(共6页)
对于大多数应用程序动作,指向模型层的控制器条目是 ModelFacade 类。为符合 MVC 模式,ModelFacade 类包含一个响应模型层中每个事件的方法。根据动作的本质,界面将它委托给以下的一个或多个模型类:
LocalModel 类处理需要对本地设备上存储的数据进行访问的动作。例如,如果一个动作需要读写首选数据,ModelFacade 调用 LocalModel中合适的动作方法。
RemoteModelProxy类,它实现了 RemoteModel 接口,处理需要对 J2EE 服务器进行访问的动作,比如购票。RemoteModelProxy 中的方法对服务器端界面进行远程过程调用(remote procedure call,RPC) ,这种方式我们将在讨论后台时讲述。
SynchronizationAgent 类用本地数据同步化远程服务器端数据。在 Smart Ticket 程序中,只有评级实现了同步。这个代理有两个动作方法:synchronizeMovieRatings() 同步了评级;commitMovieRatings() 方法向后台提交已分析的同步请求,并更新本地存储的内容。 package com.sun.j2me.blueprints.smartticket.client.midp.model;
public class ModelFacade {
private SynchronizationAgent syncAgent;
private RemoteModelProxy remoteModel;
private LocalModel localModel;
// Action methods ...
public Reservation reserveSeats(String theaterKey,
String movieKey,
int[] showTime, Seat[] seats)
throws ApplicationException {
try {
return remoteModel.reserveSeats(theaterKey,
movieKey, showTime, seats);
} catch (ModelException me) {
// ...
}
}
public void purchaseTickets(Reservation reservation)
throws ApplicationException {
try {
remoteModel.purchaseTickets(reservation.getId());
localModel.addMovieRating(
new MovieRating(remoteModel.getMovie(reservation.getMovieId()),
reservation.getShowTime()));
} catch (ModelException me) {
// ...
}
return;
}
public void synchronizeMovieRatings(int
conflictResolutionStrategyId)
throws ApplicationException {
try {
syncAgent.synchronizeMovieRatings(conflictResolutionStrategyId);
return;
} catch (ModelException me) {
// ...
}
}
// ...
}
|
应用程序的服务端使用了很多 Enterprise JavaBeans 组件 (EJB) 来封装业务逻辑和管理与关系数据库的交互。当客户端的 RemoteModelProxy 向服务器端发出 RPC 调用时,HTTP servlet SmartTicketServlet 通过业务代理对象 SmartTicketBD 调用会话 EJB 中合适的动作方法 SmartTicketFacadeBean。根据请求性质,它进一步委托两个其他会话 bean 中的一个,TicketingBean 或 SynchronizingBean。一组实体 bean 在需要时使用 EJB 2.0 的容器托管的持久性来更新数据库。
package com.sun.j2me.blueprints.smartticket.server.web.midp;
public class SmartTicketServlet extends HttpServlet {
public static final String SESSION_ATTRIBUTE_SMART_TICKET_BD =
"com.sun.j2me.blueprints.smartticket.server.web.midp.SmartTicketBD";
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(true);
SmartTicketBD smartTicketBD = (SmartTicketBD)
session.getAttribute(SESSION_ATTRIBUTE_SMART_TICKET_BD);
// Calls handleCall() method and encodes the URL for
// session tracking
}
public int handleCall(SmartTicketBD smartTicketBD,
InputStream in, OutputStream out)
throws IOException, ApplicationException {
// Identifies the requested action method
// Executes the method, as selected in a switch statement
switch (method) {
// cases ...
case MessageConstants.OPERATION_GET_MOVIE: {
getMovie(smartTicketBD, call, successfulResult);
break;
}
// more cases ...
}
}
}
package com.sun.j2me.blueprints.smartticket.server.web.midp;
public class SmartTicketBD implements RemoteModel {
public static final String EJB_REF_FACADE =
"ejb/SmartTicketFacade";
private SmartTicketFacadeLocal facade;
private ServletContext servletContext = null;
public SmartTicketBD(ServletContext servletContext)
throws ApplicationException {
this.servletContext = servletContext;
try {
Context context = (Context)
new InitialContext().lookup("java:comp/env");
facade = ((SmartTicketFacadeLocalHome)
context.lookup(EJB_REF_FACADE)).create();
return;
} catch (Exception e) {
throw new ApplicationException(e);
}
}
public Movie getMovie(String movieKey)
throws ModelException, ApplicationException {
try {
MovieLocal movieLocal = facade.getMovie(movieKey);
Movie movie = new Movie(movieLocal.getId(),
movieLocal.getTitle(),
movieLocal.getSummary(),
movieLocal.getRating());
return movie;
} catch (SmartTicketFacadeException stfe) {
throw new
ModelException(ModelException.CAUSE_MOVIE_NOT_FOUND);
} catch (Exception e) {
throw new ApplicationException(e);
}
}
// Other action methods in RemoteModel interface ...
}
package com.sun.j2me.blueprints.smartticket.server.ejb;
public class SmartTicketFacadeBean implements SessionBean {
// ...
public MovieLocal getMovie(String movieId)
throws SmartTicketFacadeException {
try {
return movieHome.findByPrimaryKey(movieId);
} catch (FinderException fe) {
throw new
SmartTicketFacadeException("No matching movie.");
}
}
// ...
}
|
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。