真实对象与代理对象要实现同一个接口
代理对象代理的是真实对象
好处:
代理公司做了好多真实对象做不了的事情,真实对象只需要做自己要做的事情
public interface Marry {
void happyMarry ();
}
class You implements Marry {
@Override
public void happyMarry () {
System.out.println("XX结婚了");
}
}
class WeddingCompany<mian> implements Marry {
private You you;
private WeddingCompany (You you) {
this.you = you;
}
@Override
public void happyMarry () {
before();
you.happyMarry();
after();
}
private void after () {
System.out.println("婚庆公司,收尾款");
}
private void before () {
System.out.println("婚庆公司,婚前准备");
}
public static void main (String[] args) {
new WeddingCompany(new You()).happyMarry();
}
}