Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Business Delegate Pattern

Business Delegate Pattern

The Business Delegate Pattern is used to decouple the presentation layer from the business layer. It is basically used to reduce communication or the remote query functionality of business layer code in the presentation layer code. In the business layer we have the following entities.


Summary

Intent

Abstract and encapsulate the application's access logic, thereby providing the presentation layer with access to the business logic layer.

Main problem solved

Use Cases

Implementation

Key code

Application Example

Advantages

Disadvantages

Usage recommendation

Notes

Main roles included

- declare methods for accessing business logic. - implement the business delegate interface, encapsulating calls to the business service. - the interface or class of the business logic layer, containing actual business operations. - the front end of the application, accessing business logic through the business delegate. - contains the core business logic of the application.

The business delegate pattern helps maintain application flexibility and maintainability by providing an abstraction layer for accessing business logic.


Create a class that implements

We will create Client、BusinessDelegate、BusinessService、LookUpService、JMSService and EJBService to represent the various entities in the business delegate pattern.

BusinessDelegatePatternDemo Class uses BusinessDelegate and Client to demonstrate the usage of the business delegate pattern.

UML diagram of the Business Delegate pattern

Step 1

Create the BusinessService interface.

BusinessService.java

public

interface

BusinessService

{

public

void

doProcessing

(

)

;

}

Step 2

Create the entity service classes.

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

"

)

;

}

}

Step 3

Create the business lookup service.

BusinessLookUp.java

public

class

BusinessLookUp

{

public

BusinessService

getBusinessService

(

String

serviceType

)

{

if

(

serviceType

.

equalsIgnoreCase

(

"

EJB

"

)

)

{

return

new

EJBService

(

)

;

}

else

{

return

new

JMSService

(

)

;

}

}

}

Step 4

Create the business delegate.

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

(

)

;

}

}

Step 5

Create the client.

Client.java

public

class

Client

{

BusinessDelegate

businessService

;

public

Client

(

BusinessDelegate

businessService

)

{

this

.

businessService

=

businessService

;

}

public

void

doTask

(

)

{

businessService

.

doTask

(

)

;

}

}

Step 6

Use the BusinessDelegate and Client classes to demonstrate the business delegate pattern.

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

(

)

;

}

}

Step 7

Run the program, output result:



Processing task by invoking EJB Service

Processing task by invoking JMS Service