Java 设计模式之代理模式
直接调用代理类,用代理类访问目标类。
package Pak; public interface Sourceable { public void method(); }
1 package Pak; 2 3 public class Source implements Sourceable { 4 5 @Override 6 public void method(){ 7 System.out.println("the original method!"); 8 9 } 10 }
1 package Pak; 2 3 public class Proxy implements Sourceable { 4 private Source source; 5 6 public Proxy() { 7 super(); 8 this.source = new Source(); 9 } 10 11 @Override 12 public void method() { 13 // TODO Auto-generated method stub 14 before(); 15 source.method(); 16 after(); 17 } 18 19 private void after() { 20 System.out.println("after proxy!"); 21 } 22 23 private void before() { 24 System.out.println("Before proxy!"); 25 } 26 }
1 package Pak; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class Main { 7 8 public static void main(String[] args) throws Exception { 9 Sourceable source = new Proxy(); 10 source.method(); 11 12 } 13 }
posted on 2014-07-12 11:10 Henry_Wang 阅读(188) 评论(0) 编辑 收藏 举报