java 代理模式

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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值