JAVA Static Proxy Mode
Proxy mode: Provides a proxy for other objects to control access to this object.
To put it bluntly, the proxy pattern is the representative of "real objects", introducing a certain degree of indirection when accessing objects, because this indirection can be added to multiple uses.
Before implementing the code, let’s tell a simple life story. We all know that many companies around us have house purchase, sale or rental businesses, such as Lianjia (LianJia), but Lianjia itself does not have any actual housing assets. The houses it sells and leases need to provide the property owner (HomeMaster) to realize the company’s housing demand; at the same time, the company’s house selling and renting business requires the company’s employees (Seller) to realize this method, but to implement the company’s authorization and use the company’s business resource channels before the task can be completed. At this point, we should be clear that the Seller here is actually a static proxy in the proxy mode. So we start writing the code of this mode (the business logic before and after the proxy implementation method is omitted here):
interface LianJia{//LianJia provides channels for house purchase and sale (company business) public void sellHouse();}class HomeMaster implements LianJia{//Homeowners need to sell houses through Lianjia (to realize the company's business channel) public void sellHouse(){ System.out.println("I have a house to sell"); }}class Seller implements LianJia{//Lianjia's business requires Seller to implement private LianJia lj;//Declare Lianjia Company (it can be understood as proof that the seller belongs to Lianjia), which is convenient for calling the method public Seller(LianJia lj){ this.lj = lj; } public void sellHouse(){ lj.sellHouse();//The actual implementation method requires calling the company's business channel method}}public class ProxyMode{ public static void main(String[] args){ HomeMaster hm = new HomeMaster(); Seller s = new Seller(hm);//Agent to implement the demand for selling houses s.sellHouse(); }}Thank you for reading, I hope it can help you. Thank you for your support for this site!