Java设计模式快速入门之业务代表模式
2020-06-06 07:12
204 查看
6.3Java设计模式快速入门之业务代表模式
6.3.1概念
业务代表模式(Business Delegate Pattern)用于对表示层和业务层解耦。它基本上是用来减少通信或对表示层代码中的业务层代码的远程查询功能。在业务层中我们有以下实体。
- 客户端(Client) - 表示层代码可以是 JSP、servlet 或 UI java 代码。
- 业务代表(Business Delegate) - 一个为客户端实体提供的入口类,它提供了对业务服务方法的访问。
- 查询服务(LookUp Service) - 查找服务对象负责获取相关的业务实现,并提供业务对象对业务代表对象的访问。
- 业务服务(Business Service) - 业务服务接口。实现了该业务服务的实体类,提供了实际的业务实现逻辑。
6.3.2实现
我们将创建 Client、BusinessDelegate、BusinessService、LookUpService、JMSService 和 EJBService 来表示业务代表模式中的各种实体。
BusinessDelegatePatternDemo,我们的演示类使用 BusinessDelegate 和 Client 来演示业务代表模式的用法。
步骤1创建 BusinessService接口。
public interface BusinessService { public void doProcessing(); }
步骤2创建实体服务类。
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"); } }
步骤3创建业务查询服务。
public class BusinessLookUp { public BusinessService getBusinessService(String serviceType){ if(serviceType.equalsIgnoreCase("EJB")){ return new EJBService(); }else { return new JMSService(); } } }
步骤4创建业务代表。
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(); } }
步骤5创建客户端。
public class Client { BusinessDelegate businessService; public Client(BusinessDelegate businessService){ this.businessService = businessService; } public void doTask(){ businessService.doTask(); } }
步骤6使用 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(); } }
步骤7执行程序,输出结果:
Processing task by invoking EJB Service Processing task by invoking JMS Service
步骤8查看类关系图。
相关文章推荐
- Java设计模式快速入门之传输对象模式
- Java设计模式快速入门之数据访问对象模式
- Java设计模式快速入门之拦截过滤器模式
- Java设计模式快速入门之服务定位器模式
- Java设计模式快速入门之前端控制器模式
- Java设计模式快速入门之组合实体模式
- Java中23种设计模式--超快速入门及举例代码
- Java中23种设计模式--超快速入门及举例代码
- 使用Java开发Domino业务应用的设计模式 (cont. 1)
- java设计模式入门(1)策略模式(Strategy Pattern)
- 轻松快速掌握JAVA设计模式
- java深入浅出设计模式----简单的工厂模式(一)----业务逻辑分离
- JAVA设计模式--入门
- java 设计模式--适配器模式(实例快速理解)
- Java 基础入门随笔(10) JavaSE版——单例设计模式
- 使用Java开发Domino业务应用的设计模式 (cont. 2)
- JAVA入门,DAO设计模式
- java设计模式入门(2)观察者模式
- JAVA入门,DAO设计模式
- 轻松快速掌握JAVA设计模式