public interface Star { public void confer(); public void sign(); public void bookTicket(); public void sing(); public void collectMoney(); }
public class RealStar implements Star{ @Override public void confer() { System.out.println("面谈"); } @Override public void sign() { System.out.println("签合同"); } @Override public void bookTicket() { System.out.println("订票"); } @Override public void sing() { System.out.println("(蔡徐坤)唱歌"); } @Override public void collectMoney() { System.out.println("收尾款"); } }
public class ProxyStar implements Star { private Star star; public ProxyStar(Star star) { super(); this.star = star; } @Override public void confer() { System.out.println("面谈"); } @Override public void sign() { System.out.println("签合同"); } @Override public void bookTicket() { System.out.println("订票"); } @Override public void sing() { //代理人自己不能唱歌,需要调歌手的唱歌的sing()方法; 需要对象 star.sing(); } @Override public void collectMoney() { System.out.println("收尾款"); } }
public class Client { public static void main(String[] args) { Star realstar= new RealStar(); Star proxystar = new ProxyStar(realstar); //理解它代理明星 //干嘛; 主要目的调用它的方法 proxystar.confer(); proxystar.sign(); proxystar.bookTicket(); proxystar.sing(); proxystar.collectMoney(); //目的: 通过代理才能拿到真实的内容(方法) } }