1.业务代表模式简介
业务代表模式(Business Delegate Pattern)用于对表示层和业务层解耦。它基本上是用来减少通信或对表示层代码中的业务层代码的远程查
询功能。在业务层中我们有以下实体。
客户端(Client) - 表示层代码可以是 JSP、servlet 或 UI java 代码。
业务代表(Business Delegate) - 一个为客户端实体提供的入口类,它提供了对业务服务方法的访问。
查询服务(LookUp Service) - 查找服务对象负责获取相关的业务实现,并提供业务对象对业务代表对象的访问。
业务服务(Business Service) - 业务服务接口。实现了该业务服务的实体类,提供了实际的业务实现逻辑。
2.Demo示例
我们将创建 Client、BusinessDelegate、BusinessService、LookUpService、JMSService 和 EJBService 来表示业务代表模式中的各种实体。
BusinessDelegatePatternDemo,我们的演示类使用 BusinessDelegate 和 Client 来演示业务代表模式的用法。
interface BusinessService { void doProcess(); } class EJBService implements BusinessService { public void doProcess() { System.out.println("Process from EJBService"); } } class JMSService implements BusinessService { public void doProcess() { System.out.println("Process from JMSService"); } } class BusinessLockup { public static BusinessService getService(String serviceType) { if (serviceType.equalsIgnoreCase("EJB")) { return new EJBService(); } else { return new JMSService(); } } } class BusinessDelegate { private String serviceType; private BusinessService businessService; public void setServiceType(String serviceType) { this.serviceType = serviceType; } public void process() { businessService = BusinessLockup.getService(serviceType); businessService.doProcess(); } } class Client { private BusinessDelegate delegate; public Client(BusinessDelegate delegate) { this.delegate = delegate; } public void doTask() { delegate.process(); } } public class DelegatePatternDemo { public static void main(String args[]) { BusinessDelegate delegate = new BusinessDelegate(); delegate.setServiceType("EJB"); Client client1 = new Client(delegate); client1.doTask(); delegate.setServiceType("JMS"); Client client2 = new Client(delegate); client2.doTask(); } }
参考:http://www.runoob.com/design-pattern/business-delegate-pattern.html
原文地址:https://www.cnblogs.com/hellokitty2/p/10744592.html
时间: 2024-11-05 15:55:59