PublicProxy 静态代理

Posted fangtingfei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PublicProxy 静态代理相关的知识,希望对你有一定的参考价值。

静态代理有三要素:1,共同实现的接口  2,真实成员  3,代理成员

共同实现的接口:Star.java

1 package cn.ftf.proxy;
2 
3 public interface Star 
4     void confer();
5     void signContract();
6     void bookTicket();
7     void sing();
8     void collect();
9 

真实成员:RealStar.java

 1 package cn.ftf.proxy;
 2 
 3 public class RealSrar implements Star
 4 
 5     @Override
 6     public void confer() 
 7         // TODO Auto-generated method stub
 8         System.out.println(" confer()");
 9         
10     
11 
12     @Override
13     public void signContract() 
14         // TODO Auto-generated method stub
15         System.out.println("signContract()");
16         
17     
18 
19     @Override
20     public void bookTicket() 
21         // TODO Auto-generated method stub
22         System.out.println("bookTicket()");
23     
24 
25     @Override
26     public void sing() 
27         // TODO Auto-generated method stub
28         System.out.println("sing()周杰伦");
29     
30 
31     @Override
32     public void collect() 
33         // TODO Auto-generated method stub
34         System.out.println("collect()");
35         
36     
37 

代理成员ProxyStar.java

 1 package cn.ftf.proxy;
 2 
 3 public class ProxyStar implements Star
 4     
 5     private Star star;
 6     
 7     public ProxyStar(Star star) 
 8         super();
 9         this.star = star;
10     
11 
12     @Override
13     public void confer() 
14         // TODO Auto-generated method stub
15         System.out.println("maintan");
16         
17     
18 
19     @Override
20     public void signContract() 
21         // TODO Auto-generated method stub
22         System.out.println("qianxieyi");
23     
24 
25     @Override
26     public void bookTicket() 
27         // TODO Auto-generated method stub
28         System.out.println("maipiao");
29     
30 
31     @Override
32     public void sing() 
33         // TODO Auto-generated method stub
34         star.sing(); 
35     
36 
37     @Override
38     public void collect() 
39         // TODO Auto-generated method stub
40         System.out.println("shouweikuan");
41     
42 
43 

测试Test.java

 1 package cn.ftf.proxy;
 2 
 3 public class Test 
 4     public static void main(String[] args) 
 5         Star real=new RealSrar();
 6         Star proxy = new ProxyStar(real);
 7         
 8         proxy.confer();
 9         proxy.bookTicket();
10         proxy.sing();
11         proxy.collect();
12         
13     
14 

运行结果:
maintan
maipiao
sing()周杰伦
shouweikuan

 

以上是关于PublicProxy 静态代理的主要内容,如果未能解决你的问题,请参考以下文章

静态代理和动态代理

静态代理和动态代理原理及实现

静态代理$动态代理

代理模式(静态代理动态代理)代码实战(详细)

静态代理和动态代理区别

代理模式_静态代理