Business Delegate Pattern trong Java - Cách triển khai và ví dụ
Trong bài này chúng ta sẽ cùng nhau tìm hiểu về Business Delegate Pattern trong Java. Đây là một trong những Design Pattern thuộc nhóm Java J2EE.

Chúng ta sẽ tìm hiểu về khai niệm, chức năng của Business Delegate Pattern. Cũng như cách triển khai nó như thế nào trong Java thông qua một chương trình đơn giản.
Business Delegate Pattern là gì?
Business Delegate Pattern được sử dụng để tách presentation layer (cấp trình bày) ra khỏi business layer (cấp kinh doanh).
Về cơn bản, nó được sử dụng để giảm chức năng giao tiếp từ xa đối với presentation layer code trong business layer code.
Bài viết này được đăng tại [free tuts .net]
Chương trình đơn giản với Business Delegate Pattern.
Đầu tiên chúng ta sẽ tạo một interface BusinessService.
public interface BusinessService {
public void process();
}Sau đó khởi tạo hai class cụ thể để triển khai interface này.
public class EJBService implements BusinessService {
@Override
public void process() {
System.out.println("Xử lý bằng Dịch vụ EJB.");
}
}
public class JMSService implements BusinessService {
@Override
public void process() {
System.out.println("Xử lý bằng Dịch vụ JSM.");
}
}Tiếp đến chúng ta sẽ tạo một class BusinessLookUp.
public class BusinessLookUp {
public BusinessService getBusinessService(String type) {
if (type.equalsIgnoreCase("ejb")) {
return new EJBService();
} else if (type.equalsIgnoreCase("JMS")) {
return new JMSService();
} else {
return null;
}
}
}Bây giờ chúng ta đã có thể khởi tạo business delegate.
public class BusinessDelegate {
private BusinessLookUp lookupService = new BusinessLookUp();
private BusinessService businessService;
private String type;
public void setServiceType(String type) {
this.type = type;
}
public void process() {
businessService = lookupService.getBusinessService(type);
businessService.process();
}
}Cuối cùng chúng ta tạo class Client (khách hàng).
public class Client {
BusinessDelegate businessDelegate;
public Client(BusinessDelegate businessDelegate) {
this.businessDelegate = businessDelegate;
}
public void process() {
businessDelegate.process();
}
}Sau khi lần lượt triển khai các thành phần trên. Chúng ta sẽ tạo class Main để thực hiện chạy chương trình và kiểm tra kết quả.
public class Main {
public static void main(String[] args) {
BusinessDelegate businessDelegate = new BusinessDelegate();
businessDelegate.setServiceType("EJB");
Client client = new Client(businessDelegate);
client.process();
businessDelegate.setServiceType("JMS");
client.process();
System.out.println("--------------------------------------");
System.out.println("Chương trình này được đăng tại Freetuts.net");
}
}
Kết quả sau khi chạy chương trình:
Như vậy là chúng ta đã thực hiện xong chương trình đơn giản với Business Delegate Pattern trong Java. Chúc các bạn thực hiện thành công!!!

Các kiểu dữ liệu trong C ( int - float - double - char ...)
Thuật toán tìm ước chung lớn nhất trong C/C++
Cấu trúc lệnh switch case trong C++ (có bài tập thực hành)
ComboBox - ListBox trong lập trình C# winforms
Random trong Python: Tạo số random ngẫu nhiên
Lệnh cin và cout trong C++
Cách khai báo biến trong PHP, các loại biến thường gặp
Download và cài đặt Vertrigo Server
Thẻ li trong HTML
Thẻ article trong HTML5
Cấu trúc HTML5: Cách tạo template HTML5 đầu tiên
Cách dùng thẻ img trong HTML và các thuộc tính của img
Thẻ a trong HTML và các thuộc tính của thẻ a thường dùng