美文网首页
第二式 业务代表模式

第二式 业务代表模式

作者: 穹生变 | 来源:发表于2019-07-31 16:42 被阅读0次

1.简介

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

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

2.实现

我们将创建 Client、BusinessDelegate、BusinessService、LookUpService、JMSService 和 EJBService 来表示业务代表模式中的各种实体。
BusinessDelegatePatternDemo,我们的演示类使用 BusinessDelegate 和 Client 来演示业务代表模式的用法。


image.png

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

相关文章

  • 第二式 业务代表模式

    1.简介 业务代表模式用于对表示层和业务层解耦。它基本上是用来减少通信或对表示层代码中的业务层代码的远程查询功能。...

  • 业务代表模式

    参考文章 http://www.runoob.com/design-pattern/business-delega...

  • 业务代表模式

    前言: 因为现在设计模式在网络上已经泛滥,但是还是有好多程序员不能够灵活的运用设计模式,这个是对设计模式简单的介绍...

  • Java业务代表模式

  • 设计模式之业务代表模式

    简介 业务代表模式(Business Delegate Pattern)用于对表示层和业务层解耦。它基本上是用来减...

  • 设计模式|菜鸟教程 - C5 J2EE 模式

    --书写中--- 0 Intro MVC 模式(MVC Pattern)业务代表模式(Business Deleg...

  • Seata-AT模式

    Seata-AT模式 概念:AT模式是一种无侵入的分布式事务解决方案,在 AT 模式下,用户只需关注自己的“业务 ...

  • 架构衍生工具——业务模式画布

    业务模式画布 一、概述 业务模式画布(亦称为业务模式九要素)是一种用来描述业务模式、可视化业务模式、评估业务模式以...

  • 业务代表加入

    第一️@首先准备好4个内容: 1,身份证正、反面; 2,手持身份证拍照(不能戴眼镜,照片要清晰,不能美颜); 3,...

  • 2020-11-27 分布式事务

    分布式事务 AT模式 -> seata实现的AT模式->对业务0入侵,使用undo_log回滚(可能有脏写) TC...

网友评论

      本文标题:第二式 业务代表模式

      本文链接:https://www.haomeiwen.com/subject/pxutdctx.html