The Business representative mode (Delegate pattern) is used to decouple the presentation layer from the business layer. It is basically used to reduce communication or remote query functionality for business layer code in the presentation layer code. In the business layer we have the following entities.
- Client -The presentation layer code can be a JSP, servlet, or UI Java code.
- Business Delegate -A portal class provided for client entities that provides access to business service methods.
- Query Service -the Lookup service object is responsible for obtaining the relevant business implementation and providing access to the business object to the business Representative object.
- Business Services -business service interfaces. The entity class of the business service is implemented, and the actual business implementation logic is provided.
Realize
We will create Client,businessdelegate,businessservice,lookupservice,jmsservice and Ejbservice to represent the various entities in the business rep pattern.
Businessdelegatepatterndemo, our demo class uses businessdelegate and Client to demonstrate the use of the business rep pattern.
Step 1
Create the Businessservice interface.
Public Interface businessservice { publicvoid doprocessing ();}
Step 2
Create the Entity service class.
Public class Implements businessservice { @Override publicvoid doprocessing () { SYSTEM.OUT.PRINTLN ("Processing task by invoking EJB Service");} }
Public class Implements businessservice { @Override publicvoid doprocessing () { SYSTEM.OUT.PRINTLN ("Processing task by invoking JMS Service");} }
Step 3
Create a Business query service.
Public class businesslookup { public businessservice getbusinessservice (String servicetype) { If(Servicetype.equalsignorecase ("EJB")) { returnnew Ejbservice (); } Else { returnnew jmsservice (); }}}
Step 4
Create a business representative.
Public classBusinessdelegate {PrivateBusinesslookup Lookupservice =NewBusinesslookup (); PrivateBusinessservice Businessservice; PrivateString servicetype; Public voidSetservicetype (String servicetype) { This. servicetype =servicetype; } Public voidDotask () {businessservice=Lookupservice.getbusinessservice (servicetype); Businessservice.doprocessing (); }}
Step 5
Create the client.
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 rep pattern.
Public class Businessdelegatepatterndemo { publicstaticvoid main (string[] args) { New businessdelegate (); Businessdelegate.setservicetype ("EJB"); New Client (businessdelegate); Client.dotask (); Businessdelegate.setservicetype ("JMS"); Client.dotask (); }}
Step 7
Verify the output.
Processing task by invoking EJB serviceprocessing task by invoking JMS Service
"Design mode" Business representative mode