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.
- Client - Presentation layer code can be JSP, servlet, or UI java code.
- Business Delegate (Business Delegate) - an entry class provided for client entities, which provides access to business service methods.
- LookUp Service (LookUp Service) - the lookup service object is responsible for obtaining the relevant business implementation and providing business object access to the business delegate object.
- Business Service (Business Service) - the business service interface. The concrete class implementing this business service provides the actual business implementation logic.
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
- Solve the coupling problem between the presentation layer and the business logic layer, allowing the presentation layer to indirectly access the business logic layer through the business delegate.
Use Cases
- When there is a need to clearly separate the presentation layer and business logic layer in a multi-tier application.
Implementation
- Business delegate interface: define methods for accessing business logic.
- Business delegate implementation: implement the business delegate interface, encapsulating the logic of calling the business logic layer.
- Business service: the interface or class of the business logic layer, containing business operations.
Key code
- Business delegate interface: declare methods for accessing business logic.
- Business delegate implementation: implement the interface, containing code that calls the business service.
- Business service: the concrete implementation of the business logic.
Application Example
- Web application: the web layer serves as the presentation layer, accessing the back-end service layer through the business delegate.
Advantages
- Decoupling of presentation layer and business logic layer: the business delegate acts as an intermediate layer, reducing coupling.
- Centralized access logic: simplify the presentation layer code, centralizing access logic in the business delegate.
- Easy to maintain and extend: when adding new business logic access, only the business delegate needs to be modified.
Disadvantages
- May increase complexity: for simple applications, may add unnecessary levels of abstraction.
Usage recommendation
- When developing a multi-tier application that needs a clear separation between the presentation layer and business logic layer, consider using the business delegate pattern.
Notes
- The business delegate should be as concise as possible, avoiding complex logic.
Main roles included
-
Business Delegate Interface (Business Delegate Interface):
- Declare methods for accessing business logic.
-
Business Delegate Implementation (Business Delegate Implementation):
- Implement the business delegate interface, encapsulating calls to the business service.
-
Business Service (Business Service):
- The interface or class of the business logic layer, containing actual business operations.
-
Presentation Layer (Presentation Layer):
- The front end of the application, accessing business logic through the business delegate.
-
Business Logic Layer (Business Logic Layer):
- 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.
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