"Design mode" Business representative mode

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.