静态代理模式
代理:顾名思义就是需要另外一方代替做事情呗。
模拟场景:小蜜代替老板做事情。我要找老板要点钱,老板很忙,我就去找他小蜜,找了他小蜜,他小蜜给了钱。小蜜可能还不单只有给钱的功能,具体大家想象...^_^
下面就是静态代理的代码喽:
//老板的功能 public interface Subject { public void operate();}
//老板登场 public class RealSubject implements Subject {public void operate() { System.out.println("this is real subject");}}
//小蜜出现了 public class ProxySubject implements Subject {private RealSubject realSubject;public void operate() {realSubject = new RealSubject();System.out.println("play other operate");realSubject.operate();}}
public class DoAction {public static void main(String[]args){Subject subject = new ProxySubject(); subject.operate();}}