网站设计模式 专题
您的位置:web > 网站设计模式专题 > 业务代表模式
业务代表模式
作者:--    发布时间:2019-11-20

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

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

实现

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

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

业务代表模式的 uml 图

步骤 1

创建 businessservice 接口。

businessservice.java

public interface businessservice {
   public void doprocessing();
}

步骤 2

创建实体服务类。

ejbservice.java

public class ejbservice implements businessservice {

   @override
   public void doprocessing() {
      system.out.println("processing task by invoking ejb service");
   }
}

jmsservice.java

public class jmsservice implements businessservice {

   @override
   public void doprocessing() {
      system.out.println("processing task by invoking jms service");
   }
}

步骤 3

创建业务查询服务。

businesslookup.java

public class businesslookup {
   public businessservice getbusinessservice(string servicetype){
      if(servicetype.equalsignorecase("ejb")){
         return new ejbservice();
      }else {
         return new jmsservice();
      }
   }
}

步骤 4

创建业务代表。

businessdelegate.java

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

创建客户端。

client.java

public class client {
   
   businessdelegate businessservice;

   public client(businessdelegate businessservice){
      this.businessservice  = businessservice;
   }

   public void dotask(){     
      businessservice.dotask();
   }
}

步骤 6

使用 businessdelegate 和 client 类来演示业务代表模式。

businessdelegatepatterndemo.java

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
网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册