業(yè)務(wù)代表模式

2021-11-26 17:26 更新

業(yè)務(wù)代表模式(Business Delegate Pattern)用于對(duì)表示層和業(yè)務(wù)層解耦。它基本上是用來(lái)減少通信或?qū)Ρ硎緦哟a中的業(yè)務(wù)層代碼的遠(yuǎn)程查詢功能。在業(yè)務(wù)層中我們有以下實(shí)體。

  • 客戶端(Client) - 表示層代碼可以是 JSP、servlet 或 UI java 代碼。
  • 業(yè)務(wù)代表(Business Delegate) - 一個(gè)為客戶端實(shí)體提供的入口類,它提供了對(duì)業(yè)務(wù)服務(wù)方法的訪問(wèn)。
  • 查詢服務(wù)(LookUp Service) - 查找服務(wù)對(duì)象負(fù)責(zé)獲取相關(guān)的業(yè)務(wù)實(shí)現(xiàn),并提供業(yè)務(wù)對(duì)象對(duì)業(yè)務(wù)代表對(duì)象的訪問(wèn)。
  • 業(yè)務(wù)服務(wù)(Business Service) - 業(yè)務(wù)服務(wù)接口。實(shí)現(xiàn)了該業(yè)務(wù)服務(wù)的實(shí)體類,提供了實(shí)際的業(yè)務(wù)實(shí)現(xiàn)邏輯。

實(shí)現(xiàn)

我們將創(chuàng)建 Client、BusinessDelegate、BusinessService、LookUpService、JMSServiceEJBService 來(lái)表示業(yè)務(wù)代表模式中的各種實(shí)體。

BusinessDelegatePatternDemo,我們的演示類使用 BusinessDelegateClient 來(lái)演示業(yè)務(wù)代表模式的用法。

業(yè)務(wù)代表模式的 UML 圖

步驟 1

創(chuàng)建 BusinessService 接口。

BusinessService.java

public interface BusinessService {
   public void doProcessing();
}

步驟 2

創(chuàng)建實(shí)體服務(wù)類。

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

創(chuàng)建業(yè)務(wù)查詢服務(wù)。

BusinessLookUp.java

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

步驟 4

創(chuàng)建業(yè)務(wù)代表。

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

創(chuàng)建客戶端。

Client.java

public class Client {
   
   BusinessDelegate businessService;

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

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

步驟 6

使用 BusinessDelegate 和 Client 類來(lái)演示業(yè)務(wù)代表模式。

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

驗(yàn)證輸出。

Processing task by invoking EJB Service
Processing task by invoking JMS Service


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)