java静态代理与动态代理笔记

</pre><pre name="code" class="html">
</pre><span style="font-family:宋体;font-size: 14px; line-height: 21px;">这是我的第一个笔记,记录学习java的静态代理与动态代理:</span><div style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', Tahoma, Helvetica, sans-serif, 'Microsoft YaHei', Georgia, Helvetica, Arial, sans-serif, 宋体, PMingLiU, serif; font-size: 14px; line-height: 21px;"><span style="font-family:宋体;"></span></div><div style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', Tahoma, Helvetica, sans-serif, 'Microsoft YaHei', Georgia, Helvetica, Arial, sans-serif, 宋体, PMingLiU, serif; font-size: 14px; line-height: 21px;"><span style="font-family:宋体;"><span data-wiz-span="data-wiz-span" style="font-size: 12pt;"><strong><span style="color:#ff0000;">代理的含义</span></strong></span>:代理起着一个中间人的作用,客户端不需要知道具体是使用哪一个具体的服务对象,但是他只需要知道为其代理的类对象,通过操作类对象,可以间接的获得服务。java中可以实现的有静态代理与动态代理。其中静态代理很简单,动态代理由java实现其机制,涉及到java的反射机制。</span></div><div style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', Tahoma, Helvetica, sans-serif, 'Microsoft YaHei', Georgia, Helvetica, Arial, sans-serif, 宋体, PMingLiU, serif; font-size: 14px; line-height: 21px;"><span style="font-family:宋体;"></span></div><div style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', Tahoma, Helvetica, sans-serif, 'Microsoft YaHei', Georgia, Helvetica, Arial, sans-serif, 宋体, PMingLiU, serif; font-size: 14px; line-height: 21px;"><span style="font-family:宋体;color:#ff0000;"><span data-wiz-span="data-wiz-span" style="font-size: 20pt;">1、静态代理:</span></span></div><div style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', Tahoma, Helvetica, sans-serif, 'Microsoft YaHei', Georgia, Helvetica, Arial, sans-serif, 宋体, PMingLiU, serif; font-size: 14px; line-height: 21px;"><span style="font-family:宋体;">    静态代理的<span style="color:#a52a00;">组成</span>:<span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">代理接口,被代理类,代理类;</span><span style="font-size: 10.5pt; line-height: 1.5;">    </span></span></div><div style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', Tahoma, Helvetica, sans-serif, 'Microsoft YaHei', Georgia, Helvetica, Arial, sans-serif, 宋体, PMingLiU, serif; font-size: 14px; line-height: 21px;"><span style="font-family:宋体;"><span style="font-size: 10.5pt; line-height: 1.5;">    代理类的<span style="color:#a52a00;">组成要素</span>:</span><span style="font-size: 10.5pt; line-height: 1.5;">    1、</span><span style="font-size: 10.5pt; line-height: 1.5;">代理类和被代理类都需要实现代理接口。</span></span></div><div style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', Tahoma, Helvetica, sans-serif, 'Microsoft YaHei', Georgia, Helvetica, Arial, sans-serif, 宋体, PMingLiU, serif; font-size: 14px; line-height: 21px;"><span style="font-family:宋体;"><span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">  </span><span style="font-size: 10.5pt; line-height: 1.5;">2、</span><span style="font-size: 10.5pt; line-height: 1.5;">代理类包含被代理类的对象。</span></span></div><div style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', Tahoma, Helvetica, sans-serif, 'Microsoft YaHei', Georgia, Helvetica, Arial, sans-serif, 宋体, PMingLiU, serif; font-size: 14px; line-height: 21px;"><span style="font-family:宋体;"><span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">  </span><span style="font-size: 10.5pt; line-height: 1.5;">3、</span><span style="font-size: 10.5pt; line-height: 1.5;">客户端获得代理类并把其转换成代理接口,并访问代理接口。</span></span></div><div style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', Tahoma, Helvetica, sans-serif, 'Microsoft YaHei', Georgia, Helvetica, Arial, sans-serif, 宋体, PMingLiU, serif; font-size: 14px; line-height: 21px;"><span style="font-family:宋体;"> <span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;">    </span><span style="font-size: 10.5pt; line-height: 1.5;"> </span><span style="font-size: 10.5pt; line-height: 1.5;">4、客户端访问代理类的代理接口方法时候,会访问代理类的实现,代理类在调用其内部被代理类的相同接口实现,从而实现间接访问。</span></span></div><div style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', Tahoma, Helvetica, sans-serif, 'Microsoft YaHei', Georgia, Helvetica, Arial, sans-serif, 宋体, PMingLiU, serif; font-size: 14px; line-height: 21px;"><span style="font-size: 10.5pt; line-height: 1.5;"><span style="font-family:宋体;">代理的一个<span style="color:#ff0000;"><strong><span data-wiz-span="data-wiz-span" style="font-size: 16pt;">小例子</span></strong></span>:</span></span></div><div style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', Tahoma, Helvetica, sans-serif, 'Microsoft YaHei', Georgia, Helvetica, Arial, sans-serif, 宋体, PMingLiU, serif; font-size: 14px; line-height: 21px;"><pre class="prettyprint linenums prettyprinted" style="font-size: 13px; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; background-color: rgb(247, 247, 249); padding: 10px; border: 1px solid rgb(225, 225, 232); tab-size: 4; box-shadow: rgb(251, 251, 252) 40px 0px 0px inset, rgb(236, 236, 240) 41px 0px 0px inset;"><div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;">/**</span></span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;"> * </span></span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;"> */</span></span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="kwd" style="color: rgb(30, 52, 123);">package</span><span class="pln" style="color: rgb(72, 72, 76);"> com</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">liuyao</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">rtti</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">reflect</span><span class="pun" style="color: rgb(147, 161, 161);">;</span></span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><span style="font-family:宋体;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"> </code></span></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><span style="font-family:宋体;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"> </code></span></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;color:#993366;">/**</span></span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;color:#993366;"> * dothings是代理类和被代理类都要实现的代理接口,包含两个方法void doSomething()与void doSomethingelse();</span></span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;color:#993366;"> */</span></span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="kwd" style="color: rgb(30, 52, 123);">interface</span><span class="pln" style="color: rgb(72, 72, 76);"> dothings</span><span class="pun" style="color: rgb(147, 161, 161);">{</span></span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="kwd" style="color: rgb(30, 52, 123);">void</span><span class="pln" style="color: rgb(72, 72, 76);"> doSomething</span><span class="pun" style="color: rgb(147, 161, 161);">();</span></span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="kwd" style="color: rgb(30, 52, 123);">void</span><span class="pln" style="color: rgb(72, 72, 76);"> doSomethingelse</span><span class="pun" style="color: rgb(147, 161, 161);">();</span></span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;">}</span></span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><span style="font-family:宋体;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"> </code></span></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161); background-color: rgb(255, 255, 255);"><span style="font-family:宋体;color:#993366;">/**</span></span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161); background-color: rgb(255, 255, 255);"><span style="font-family:宋体;color:#993366;"> *  被代理类,这里是提供真真实实的服务,但是由于某中原因,用户不能直接访问。</span></span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161); background-color: rgb(255, 255, 255);"><span style="font-family:宋体;color:#993366;"> *  但是也是实现了代理接口,并提供了两个接口方法的实现。</span></span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161); background-color: rgb(255, 255, 255);"><span style="font-family:宋体;color:#993366;"> */</span></span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="kwd" style="color: rgb(30, 52, 123);">class</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="typ" style="color: teal;">RealObject</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">implements</span><span class="pln" style="color: rgb(72, 72, 76);"> dothings</span><span class="pun" style="color: rgb(147, 161, 161);">{</span></span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><span style="font-family:宋体;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"> </code></span></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="lit" style="color: rgb(25, 95, 145);">@Override</span></span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="kwd" style="color: rgb(30, 52, 123);">public</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">void</span><span class="pln" style="color: rgb(72, 72, 76);"> doSomething</span><span class="pun" style="color: rgb(147, 161, 161);">()</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span></span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">		</span><span class="com" style="color: rgb(147, 161, 161);">// TODO Auto-generated method stub</span></span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">		</span><span class="typ" style="color: teal;">System</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">out</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">println</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="str" style="color: rgb(221, 17, 68);">"do Something"</span><span class="pun" style="color: rgb(147, 161, 161);">);</span></span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="pun" style="color: rgb(147, 161, 161);">}</span></span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><span style="font-family:宋体;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"> </code></span></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="lit" style="color: rgb(25, 95, 145);">@Override</span></span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="kwd" style="color: rgb(30, 52, 123);">public</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">void</span><span class="pln" style="color: rgb(72, 72, 76);"> doSomethingelse</span><span class="pun" style="color: rgb(147, 161, 161);">()</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span></span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">		</span><span class="com" style="color: rgb(147, 161, 161);">// TODO Auto-generated method stub</span></span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">		</span><span class="typ" style="color: teal;">System</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">out</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">println</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="str" style="color: rgb(221, 17, 68);">"do Something else"</span><span class="pun" style="color: rgb(147, 161, 161);">);</span></span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="pun" style="color: rgb(147, 161, 161);">}</span></span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;">}</span></span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><span style="font-family:宋体;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"> </code></span></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;color:#993366;">/**</span></span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;color:#993366;"> *	真正重要的是代理类,也实现了代理接口,其内部包含着</span></span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;color:#993366;"> *	private RealObject realObject;这个是一个代理类的引用,</span></span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;color:#993366;"> *	注意在其的接口方法实现上面(调用了被代理类的实现):</span></span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;color:#993366;"> *	public void doSomething() {</span></span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;color:#993366;"> *		// TODO Auto-generated method stub</span></span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;color:#993366;"> *		System.out.println("frome proxy");</span></span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;color:#993366;"> *		realObject.doSomething();</span></span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;color:#993366;"> *	}</span></span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;color:#993366;"> */</span></span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="kwd" style="color: rgb(30, 52, 123);">class</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="typ" style="color: teal;">SimpleProxy</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">implements</span><span class="pln" style="color: rgb(72, 72, 76);"> dothings</span><span class="pun" style="color: rgb(147, 161, 161);">{</span></span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="kwd" style="color: rgb(30, 52, 123);">private</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="typ" style="color: teal;">RealObject</span><span class="pln" style="color: rgb(72, 72, 76);"> realObject</span><span class="pun" style="color: rgb(147, 161, 161);">;</span></span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pln" style="color: rgb(72, 72, 76);"><span style="font-family:宋体;">	</span></span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="kwd" style="color: rgb(30, 52, 123);">public</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="typ" style="color: teal;">SimpleProxy</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="typ" style="color: teal;">RealObject</span><span class="pln" style="color: rgb(72, 72, 76);"> realObject</span><span class="pun" style="color: rgb(147, 161, 161);">)</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span></span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">		</span><span class="kwd" style="color: rgb(30, 52, 123);">super</span><span class="pun" style="color: rgb(147, 161, 161);">();</span></span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">		</span><span class="kwd" style="color: rgb(30, 52, 123);">this</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">realObject </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> realObject</span><span class="pun" style="color: rgb(147, 161, 161);">;</span></span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="pun" style="color: rgb(147, 161, 161);">}</span></span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><span style="font-family:宋体;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"> </code></span></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="lit" style="color: rgb(25, 95, 145);">@Override</span></span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="kwd" style="color: rgb(30, 52, 123);">public</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">void</span><span class="pln" style="color: rgb(72, 72, 76);"> doSomething</span><span class="pun" style="color: rgb(147, 161, 161);">()</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span></span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">		</span><span class="com" style="color: rgb(147, 161, 161);">// TODO Auto-generated method stub</span></span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">		</span><span class="typ" style="color: teal;">System</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">out</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">println</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="str" style="color: rgb(221, 17, 68);">"frome proxy"</span><span class="pun" style="color: rgb(147, 161, 161);">);</span></span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">		realObject</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">doSomething</span><span class="pun" style="color: rgb(147, 161, 161);">();</span></span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="pun" style="color: rgb(147, 161, 161);">}</span></span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><span style="font-family:宋体;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"> </code></span></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="lit" style="color: rgb(25, 95, 145);">@Override</span></span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="kwd" style="color: rgb(30, 52, 123);">public</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">void</span><span class="pln" style="color: rgb(72, 72, 76);"> doSomethingelse</span><span class="pun" style="color: rgb(147, 161, 161);">()</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span></span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">		</span><span class="com" style="color: rgb(147, 161, 161);">// TODO Auto-generated method stub</span></span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">		</span><span class="typ" style="color: teal;">System</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">out</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">println</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="str" style="color: rgb(221, 17, 68);">"frome proxy"</span><span class="pun" style="color: rgb(147, 161, 161);">);</span></span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">		realObject</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">doSomethingelse</span><span class="pun" style="color: rgb(147, 161, 161);">();</span></span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="pun" style="color: rgb(147, 161, 161);">}</span></span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pln" style="color: rgb(72, 72, 76);"><span style="font-family:宋体;">	</span></span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;">}</span></span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><span style="font-family:宋体;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"> </code></span></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;color:#993366;">/**</span></span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;color:#993366;"> * 客户端使用着,只需要知道代理类,便可以对真实的被代理类进行各种操作</span></span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;color:#993366;"> *</span></span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;color:#993366;"> */</span></span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="kwd" style="color: rgb(30, 52, 123);">public</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">class</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="typ" style="color: teal;">StaticProxyDemo</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span></span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><span style="font-family:宋体;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"> </code></span></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="com" style="color: rgb(147, 161, 161);">/**</span></span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;">	 * @param args</span></span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="com" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;">	 */</span></span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="kwd" style="color: rgb(30, 52, 123);">public</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">static</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">void</span><span class="pln" style="color: rgb(72, 72, 76);"> main</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="typ" style="color: teal;">String</span><span class="pun" style="color: rgb(147, 161, 161);">[]</span><span class="pln" style="color: rgb(72, 72, 76);"> args</span><span class="pun" style="color: rgb(147, 161, 161);">)</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="pun" style="color: rgb(147, 161, 161);">{</span></span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">		</span><span class="com" style="color: rgb(147, 161, 161);">// TODO Auto-generated method stub</span></span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">		</span><span class="typ" style="color: teal;">RealObject</span><span class="pln" style="color: rgb(72, 72, 76);"> robject </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">new</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="typ" style="color: teal;">RealObject</span><span class="pun" style="color: rgb(147, 161, 161);">();</span></span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">		</span><span class="typ" style="color: teal;">SimpleProxy</span><span class="pln" style="color: rgb(72, 72, 76);"> proxy </span><span class="pun" style="color: rgb(147, 161, 161);">=</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="kwd" style="color: rgb(30, 52, 123);">new</span><span class="pln" style="color: rgb(72, 72, 76);"> </span><span class="typ" style="color: teal;">SimpleProxy</span><span class="pun" style="color: rgb(147, 161, 161);">(</span><span class="pln" style="color: rgb(72, 72, 76);">robject</span><span class="pun" style="color: rgb(147, 161, 161);">);</span></span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">		proxy</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">doSomething</span><span class="pun" style="color: rgb(147, 161, 161);">();</span></span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">		proxy</span><span class="pun" style="color: rgb(147, 161, 161);">.</span><span class="pln" style="color: rgb(72, 72, 76);">doSomethingelse</span><span class="pun" style="color: rgb(147, 161, 161);">();</span></span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span style="font-family:宋体;"><span class="pln" style="color: rgb(72, 72, 76);">	</span><span class="pun" style="color: rgb(147, 161, 161);">}</span></span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; padding-left: 0px; list-style-type: none;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word;"><span class="pun" style="color: rgb(147, 161, 161);"><span style="font-family:宋体;">}</span></span></code></div></div>
静态代理的缺点:1、对于每一个接口,都要实现一个代理类,这样会做很多冗余的工作




二、动态代理:
        ​动态代理的组成:代理接口,被代理类,invocationHandler(调用处理器),Proxy类;
        动态代理的步骤:1、定义代理接口,接口可以包含有多个。
                                      2、实现被代理类,被代理类需要实现代理接口并覆盖代理接口的方法。
                                      3、实现invocationHandler接口。
                                      4、动态生成Proxy类,通过Proxy的动态生成实例方法调用。
        动态代理的详解: 
1、定义要代理的接口。
   注意:接口的定义必须为public,不然包外访问不了。接口中的操作,也就是客户想要的操作:
  
  
package com.liuyao.rtti.reflect;
 
public interface Display {
public void display();
}
2、实现被代理类:
   被代里类就是实现代理接口的类,实现代理接口,覆盖代理接口的方法。
  
  
/**
*
*/
package com.liuyao.rtti.reflect;
 
//这个类相当于display对象的实体类
 
/**
* @author Administrator
*
*/
public class ScreenObject implements Display {
 
/* (non-Javadoc)
* @see com.liuyao.rtti.reflect.Display#display()
*/
@Override
public void display() {
// TODO Auto-generated method stub
System.out.println("Dispaly on a real Screen");
}
}
3、实现invocationHandler接口。
  
  
/**
*
*/
package com.liuyao.rtti.reflect;
 
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
 
/**
* @author Administrator
*
*/
public class ScreenProxy implements InvocationHandler {
private Object realObject; //他和realobject toproxy
public ScreenProxy(Object realObject) {
super();
this.realObject = realObject;
}
 
/**
* @author ly
* 这个方法覆盖方法调用处理器的invoke方法
* 参数:1、Object proxy:动态代理类,用于识别访问的是哪个代理类,区别服务
* 2、Method method: 调用的方法,method的方法含有一个invoke方法。
* 3、Object[] args: 这个方法调用传递的参数数组。
* 工作原理:1、用户在操作代理类的时候,比如调用代理类的一个方法,则代理类会自动调用这个invoke方法
* 在调用invoke方法的同时告诉是哪个动态代理类在使用。
* 2、invocationHandler在这个方法被调用的同时,可以调用method的方法来调用被代理的类
* 注意在invocationHandler的实现类中嵌套了一个被代理的对象。
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("before invoke");
Object result = method.invoke(realObject, args); //调用真实的对象
System.out.println("after invoke");
// TODO Auto-generated method stub
return result;
}
}
4、实现动态代理类创建:
动态代理类通过Proxy.newProxyInstance()这个方法调用来实现,java中关于Proxy类的newProxyInstance的实现:
  
  
public static Object newProxyInstance(ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException
Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler.
这个方法接受的参数:args[0]ClassLoader loader(类转载器)->类装载器总共好像有三类,我们通过Class.getClassLoader()来说明。
public ClassLoader getClassLoader()->Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.
  使用bootstrap类装载器实现的时候会返回一个null值。所以可以传递null,可以传递代理接口的classloader也可以传递调用处理器的classLoader。
                    args[1]: Class<?>[] interfaces ->传递的是被代理接口的Class对象数组。
                    args[3]: InvocationHandler h->​ 调用处理器的引用。
  
  
/**
*
*/
package com.liuyao.rtti.reflect;
 
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
 
/**
* @author Administrator
*
*/
public class DynamicProxyDemo {
 
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ScreenObject real = new ScreenObject();
InvocationHandler handler = new ScreenProxy(real);
Object resultObject = Proxy.newProxyInstance(
real.getClass().getClassLoader(),
new Class[]{Display.class},handler);
Display display = (Display)resultObject;
display.display();
//System.out.print(resultObject);
}
}
动态代理的好处:通用的动态代理类,可以实现任何被代理类的代理工作。解耦合性更好!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值