第2章Web层设计模式 Concrete Command(具体命令) 定义接收对象与操作之间的关联 Invoker(调用者) 请求执行请求的命令 · Receiver(接收者) 知道如何进行与执行请求相关的操作 框图27显示了 Command模式的调用序列。 ConcreteHandler1 Receiver 1: new Command 2: 1 action Command实现 宾馆管理视图的命令类包装在框图28中。实际上有三种命令对象,包括 CreateComm and、 Findcommand与 Remove Command CreateCommand FindCommandRemoveCommand )L+executed+executed 每个对象有一个 execute方法,映射该命令的操作。 Command接口的源代码如下 package hotel.util; import javax. servlet ServletException; import java.io IOException 表示一个 Command对象。根据 Command设计模式,它将请求包装成对象,用不同请求 将客户机参数化
实用J2EE设计模式编程指南 public interface Command i public String execute( RequestHelper helper hrows ServletException, IOException 这个接口提供了三个实现,分别是 Create HotelCommand、 Remove HotelCommand与Fnd 以下类实现 Create命令的 Command接口 import hotel. util. Command import hotel util. HotelDetail nport hotel. businesstier HotelBusinessDelegate import hotel. businesstier Hotel BusinessDelegateImpl import java. util. Properties import javax. servlet ServletResponse import javax. servlet ServletException: import java. io. IOException pport java. math. BigDecimal; chass CreateHotelCommmand implements command i private HotelDetails hoteldetails null; private HotelBusinessDelegate delegate new Hotel BusinessDelegateImpl( public CreateHotelCormmand()( private String getDisplayMessage( Properties properties return properties. getProperty(id")+ is successfully created public synchronized String execute( RequestHelper helper throws ServletException, IOException Properties properties helper getProperties() hoteldetails new HotelDetails( properties, getProperty ("name") properties. getProperty( properties. getProperty ("regionorTowm") properties. getProperty(type") properties. getProperty("chain") (BigDecimal)properties. get("swimmingPool") (BigDecimal)properties. get("gynm') BigDecimal)properties. get("conferenceRooms")
第2章Web层设计模式 delegate. createHotel (properties. getProperty (id), hoteldetails elper. get Request(). setAttribute( result this getDisplayMessage( properties y helper getRequest(). setAttribute tel", hoteldetails ) y catch( Exception e )i helper getRequest().setAttribute( 'result" Error processing Create Hotel Command e tostring()) /errorPage jsp return "/adminHotelResult, jsp 以下类实现 Lremove命令的 Command接口 import hotel util. HotelDetails: import businesstier HotelBusinessDelegateImpl: import java. util. Properties; mport java. 10. IoExcer import javax. servlet ServletException class RemoveHotelcommand implements Command private HotelDetails hoteldetails private HotelBusinessDelegate delegate new HotelBusinessDelegateImpl public RemoveHotelcommand( )I private string getDisplayMessage( String id ) return Hotel Id.+ id+"is successfully removed oublic synchronized string execute( RequestHelper helper throws ServletException, IOException try String id helper, getProperties(). getProperty(id) hoteldetails= delegate. getHotel( id helper getRequest(.setAttribute('result his. getDisplayMessage( id )
实用J2EE设计模式编程指南 helper getRequest().setAttribute(" hotel, hoteldetails s catch( Exception e)( e printstackTrace() helper getRequest(). setAttribute("result Error processing Remove Hotel Command"+ e tostring() return "/errorPage. sp return /adminHotelResult. sp 以下类实现fnd命令的 Command接口: package hotel. presentationtier import hotel util. Command import hotel, util. HotelDetails import hotel util. RequestHely import hotel. businesstier HotelBusinessDelegate import hotel. businesstier Hotel BusinessDelegateImpl import java.util. Properties import java. io. IOException; t jave class FindHotelCommand implements Command i private HotelDetails hotelde private HotelBusinessDelegate delegate new Hotel BusinessDelegateImpl() blic FindHotelcormand private string getDisplayMessage( Properties properties return properties. getProperty("id) is found public synchronized String execute( RequestHelper helper throws ServletException, IOException try i Properties properties= helper. get Properties( HotelDetails hoteldetails =delegate. getHotel( properties get Property('ia')) setAttribute("result", this, getDisplayMessage( properties helper getRequest(). setAttribute(.hotel",hoteldetails s catch( Exception e)( e printstackTrace(
第2章Web层设计模式 helper getRequest(), setAttribute( Error processing Find Hotel Command.+ e tostring()) return ' /adminHotelResult jsp 注意这三个命令对象都能访问包,因为 Command对象的客户机不需要知道每个对象的 特定具体类。所有命令类型可以用 Command接口统一处理。 下节介绍如何用抽象工厂类 CommandFactory创建和定义这些 Command对象 抽象工厂设计模式 由于有多种命令类型,因此要用整洁的方式建立这些命令,将其交给控制器,使控制 器能够委托。为此,我们使用 Abstract Factory设计模式。 Abstract Factory可以创建相关对 象系列而不暴露其具体类。 图29显示了 Abstract Factory设计模式的结构 Concretefactory2ConcreteFactoryt ConcreteProduct 1 ConcreteProduct 2 Abstract Factory设计模式的参与者如下: Abstract Factory(抽象工厂) 提供创建产品的接口抽象 · Product(产品) 提供产品抽象的接口 Concrete Factory(具体工厂) 创建具体产品的抽象⊥厂实现方法