📅  最后修改于: 2020-12-21 02:07:16             🧑  作者: Mango
业务委托模式用于分离表示层和业务层。它基本上用于减少与表示层代码中的业务层代码的通信或远程查找功能。在业务层中,我们有以下实体。
客户端-表示层代码可以是JSP,Servlet或UI Java代码。
业务委托-客户实体的单一入口点类,以提供对业务服务方法的访问。
查找服务-查找服务对象负责获取相关业务的实施和提供业务的委托对象业务对象的访问。
商业服务-商业服务界面。具体的类实现此业务服务以提供实际的业务实现逻辑。
我们将创建一个Client , BusinessDelegate , BusinessService , LookUpService , JMSService和EJBService ,它们代表业务代表模式的各种实体。
BusinessDelegatePatternDemo,我们的演示课,将使用BusinessDelegate和客户端来演示如何使用业务委托模式的。
创建BusinessService接口。
BusinessService.java
public interface BusinessService {
public void doProcessing();
}
创建具体的服务类。
EJBService.java
public class EJBService implements BusinessService {
@Override
public void doProcessing() {
System.out.println("Processing task by invoking EJB Service");
}
}
JMSService.java
public class JMSService implements BusinessService {
@Override
public void doProcessing() {
System.out.println("Processing task by invoking JMS Service");
}
}
创建业务查找服务。
BusinessLookUp.java
public class BusinessLookUp {
public BusinessService getBusinessService(String serviceType){
if(serviceType.equalsIgnoreCase("EJB")){
return new EJBService();
}
else {
return new JMSService();
}
}
}
创建业务代表。
BusinessDelegate.java
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();
}
}
创建客户端。
客户端.java
public class Client {
BusinessDelegate businessService;
public Client(BusinessDelegate businessService){
this.businessService = businessService;
}
public void doTask(){
businessService.doTask();
}
}
使用BusinessDelegate和Client类来演示Business Delegate模式。
BusinessDelegatePatternDemo.java
public class BusinessDelegatePatternDemo {
public static void main(String[] args) {
BusinessDelegate businessDelegate = new BusinessDelegate();
businessDelegate.setServiceType("EJB");
Client client = new Client(businessDelegate);
client.doTask();
businessDelegate.setServiceType("JMS");
client.doTask();
}
}
验证输出。
Processing task by invoking EJB Service
Processing task by invoking JMS Service