业务代表模式(Business Delegate Pattern)用于对表示层和业务层进行解耦。它基本上是用来减少通信或对表示层代码中的业务层代码的远程查询功能。
// 业务服务接口
public interface BusinessService {
void doProcessing();
}
// 具体业务服务实现
public class EJBService implements BusinessService {
@Override
public void doProcessing() {
System.out.println("Processing task by invoking EJB Service");
}
}
public class JMSService implements BusinessService {
@Override
public void doProcessing() {
System.out.println("Processing task by invoking JMS Service");
}
}
// 业务查询服务
public class BusinessLookUp {
public BusinessService getBusinessService(String serviceType) {
if(serviceType.equalsIgnoreCase("EJB")) {
return new EJBService();
} else {
return new JMSService();
}
}
}
// 业务代表
public class BusinessDelegate {
private BusinessLookUp lookupService = new BusinessLookUp();
private BusinessService businessService;
private String serviceType;
public void setServiceType(String serviceType) {
this.serviceType = serviceType;
}
public void doTask() {
businessService = lookupService.getBusinessService(serviceType);
businessService.doProcessing();
}
}
// 客户端
public class Client {
BusinessDelegate businessService;
public Client(BusinessDelegate businessService) {
this.businessService = businessService;
}
public void doTask() {
businessService.doTask();
}
}