1. 首页
  2. 设计模式2

31-三十一、业务代表模式(Business Delegate Pattern)

业务代表模式(Business Delegate Pattern)用于对表示层和业务层解耦

业务代表模式用来减少通信或对表示层代码中的业务层代码的远程查询功能

在业务层中我们有以下实体:

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

实现

img_1.png

1、创建 ClientBusinessDelegateBusinessServiceLookUpServiceJMSServiceEJBService 来表示业务代表模式中的各种实体
2、定义类 BusinessDelegatePatternDemo 使用 BusinessDelegateClient 来演示业务代表模式的用法

范例

1. 创建 BusinessService 接口

BusinessService.java

// author: 研发军团(www.ycbbs.vip)
// Copyright © 2015-2065 www.ycbbs.vip. All rights reserved.

package com.ycbbs.tech.gof;


public interface BusinessService {
   public void doProcessing();
}

2. 创建实体服务类

EJBService.java

// author: 研发军团(www.ycbbs.vip)
// Copyright © 2015-2065 www.ycbbs.vip. All rights reserved.

package com.ycbbs.tech.gof;


public class EJBService implements BusinessService {

   @Override
   public void doProcessing() {
      System.out.println("Processing task by invoking EJB Service");
   }
}

JMSService.java

// author: 研发军团(www.ycbbs.vip)
// Copyright © 2015-2065 www.ycbbs.vip. All rights reserved.

package com.ycbbs.tech.gof;


public class JMSService implements BusinessService {

   @Override
   public void doProcessing() {
      System.out.println("Processing task by invoking JMS Service");
   }
}

3. 创建业务查询服务

BusinessLookUp.java

// author: 研发军团(www.ycbbs.vip)
// Copyright © 2015-2065 www.ycbbs.vip. All rights reserved.

package com.ycbbs.tech.gof;


public class BusinessLookUp {
   public BusinessService getBusinessService(String serviceType){
      if(serviceType.equalsIgnoreCase("EJB")){
         return new EJBService();
      }else {
         return new JMSService();
      }
   }
}

4. 创建业务代表

BusinessDelegate.java

// author: 研发军团(www.ycbbs.vip)
// Copyright © 2015-2065 www.ycbbs.vip. All rights reserved.

package com.ycbbs.tech.gof;


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

// author: 研发军团(www.ycbbs.vip)
// Copyright © 2015-2065 www.ycbbs.vip. All rights reserved.

package com.ycbbs.tech.gof;


public class Client {

   BusinessDelegate businessService;

   public Client(BusinessDelegate businessService){
      this.businessService  = businessService;
   }

   public void doTask(){        
      businessService.doTask();
   }
}

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

BusinessDelegatePatternDemo.java

// author: 研发军团(www.ycbbs.vip)
// Copyright © 2015-2065 www.ycbbs.vip. All rights reserved.

package com.ycbbs.tech.gof;


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

编译运行以上 Java 范例,输出结果如下

$ javac -d . src/main/com/ycbbs/tech/gof/BusinessDelegatePatternDemo.java
$ java  com.ycbbs.tech.gof.BusinessDelegatePatternDemo
Processing task by invoking EJB Service
Processing task by invoking JMS Service

希望读者能够给小编留言,也可以点击[此处扫下面二维码关注微信公众号](https://www.ycbbs.vip/?p=28 "此处扫下面二维码关注微信公众号")

JS中文网,Javascriptc中文网是中国领先的新一代开发者社区和专业的技术媒体,一个帮助开发者成长的社区,是给开发者用的 Hacker News,技术文章由为你筛选出最优质的干货,其中包括:Android、iOS、前端、后端等方面的内容。目前已经覆盖和服务了超过 300 万开发者,你每天都可以在这里找到技术世界的头条内容。

本文著作权归作者所有,如若转载,请注明出处

转载请注明:文章转载自「 Java极客技术学习 」https://www.javajike.com

标题:31-三十一、业务代表模式(Business Delegate Pattern)

链接:https://www.javajike.com/article/2391.html

« 32-三十二、组合实体模式 (Composite Entity Pattern)
30-三十、MVC 模式»

相关推荐

QR code