设计模式之代理模式

来源:互联网 发布:vb基础教程实验题目 编辑:程序博客网 时间:2024/06/18 01:50
自己写了一些关于代理模式的讲解,但确实没有如下这个例子讲的生动。但此内容有很多相同的博客,不知原创是哪一篇,我学习时看的是  http://yangguangfu.iteye.com/blog/815787  /** * @author BinCain * @version V1.0 * @Description: 定义了女人的function * @date 2017/10/26 0026 18:23 */public interface WomanInterface {    /**     * 抛媚眼     */    void ogleWithMan();    /**     * 额----咳咳----ooxx     */    void happyWithMan();}
public class PanJinLianClass implements WomanInterface {    @Override    public void ogleWithMan() {        System.out.println("潘金莲抛媚眼-----");    }    @Override    public void happyWithMan() {        System.out.println("潘金莲ooxx-----");    }}

/** * @author BinCain * @version V1.0 * @Description: 王婆老了,别人看不上她,所以她做了代理,接活让年轻的女子去做 *               因为王婆和潘金莲都实现了女人的功能,所以她知道潘金莲能完成任务 * @date 2017/10/26 0026 18:25 */public class WangPoClass implements WomanInterface {    WomanInterface womanInterface;    public WangPoClass(WomanInterface womanInterface) {        this.womanInterface = womanInterface;    }    @Override    public void ogleWithMan() {        this.womanInterface.ogleWithMan();    }    @Override    public void happyWithMan() {        this.womanInterface.happyWithMan();    }}

/** * @author BinCain * @version V1.0 * @Description: 西门庆来测试真伪 * @date 2017/10/26 0026 18:35 */public class XiMenQingTestClass {    @Test    public void testWithWoman(){        //联系王婆   ----- >  王婆把这个活给了潘金莲 (当然啦也可以找别人,只要西门庆看得上)        WangPoClass wangPoClass = new WangPoClass(new PanJinLianClass());        //接下来看似是王婆在接活,其实干活的是潘金莲        wangPoClass.ogleWithMan();        wangPoClass.happyWithMan();    }}
西门庆测试结果如下:潘金莲抛媚眼-----潘金莲xxx-----Process finished with exit code 0
原创粉丝点击