Trong bài này chúng ta vẫn với mọi người trong nhà mày mò về Business Delegate Pattern trong Java. Đây là 1 Một trong những Design Pattern thuộc đội Java J2EE. Bạn đang xem: Delegation in java with example
Chúng ta vẫn khám phá về khai niệm, tính năng của Business Delegate Pattern. Cũng nlỗi biện pháp thực thi nó ra sao trong Java thông sang 1 công tác đơn giản.
Business Delegate Pattern là gì?
Business Delegate Pattern được thực hiện để tách presentation layer (cấp trình bày) thoát khỏi business layer (cấp cho ghê doanh).
Về cơn bản, nó được sử dụng để sút tính năng tiếp xúc từ bỏ xa so với presentation layer code trong business layer code.
Chương thơm trình đơn giản dễ dàng cùng với Business Delegate Pattern.
Trước hết họ sẽ tạo nên một interface BusinessService.
Xem thêm: Giới Thiệu Kpt Là Gì - Nghĩa Của Từ Kpt Trong Tiếng Việt
BusinessService.java
public interface BusinessService public void process();
Sau kia khởi tạo nên nhì class rõ ràng để thực thi interface này.
EJBService.java
public class EJBService implements BusinessService
Override public void process() System.out.println("Xử lý bởi Dịch Vụ Thương Mại EJB.");
JMSService.java
public class JMSService implements BusinessService
Override public void process() System.out.println("Xử lý bởi Dịch vụ JSM.");
Tiếp đến chúng ta sẽ khởi tạo một class BusinessLookUp.
BusinessLookUp.java
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ờ họ sẽ có thể khởi sản xuất business delegate.
BusinessDelegate.java
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 họ sinh sản class Client (khách hàng).
Client.java
public class Client BusinessDelegate businessDelegate; public Client(BusinessDelegate businessDelegate) this.businessDelegate = businessDelegate; public void process() businessDelegate.process();
Sau Lúc theo thứ tự triển khai những yếu tắc bên trên. Chúng ta sẽ tạo class Main để thực hiện chạy chương trình và kiểm soát hiệu quả.
Main.java
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("Cmùi hương trình này được đăng trên balkanpoliticalclub.net");
Kết trái sau khoản thời gian chạy chương thơm trình:
Vậy nên là họ vẫn triển khai hoàn thành chương trình đơn giản với Business Delegate Pattern vào Java. Chúc chúng ta tiến hành thành công!!!
Bài sau Bài tiếp
-------------------#####-------------------
Các khóa học xây dựng Javađang bớt 40%:
Tải Thêm Comment
DANH SÁCH BÀI HỌC
Danh sách chủ đề
MÃ GIẢM GIÁ | ||
Unica | 50% | Lấy Mã |
TinoHost | 30% | Lấy Mã |
INET | 30% | Lấy Mã |
Liên hệ
Mã giảm giá
Khóa học
Giới thiệu
Admin Cường, quản lý bao gồm của trang web.
20đôi mươi - balkanpoliticalclub.net. All Right Reserved Theme GoodNews, gốc rễ Codeigniter, VPS mua trên Tinohost
BÀI VIẾT
Nếu chúng ta phát hiện tại lỗi không đúng links, câu chữ không đúng, hay 1 lỗi bất kì làm sao đó trên trang này thì nên cho khách hàng biết nhé. Cám ơn bạn!