業務表明模式

業務表明模式(Business Delegate Pattern)用於對錶示層和業務層解耦。它基本上是用來減小通訊或對錶示層代碼中的業務層代碼的遠程查詢功能。在業務層中咱們有如下實體。java

  • 客戶端(Client) - 表示層代碼能夠是 JSP、servlet 或 UI java 代碼。
  • 業務表明(Business Delegate) - 一個爲客戶端實體提供的入口類,它提供了對業務服務方法的訪問。
  • 查詢服務(LookUp Service) - 查找服務對象負責獲取相關的業務實現,並提供業務對象對業務表明對象的訪問。
  • 業務服務(Business Service) - 業務服務接口。實現了該業務服務的實體類,提供了實際的業務實現邏輯。

實現

咱們將建立 ClientBusinessDelegateBusinessServiceLookUpServiceJMSService 和 EJBService 來表示業務表明模式中的各類實體。ide

BusinessDelegatePatternDemo,咱們的演示類使用 BusinessDelegate 和 Client 來演示業務表明模式的用法。this

業務表明模式的 UML 圖

步驟 1

建立 BusinessService 接口。spa

BusinessService.java

public interface BusinessService { public void doProcessing(); }

步驟 2

建立實體服務類。code

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

建立業務表明。blog

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 類來演示業務表明模式。get

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

執行程序,輸出結果:servlet

Processing task by invoking EJB Service Processing task by invoking JMS Service
相關文章
相關標籤/搜索