//静态代理模式 //真实对象和代理对象都要实现同一个接口 //代理对象要代理真实角色 //好处:代理对象可以做很多真实对象做不了的事情 //真实对象专注做自己的事情 public class StaticProxy { public static void main(String[] args) { // new Thread(()-> System.out.println("呜呜呜")).start(); // new WeddingCompany(new You()).HappyMarry(); You you = new You(); WeddingCompany weddingCompany = new WeddingCompany(you); weddingCompany.HappyMarry(); } } interface Marry{ void HappyMarry(); } //真实 class You implements Marry{ @Override public void HappyMarry() { System.err.println("------------------------------"); } } //代理 class WeddingCompany implements Marry{ private Marry target; public WeddingCompany(Marry target) { this.target = target; } @Override public void HappyMarry() { before(); this.target.HappyMarry(); after(); } private void after() { System.out.println("之后"); } private void before(){ System.err.println("之前"); } }
多线程底层的思想也是静态代理模式