业务代表模式(Business Delegate Pattern)用于对表示层和业务层解耦,用来减少通信或对表示层代码中的业务层代码的远程查询功能。在业务层中我们有以下实体。
客户端(Client) - 表示层代码可以是 JSP、servlet 或 UI java 代码。
业务代表(Business Delegate) - 一个为客户端实体提供的入口类,它提供了对业务服务方法的访问。
查询服务(LookUp Service) - 查找服务对象负责获取相关的业务实现,并提供业务对象对业务代表对象的访问。
业务服务(Business Service) - 业务服务接口。实现了该业务服务的实体类,提供了实际的业务实现逻辑。
假设我们有以下业务服务层。
class EJBService {
    doProcessing() {
       console.log("Processing task by invoking EJB Service");
    }
}
class JMSService {
    doProcessing() {
       console.log("Processing task by invoking JMS Service");
    }
}服务查询层能对服务进行查找。
class BusinessLookUp {
    getBusinessService(serviceType){
        switch(serviceType.toUpperCase()) {
            case 'EJB':
                return new EJBService();
            default:
                return new JMSService();
        }
    }
}定义业务代表
class BusinessDelegate {
    constructor() {
        this.lookupService = new BusinessLookUp();
    }
    setServiceType(serviceType){
       this.serviceType = serviceType;
    }
    doTask(){
       this.businessService = this.lookupService.getBusinessService(this.serviceType);
       this.businessService.doProcessing();     
    }
}定义客户端
class Client {
    constructor(businessService){
       this.businessService  = businessService;
    }
    doTask(){      
        this.businessService.doTask();
    }
 }使用业务代表和客户端交互
const businessDelegate = new BusinessDelegate();
businessDelegate.setServiceType("EJB");
const client = new Client(businessDelegate);
client.doTask();
businessDelegate.setServiceType("JMS");
client.doTask();统一了业务出口,客户端不需要关心底层的模型,只需要关系业务代表暴露的模型。
本文内容仅供个人学习/研究/参考使用,不构成任何决策建议或专业指导。分享/转载时请标明原文来源,同时请勿将内容用于商业售卖、虚假宣传等非学习用途哦~感谢您的理解与支持!