📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Table of contents |
---|
Problem |
Forces |
Solution |
Structure - Class Diagram, Sequence Diagram |
Participants and Responsibilities |
Implementation |
Consequences |
Applicability |
References |
Problem
Forces
- You want to access the business-tier components from your presentation-tier components and clients, such as devices, web services, and rich clients.
- You want to minimize coupling between clients and the business services, thus hiding the underlying implementation details of the service, such as lookup and access.
- You want to avoid unnecessary invocation of remote services.
- You want to translate network exceptions into application or user exceptions.
- You want to hide the details of service creation, reconfiguration, and invocation retries from the clients.
Solution
Structure
Class Diagram
Sequence Diagram
Participants and Responsibilities
Implementation
public interface BusinessService {
void doProcessing();
}
public class EJBService implements BusinessService {
@Override
public void doProcessing() {
System.out.println("EJBService is now processing");
}
}
//This is Service JMS implementation.
public class JMSService implements BusinessService {
@Override
public void doProcessing() {
System.out.println("JMSService is now processing");
}
}
//Service Email implementation.
public class EmailService implements BusinessService {
@Override
public void doProcessing() {
System.out.println("EmailService is now processing");
}
}
public class BusinessLookup {
private EjbService ejbService;
private JmsService jmsService;
private EmailService emailService;
/**
* @param serviceType
* Type of service instance to be returned.
* @return Service instance.
*/
public BusinessService getBusinessService(ServiceType serviceType) {
if (serviceType.equals(ServiceType.EJB)) {
return ejbService;
} else if (serviceType.equals(ServiceType.JMS)) {
return jmsService;
} else {
return emailService;
}
}
public void setJmsService(JmsService jmsService) {
this.jmsService = jmsService;
}
public void setEjbService(EjbService ejbService) {
this.ejbService = ejbService;
}
public void setEmailService(EmailService emailService) {
this.emailService = emailService;
}
}
public class BusinessDelegate {
private BusinessLookup lookupService;
private BusinessService businessService;
private ServiceType serviceType;
public void setLookupService(BusinessLookup businessLookup) {
this.lookupService = businessLookup;
}
public void setServiceType(ServiceType serviceType) {
this.serviceType = serviceType;
}
public void doTask() {
businessService = lookupService.getBusinessService(serviceType);
businessService.doProcessing();
}
}
Enumeration of service types
public enum ServiceType {
EJB, JMS,EMAIL;
}
public class Client {
private BusinessDelegate businessDelegate;
public Client(BusinessDelegate businessDelegate) {
this.businessDelegate = businessDelegate;
}
public void doTask() {
businessDelegate.doTask();
}
}
public class App {
/**
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {
BusinessDelegate businessDelegate = new BusinessDelegate();
BusinessLookup businessLookup = new BusinessLookup();
businessLookup.setEjbService(new EjbService());
businessLookup.setJmsService(new JmsService());
businessLookup.setEmailService(new EmailService());
businessDelegate.setLookupService(businessLookup);
businessDelegate.setServiceType(ServiceType.EJB);
Client client = new Client(businessDelegate);
client.doTask();
businessDelegate.setServiceType(ServiceType.JMS);
client.doTask();
businessDelegate.setServiceType(ServiceType.EMAIL);
client.doTask();
}
}
Applicability
- you want loose coupling between presentation and business tiers
- you want to orchestrate calls to multiple business services
- you want to encapsulate service lookups and service calls
References
- J2EE Design Patterns
- Core J2EE Patterns: Best Practices and Design Strategies
- http://www.corej2eepatterns.com
Related Patterns
- Data Transfer Object Design Pattern in Java
- Service Locator Design Pattern in Java
- Business Delegate Design Pattern in Java
- Converter Design Pattern in Java
- Transfer Object Assembler Pattern in Java
- Value List Handler Pattern in Java
Comments
Post a Comment
Leave Comment