package com.test.proxy;
public interface KindWomen {
/*代理模式的好处是:现在有个接口 KindWomen,接口中有个方法playWihtMan(),接口有很多实现类
* 但是现在的问题是新需求要在执行playWihtMan()方法结尾要给客人sing();
* 我们不可能去该接口或者去一个一个的改实现类,代理模式就会很好的解决这个问题
* 我们只要在代理类中定义一个sing();然后再playWihtMan()中调用sing()方法即可
* */
public void makeEyesWithMan();
public void playWihtMan();
}
package com.test.proxy;
public class KingWomen implements KindWomen {
public void playWihtMan() {
System.out.println("The KingWomen play with man");
}
public void makeEyesWithMan() {
System.out.println("The KingWomen make eyes with man");
}
}
package com.test.proxy;
public class xishi implements KindWomen {
public void makeEyesWithMan() {
System.out.println("xishi make eyes with man,and the man feel very happy");
}
public void playWihtMan() {
System.out.println("xishi play wiht man,and the man feel very happy");
}
}
package com.test.proxy;
public class WangPo implements KindWomen {
private KindWomen kindWomen;
public WangPo(){
this.kindWomen = new KingWomen();
}
public WangPo(KindWomen kindWomen){
this.kindWomen = kindWomen;
}
public void makeEyesWithMan() {
this.kindWomen.makeEyesWithMan();
}
public void playWihtMan() {
this.kindWomen.playWihtMan();
this.sing();//此处新需求
}
public void sing(){
System.out.println("you must sing a song for the man,thx!");
}
}
package com.test.proxy;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WangPo w = new WangPo(new xishi());
w.makeEyesWithMan();
w.playWihtMan();
System.out.println("------------------------------------------------------------------");
WangPo wangPo = new WangPo();
wangPo.makeEyesWithMan();
wangPo.playWihtMan();
}
}
输出
xishi make eyes with man,and the man feel very happy
xishi play wiht man,and the man feel very happy
you must sing a song for the man,thx!
------------------------------------------------------------------
The KingWomen make eyes with man
The KingWomen play with man
you must sing a song for the man,thx!