Original nuggets: L_Sivan

The proxy pattern

  • Definition: Provide asurrogate or placeholder foranother object to control access to it.
  • At the time of writing this article, I’m a little confused about the difference between the proxy mode and the decorator mode because I’ve read all the books. But now that you look at the definition, it’s pretty clear, providing a proxy to control access to this object, see the bold controls, that’s the essence of the proxy pattern. (Because without this control, it’s not much different from decorator mode.)
  • Belongs to the behavior class pattern
  • Classification: Static proxy, dynamic proxy

For example

The first thing to be clear is that design patterns must be proposed to solve some common coding problems. The key word is control, so take an example of accessing an object and being controlled by something else. For example, I like Eddie Peng very much, I really want to ask Eddie Peng to sign an autograph for me, but I can’t go to Eddie Peng directly, I need to ask his agent to help me, just imitate this. No proxy mode is used

// public class Eddie {privateEddiePrivate final static Eddie = new Eddie(); private final static Eddie = new Eddie(); private AgentOfEddie eddieAgent; public void sign(AgentOfEddie eddieAgent) {if (eddieAgent == this.eddieAgent) {
			System.out.println(this.getClass().getSimpleName() + ": VivoX9 soft light double shot, illuminate your beauty");
		} else {
			System.out.println("It's not my agent. I'm not signing it."); }} // Provide access to brokers, after all brokers are also identified as public AgentOfEddiegetAgentOfEddie() {
		eddieAgent = new AgentOfEddie(this);
		return eddieAgent;
	}
	public static Eddie getEddie() {
		returneddie; }} public class AgentOfEddie {public class AgentOfEddie(Eddie Eddie){this. Eddie = Eddie; } private Eddie eddie; // Let Gerengo sign public voidgetEddieSign(){ eddie.sign(this); Public class Cilent {public static void main(String[] args) {Eddie Eddie = eid.geteddie (); AgentOfEddie agentOfEddie = eddie.getAgentOfEddie(); agentOfEddie.getEddieSign(); }} Eddie: VivoX9 Soft Light double shot, illuminate your beauty // Scene Class 2, Public class Cilent {public static void main(String[] args) {Eddie Eddie = Eddie. GetEddie (); AgentOfEddie agentOfEddie = new AgentOfEddie(eddie); agentOfEddie.getEddieSign(); }} Results: This is not my agent, I don't sign // Scene class 3, Public class Cilent {public static void main(String[] args) {Eddie Eddie = Eddie. GetEddie (); eddie.sign(new AgentOfEddie(eddie)); }} Result: This is not my agent, I will not signCopy the code

The meaning of the code is that only the broker obtained through Eddie can sign for Peng. In addition, no matter his new broker goes to sign or directly uses Eddie to execute sign, peng cannot get the signature. It doesn’t seem to be a problem, and it works perfectly, giving three seconds of control. Indeed, writing this example I was a little skeptical of the sense of what the proxy model was for. Right? It says in the book, for example, in real life, when you go to court to find a lawyer, you just need to do your own defense. Using the above code, that is, Peng yuyan can not deal with external things, to the broker on the line, that is not wrong ah, the above code can be used, the broker there to add the control of the line? It is also by definition to provide a proxy to control access to this object. (Right, the point here is, ** why should the proxy pattern be implemented with interfaces? Is this interface necessary? ** Those who have seen the proxy model will understand. I am useless interface above, but also achieved, so I didn’t want to see) let me think, is the Spring aop programming, aop is aspect oriented programming, and this aspect, popular point is the way one by one, then the Spring is how to do can of plane control of each method (although dynamic proxy)? Hey, hey, you got a clue. Going back to the code above, suppose That Eddie Peng not only needs to sign his name, but also needs to shoot Vivo’s advertisement, Yida’s advertisement and Mekong Operation. All these need to be discussed with the outside and controlled by brokers. What problems will this cause? Every time Eddie Peng has an external action, the agent has to have an external action. This is true in life, but in the code above, the agent adds methods one by one to Eddie Peng’s external action!! Terrible.

So what do you do with proxy mode? Static proxy example

Public void sign(public void sign); public void sign(public void sign); Public void makeAdvertising(public void makeAdvertising); } public class Eddie implements Celebrity{privateEddiePrivate final static Eddie = new Eddie(); private final static Eddie = new Eddie(); private AgentOfEddie eddieAgent; AgentOfEddie public AgentOfEddie public AgentOfEddiegetAgentOfEddie() {
		eddieAgent = new AgentOfEddie(this);
		return eddieAgent;
	}
	public static Eddie getEddie() {
		returneddie; } @override public void sign(Celebrity Celebrity) {if (celebrity == this.eddieAgent) {
			System.out.println(this.getClass().getSimpleName() + ": VivoX9 soft light double shot, illuminate your beauty");
		} else {
			System.out.println("It's not my agent. I'm not signing it.");
		}
	}
	// 拍广告
	@Override
	public void makeAdvertising(Celebrity celebrity) {
		if (celebrity == this.eddieAgent) {
			System.out.println(this.getClass().getSimpleName() + "I'm shooting a commercial: VivoX9 soft light double shot to illuminate your beauty.");
		} else {
			System.out.println("It's not my agent. I'm not doing it."); Public class AgentOfEddie implements Celebrity{public AgentOfEddie(Eddie Eddie){this.eddie = eddie; } private Eddie eddie; @override public void sign(Celebrity) {// TODO auto-generated method stub Eddie. Sign (Celebrity); // TODO auto-generated method stub Eddie. } @Override public void makeAdvertising(Celebrity celebrity) { eddie.makeAdvertising(celebrity); Public class Cilent {public static void main(String[] args) {Eddie Eddie = eid.geteddie (); AgentOfEddie agentOfEddie = eddie.getAgentOfEddie(); agentOfEddie.sign(agentOfEddie); agentOfEddie.makeAdvertising(agentOfEddie); }} Eddie: VivoX9 soft light double shot, light up your beauty VivoX9 Soft Light double shot, illuminate your beauty // Scene Class 2, Public class Cilent {public static void main(String[] args) {Eddie Eddie = Eddie. GetEddie (); AgentOfEddie agentOfEddie = new AgentOfEddie(eddie); agentOfEddie.sign(agentOfEddie); agentOfEddie.makeAdvertising(agentOfEddie); }} Results: This is not my agent, I don't sign this is not my agent, I don't do // scene class 3, Public class Cilent {public static void main(String[] args) {Eddie Eddie = Eddie. GetEddie (); eddie.sign(new AgentOfEddie(eddie)); }} Result: This is not my agent, I will not sign this is not my agent, I will not takeCopy the code

The Eddie class implements this interface, as does the AgentOfEddie class, and declares Eddie in AgentOfEddie. The method implementation of the interface is implemented using Eddie’s method. One benefit of specifying interface methods is to ensure that the AgentOfEddie method is consistent with the Eddie principal method, and that the scenario class executes correctly. And the above proxy mode, a specific point is also called mandatory proxy (if there are other blogs, should know there are ordinary proxy and many specific proxy methods, as well as JDK dynamic proxy, Cglib dynamic proxy, here is not an example, because there is not that strength.

The proxy pattern is most commonly used, is to achieve control over another object, such as our J2EE interceptor, is a live application of the proxy pattern, before and after the execution of the method of the intercepted object transaction control. Of course, there are many applications, slowly explore.

The level is limited, it is hard to avoid mistakes, but also please criticize the comments section