1. 程式人生 > 其它 >[USACO17FEB]Why Did the Cow Cross the Road III P

[USACO17FEB]Why Did the Cow Cross the Road III P

//真實物件和代理物件都實現同一個介面
//代理物件要代理真實角色

//好處:代理物件可以做真實物件做不了的事情;真實物件專注做自己的事情
public class StaticProxy {
  public static void main(String[] args) {
    // WeddingCompany weddingCompany= new WeddingCompany(new You());
    // weddingCompany.HappyMarry();

    // You y =new You();
    new Thread(()-> System.out.println("我愛你")).start();


    new WeddingCompany(new You()).HappyMarry();
   }
}
  interface Marry{

    void HappyMarry();

  }
    //真實角色
    class You implements Marry{
    @Override
      public void HappyMarry() {
      System.out.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.out.println("結婚之前,佈置現場");
    }
  }