給大家舉個(gè)比較簡單的例子:
假如你買臺IBM的筆記本,IBM產(chǎn)家是不提供鼠標(biāo)的.但是我們?nèi)绻麖拇砩痰氖掷镔I就有鼠標(biāo)和送.
很簡單的例子,寫幾個(gè)類來實(shí)現(xiàn)一下吧.
1. 首先設(shè)計(jì)一個(gè)購買的接口代碼如下:(ComputerInterface.java)
1 public interface ComputerInterface {
2 public void buy();
3 }
4
2 public void buy();
3 }
4
2. 建一個(gè)電腦類實(shí)現(xiàn)購買的接口代碼如下:(Computer.java)
1 public class Computer implements ComputerInterface{
2 private String pcName="IBMT60";
3 private int pcPrice=17800;
4 public String getPcName() {
5 return pcName;
6 }
7 public void setPcName(String pcName) {
8 this.pcName = pcName;
9 }
10 public int getPcPrice() {
11 return pcPrice;
12 }
13 public void setPcPrice(int pcPrice) {
14 this.pcPrice = pcPrice;
15 }
16 public void buy() {
17 System.out.print("獲得筆記本電腦:"+pcName+"一臺");
18 }
19 }
2 private String pcName="IBMT60";
3 private int pcPrice=17800;
4 public String getPcName() {
5 return pcName;
6 }
7 public void setPcName(String pcName) {
8 this.pcName = pcName;
9 }
10 public int getPcPrice() {
11 return pcPrice;
12 }
13 public void setPcPrice(int pcPrice) {
14 this.pcPrice = pcPrice;
15 }
16 public void buy() {
17 System.out.print("獲得筆記本電腦:"+pcName+"一臺");
18 }
19 }
3. 再建設(shè)一個(gè)代理商的類:用來完成買電腦和贈送鼠標(biāo):(ComputerProxy.java)
1 public class ComputerProxy {
2 private ComputerInterface pci;
3 public ComputerInterface getPci() {
4 return pci;
5 }
6 public void setPci(ComputerInterface pci) {
7 this.pci = pci;
8 }
9 public void buy(){
10 pci.buy();
11 System.out.println("贈送鼠標(biāo)一個(gè)");
12 }
13 }
2 private ComputerInterface pci;
3 public ComputerInterface getPci() {
4 return pci;
5 }
6 public void setPci(ComputerInterface pci) {
7 this.pci = pci;
8 }
9 public void buy(){
10 pci.buy();
11 System.out.println("贈送鼠標(biāo)一個(gè)");
12 }
13 }
4. 建一個(gè)主函數(shù)測試一下:(TestDemo.java)
1 public class TestDemo {
2 public static void main(String[] args) {
3 ComputerProxy c1=new ComputerProxy();
4 c1.setPci(new Computer());
5 c1.buy();
6 }
7 }
2 public static void main(String[] args) {
3 ComputerProxy c1=new ComputerProxy();
4 c1.setPci(new Computer());
5 c1.buy();
6 }
7 }
運(yùn)行結(jié)果如下:
獲得筆記本電腦:IBMT60一臺獲得鼠標(biāo)一個(gè)
以上就是代理功能的實(shí)現(xiàn),由代理商銷售筆記本,并贈送鼠標(biāo).但是這樣的程序只適合是銷售IBM筆記本.那么如果要是其它品牌呢.所以我們來更改一個(gè)代理類.來實(shí)現(xiàn)動(dòng)態(tài)的代理.
5. 建立一個(gè)類代碼如下:(ComputerDynamicProxy.java)
1 public class ComputerDynamicProxy implements InvocationHandler {
2 /*
3 * 調(diào)用處理器是實(shí)現(xiàn)了InvocationHandler接口的類對象,在這個(gè)接口中只有一個(gè)方法:
4 * Object invoke(Object proxy,Method method,Object[] args);
5 * 無論何時(shí)調(diào)用代理對象的方法,調(diào)用處理器的invoke()方法都會被調(diào)用,并向其傳遞Method對象和原始的參數(shù)。
6 */
7 private Object delegate;
8
9 // 描述是誰的代理,也就是與那個(gè)類有關(guān)系
10 public Object bind(Object delegate) {
11 this.delegate = delegate;
12 return Proxy.newProxyInstance(delegate.getClass().getClassLoader(),
13 delegate.getClass().getInterfaces(), this);
14 }
15
16 public Object invoke(Object proxy, Method method, Object[] args)
17 throws Throwable {
18 System.out.println("贈送鼠標(biāo)一個(gè)!");
19 Object result = method.invoke(delegate, args);
20 return result;
21 }
22 }
2 /*
3 * 調(diào)用處理器是實(shí)現(xiàn)了InvocationHandler接口的類對象,在這個(gè)接口中只有一個(gè)方法:
4 * Object invoke(Object proxy,Method method,Object[] args);
5 * 無論何時(shí)調(diào)用代理對象的方法,調(diào)用處理器的invoke()方法都會被調(diào)用,并向其傳遞Method對象和原始的參數(shù)。
6 */
7 private Object delegate;
8
9 // 描述是誰的代理,也就是與那個(gè)類有關(guān)系
10 public Object bind(Object delegate) {
11 this.delegate = delegate;
12 return Proxy.newProxyInstance(delegate.getClass().getClassLoader(),
13 delegate.getClass().getInterfaces(), this);
14 }
15
16 public Object invoke(Object proxy, Method method, Object[] args)
17 throws Throwable {
18 System.out.println("贈送鼠標(biāo)一個(gè)!");
19 Object result = method.invoke(delegate, args);
20 return result;
21 }
22 }
主函數(shù)修改如下:
1 public class TestDemo {
2 public static void main(String[] args) {
3 ComputerDynamicProxy proxy = new ComputerDynamicProxy();
4 ComputerInterface computer = (ComputerInterface) proxy.bind(new Computer());
5 //返回一個(gè)代理類的實(shí)例,并非返回一個(gè)ComputerInterface對象,事實(shí)上他也不能被實(shí)例化不能有對象,返回的對象名可能為$computer
6 computer.buy();
7 }
8 }
9
2 public static void main(String[] args) {
3 ComputerDynamicProxy proxy = new ComputerDynamicProxy();
4 ComputerInterface computer = (ComputerInterface) proxy.bind(new Computer());
5 //返回一個(gè)代理類的實(shí)例,并非返回一個(gè)ComputerInterface對象,事實(shí)上他也不能被實(shí)例化不能有對象,返回的對象名可能為$computer
6 computer.buy();
7 }
8 }
9
就可以實(shí)現(xiàn)動(dòng)態(tài)代理了.