1. Main problems solved:
The client code is decoupled from the implementation code, and the proxy class takes care of everything
The class that actually works must be hidden from the client.
The proxy class and implementation class generally implement the same interface (or you may not do this)
The proxy design mode is similar to the state mode. The proxy mode is a special case of the state mode.
In a proxy mode, the proxy class generally corresponds to only one implementation class. In a status mode, the proxy class generally corresponds to multiple implementation classes.
The main purpose of the proxy mode is to control access to the implementation class.
The main purpose of the Status mode is to dynamically change the implementation class.
2. Illustrations
Illustration and UML diagram (astah/Jude) http://pan.baidu.com/s/1pJylLG3
3. Code implementation example
Interface
1 package com.xinye.test.proxy; 2 /** 3 * 4 * @author xinye 5 * 6 */ 7 public interface IProxy { 8 void method1(); 9 void method2();10 void method3();11 }
Implementation class:
1 package COM. xinye. test. proxy; 2/** 3 * proxy class 4 * @ author Xinye 5*6 */7 public class proxyimpl implements iproxy {8 9 @ override10 public void Method1 () {11 system. out. println (getclass (). getsimplename () + "Method1 () invoke"); 12} 13 14 @ override15 public void method2 () {16 system. out. println (getclass (). getsimplename () + "method2 () invoke"); 17} 18 19 @ override20 public void method3 () {21 system. out. println (getclass (). getsimplename () + "method3 () invoke"); 22} 23 24}
Proxy type:
1 package COM. xinye. test. proxy; 2/** 3 * proxy class 4 * @ author Xinye 5*6 */7 public class proxy implements iproxy {8 // The proxy object 9 private iproxy impl; 10 11 Public proxy () {12 impl = new proxyimpl (); 13} 14 15 @ override16 public void Method1 () {17 impl. method1 (); 18} 19 20 @ override21 public void method2 () {22 impl. method2 (); 23} 24 25 @ override26 public void method3 () {27 impl. method3 (); 28} 29 30}
Client code:
1 package com.xinye.test.proxy; 2 /** 3 * 4 * @author xinye 5 * 6 */ 7 public class Client { 8 public static void main(String[] args) { 9 Proxy proxy = new Proxy();10 11 proxy.method1();12 proxy.method2();13 proxy.method3();14 }15 }