Notes on Java Design Patterns for Beginners--Static Proxy of Proxy Pattern

Proxy mode

 

1. Purpose:
Provide a proxy for other objects to control access to this object.

(Provide a proxy object for an object, and the proxy object controls the reference of the specific object. )

 

2. Participants:

• Abstract subject (Subject): Defines the common interface between the real subject (Real Subject) and the proxy (Proxy) , so that the proxy (Proxy) can be used wherever the real subject (Real Subject) is used .

• Real Subject: Definesthe entity represented by the Proxy .

• Proxy:

   1) It contains a reference to the Real Subject object internally, so that the Real Subject object can be operated;

   2) At the same time, the proxy subject (Proxy) object provides the same interface abstract subject (Subject) as the real subject (Real Subject) object , so that it can replace the real subject (Real Subject) at any time ;

   3) Control the reference to the real subject (Real Subject) and be responsible for creating (or deleting) the real subject object when needed;

   4) At the same time, the Proxy object can attach other operations when performing operations on the Real Subject object, instead of simply passing the call to the Real Subject object.

 

3. Structure:



 

 

Personally, I think the agent pattern is a design pattern that is easier to understand.

Or understand the agency model through real life.

In real life, we often hear the word agent. By dealing with agents, buyers can get better and more services, and sellers can also focus on the production of products without having to spend more energy on other things. superior.

 

Let’s take buying a car as an example. In real life, when you buy a car from a seller ( Abstract Subject) , you usually don’t buy a car directly from the manufacturer ( Real Subject) . You can go to a 4S store ( Proxy role ) Buy a car and get more services there.

 

Let’s see how it is implemented in Java code:

Seller of cars (abstract theme):

/**
 * 抽象主题(Subject)
 * 
 * 汽车的销售商
 *
 */
public interface CarSeller {
	
	/*
	 * 销售汽车
	 */
	public Object sellCars(int type);

}

 

Audi manufacturer (real theme):

/**
 * 真实主题(Real Subject)角色
 * 奥迪厂家
 *
 */
public class AudiCarFactory implements CarSeller {

	/*
	 * 实现了抽象主题(Subject)角色的方法
	 */
	public Object sellCars(int type) {
		System.out.println("奥迪工厂出售汽车。");
		if(type == 1){
			return "AudiA6";
		}else{
			return "AudiA8";
		}
	}

}

 

Car dealer (agency): 

**
 * 代理(Proxy)角色
 * 
 * 汽车代理商
 * (这个代理商销售的是奥迪汽车)
 *
 */
public class CarProxy implements CarSeller {
	
	private AudiCarFactory carFactory = new AudiCarFactory();
	
	/*
	 * 同样也实现了抽象主题(Subject)角色的方法
	 */
	public Object sellCars(int type) {
		
//		售前服务
		serveBeforeSell();
		
//		代理奥迪厂家来销售汽车
		Object car = carFactory.sellCars(type);
		System.out.println("由代理商从厂家取得客户需要的汽车");

//		售后服务
		serveAfterSell();
		
		return car;
	}
	
	protected void serveBeforeSell(){
		System.out.println("汽车代理商为客户提供了一些售前服务");
	}
	
	protected void serveAfterSell(){
		System.out.println("汽车代理商为客户提供了一些售后服务");
	}

}

 

Customer (client call):

/**
 * 客户端调用
 */
public class Customer {
	
	public static void main(String[] args){
		
//		顾客找到代理商
		CarSeller carSeller = new CarProxy();
		
//		由代理商来销售汽车,可以发现代理商在销售给顾客汽车时,为顾客提供了更多的其他服务
		Object car = carSeller.sellCars(1);
		System.out.println("顾客从代理商那里买了一辆" + car);
		
	}
}

 

operation result:

汽车代理商为客户提供了一些售前服务
奥迪工厂出售汽车。
由代理商从厂家取得客户需要的汽车
汽车代理商为客户提供了一些售后服务
顾客从代理商那里买了一辆AudiA6

 

What are the advantages of the agency model?

Through the proxy mode, an intermediary is added between the client and the real subject (Real Subject) , which prevents the client from directly accessing the real subject (Real Subject) . This has the following benefits:

1) As shown in the above example, some additional operations can be added before or after accessing the real topic;

2) Ability to hide some access details;

3) Some controls can be added to access to real topics, such as security verification, etc.

 

According to the above example, it can be found that in the proxy pattern, the Real Subject role must exist in advance and be used as an internal attribute of the Proxy object. This is actually the Static Proxy mode .

In the static proxy mode, the relationship between the proxy (Proxy) and the real subject (Real Subject) has been defined in advance in the code and is fixed.

 

 

 Just imagine, if an agent increases or changes the types of products it represents (for example: acting as an agent for BMW cars),

1) So, do you need to modify the proxy class? Doing so will affect the stability of the existing system.

2) If you do not modify the existing proxy class, but add a new proxy class ( the code risk of adding a new class is much lower than modifying the code of an existing class ), ensure a Real Subject ) role corresponds to a proxy (Proxy) role, which will lead to a sharp expansion of the class.

 

This tight coupling is not conducive to system expansion and response to future demand changes.

So is there any way that the system can dynamically bind the relationship between the real subject (Real Subject) and the proxy (Proxy) as needed during runtime?

Dynamic Proxy gives a solution, seeNotes on Java Design Patterns for Beginners - Dynamic Proxy of Proxy Pattern 

 

Guess you like

Origin blog.csdn.net/louis_lee7812/article/details/83803450