业务代表模式(Business Delegate Pattern)
优质
小牛编辑
127浏览
2023-12-01
Business Delegate Pattern用于解耦表示层和业务层。 它基本上用于减少表示层代码中的业务层代码的通信或远程查找功能。 在业务层,我们有以下实体。
Client - 表示层代码可以是JSP,servlet或UI java代码。
Business Delegate - 客户实体提供对业务服务方法的访问的单一入口点类。
LookUp Service - 查找服务对象负责获取相关业务实现并提供业务对象访问业务委托对象。
Business Service - 商业服务界面。 具体类实现此业务服务以提供实际的业务实现逻辑。
实现 (Implementation)
我们将创建一个Client , BusinessDelegate , BusinessService , LookUpService , JMSService和EJBService代表Business Delegate模式的各种实体。
我们的演示类BusinessDelegate将使用BusinessDelegate和Client来演示Business Delegate模式的使用。
Step 1
创建BusinessService接口。
BusinessService.java
public interface BusinessService {
public void doProcessing();
}
Step 2
创建具体的服务类。
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");
}
}
Step 3
创建业务查询服务。
BusinessLookUp.java
public class BusinessLookUp {
public BusinessService getBusinessService(String serviceType){
if(serviceType.equalsIgnoreCase("EJB")){
return new EJBService();
}
else {
return new JMSService();
}
}
}
Step 4
创建业务代表。
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();
}
}
Step 5
创建客户端。
Client.java
public class Client {
BusinessDelegate businessService;
public Client(BusinessDelegate businessService){
this.businessService = businessService;
}
public void doTask(){
businessService.doTask();
}
}
Step 6
使用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();
}
}
Step 7
验证输出。
Processing task by invoking EJB Service
Processing task by invoking JMS Service