【转】Java设计模式之静态代理模式

原文:http://soft.chinabyte.com/database/118/12573118.shtml

代理接口:

  public interface ProxyInterface {

  public abstract void action();

  }

  真实角色:

  public class RealObject implements ProxyInterface{

  @Override

  public void action() {

  // TODO Auto-generated method stub

  System.out.println("Real Action method!");

  }

  }

  代理角色

  public class ProxyObject implements ProxyInterface{

  private RealObject realObject;//代理角色中的真实角色

  @Override

  public void action() {

  // TODO Auto-generated method stub

  BeforeAction();

  if(null == realObject){

  realObject = new RealObject();

  }

  realObject.action();

  AfterAction();

  }

  private void BeforeAction(){

  System.out.println("do something before Action of RealObject");

  }

  private void AfterAction(){

  System.out.println("do something after Action of RealObject");

  }

  }

  测试类:

  public class ProxyTest {

  public static void main(String[]args){

  ProxyObject proxyObject = new ProxyObject();//只能获取代理角色,

  proxyObject.action();

  }

  }

  测试结果;

  do something before Action of RealObject

  Real Action method!

  do something after Action of RealObject

posted @ 2013-03-29 09:55  Snuby  阅读(74)  评论(0)    收藏  举报