设计模式笔记 – 业务代表模式

业务代表模式(Business Delegate Pattern)用于对表示层和业务层解耦。它基本上是用来减少通信或对表示层代码中的业务层代码的远程查询功能。在业务层中我们有以下实体。

  • 客户端(Client) – 表示层代码可以是 JSP、servlet 或 UI java 代码。
  • 业务代表(Business Delegate) – 一个为客户端实体提供的入口类,它提供了对业务服务方法的访问。
  • 查询服务(LookUp Service) – 查找服务对象负责获取相关的业务实现,并提供业务对象对业务代表对象的访问。
  • 业务服务(Business Service) – 业务服务接口。实现了该业务服务的实体类,提供了实际的业务实现逻辑。

示例

我们将创建 ClientBusinessDelegateBusinessServiceLookUpServiceJMSServiceEJBService 来表示业务代表模式中的各种实体。

BusinessDelegatePatternDemo,我们的演示类使用 BusinessDelegateClient 来演示业务代表模式的用法。

Java 实现

创建 BusinessService 接口

public interface BusinessService {
    public 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 BusinessLoopUp lookupServive = new BusinessLoopUp();
    private BusinessService businessService;
    private String serviceType;
    public void setServiceType(String serviceType) {
        this.serviceType = serviceType;
    }
    public void doTask() {
        businessService = lookupServive.getBusinessService(serviceType);
        businessService.doProcessing();
    }
}

创建客户端

public class Client {
    BusinessDelegate businessDelegate;
    public Client(BusinessDelegate businessDelegate) {
        this.businessDelegate = businessDelegate;
    }
    public void doTask() {
        businessDelegate.doTask();
    }
}

使用 BusinessDelegate 和 Client 类来演示业务代表模式

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();
    }
}

Python 实现

import abc

class BusinessService(abc.ABC):
    @abc.abstractmethod
    def do_processing(self): ...

class EJBService(BusinessService):
    def do_processing(self):
        print("Processing task by invoking EJB Service")

class JMSService(BusinessService):
    def do_processing(self):
        print("Processing task by invoking JMS Service")

class BusinessLoopUp:
    def get_business_service(self, service_type: str) -> BusinessService:
        if service_type == "EJB":
            return EJBService()
        else:
            return JMSService()

class BusinessDelegate:
    def __init__(self):
        self.lookup_service = BusinessLoopUp()
        self.business_service = None
        self.service_type = ''

    def set_service_type(self, service_type: str):
        self.service_type = service_type

    def do_task(self):
        self.business_service = self.lookup_service.get_business_service(self.service_type)
        self.business_service.do_processing()

class Client:
    def __init__(self, business_service: BusinessDelegate):
        self.business_service = business_service

    def do_task(self):
        self.business_service.do_task()

if __name__ == "__main__":
    business_delegate = BusinessDelegate()
    business_delegate.set_service_type("EJB")
    client = Client(business_delegate)
    client.do_task()
    business_delegate.set_service_type("JMS")
    client.do_task()

You may also like...

发表评论

您的电子邮箱地址不会被公开。