设计模式---代理模式
2016-06-10 16:17
288 查看
最简单的例子就是我们找中介买房子:中介卖房子实际是帮忙房东卖房子,因此我们可以写出如下例子
package com.zcj.proxypattern;
public interface Person {
public void sale();
}
package com.zcj.proxypattern;
public class ProxyTest {
public static void main(String[] args) {
Agency agency = new Agency(new Landlord());
agency.sale();
}
}结果:
房东卖房子!中介只是一个代理,房子的最终信息都会与房东联系,好处就是房东不用亲自去卖房子,将客户和房东之间没那么耦合
package com.zcj.proxypattern;
public interface Person {
public void sale();
}
package com.zcj.proxypattern; public class Landlord implements Person { @Override public void sale(){ System.out.println("房东卖房子!"); } }
package com.zcj.proxypattern; public class Agency implements Person { private Person person; public Agency(Person person) { super(); this.person = person; } @Override public void sale(){ person.sale(); } }最后我们写测试类:看中介卖房子,实际上是调用了房东卖房信息
package com.zcj.proxypattern;
public class ProxyTest {
public static void main(String[] args) {
Agency agency = new Agency(new Landlord());
agency.sale();
}
}结果:
房东卖房子!中介只是一个代理,房子的最终信息都会与房东联系,好处就是房东不用亲自去卖房子,将客户和房东之间没那么耦合
相关文章推荐
- iOS开发中,在一般的ViewController中使用static cell
- Javascript Math.ceil()与Math.round()与Math.floor()区别
- Symfony学习--目录和入口
- Jquery操作下拉框(DropDownList)实现取值赋值
- 【Leetcode】Longest Valid Parentheses
- 集成JSF
- 【Java学习-J.160601.0.20】Servlet 基础二
- 51nod 1067 Bash游戏 V2 博弈论
- 安卓开发(二)ListView显示菜单,SQLite存储和ListView显示
- First Step
- 数据库系统概念第十二章 事务
- 使用Ansible部署SVN服务
- webService学习5:Eclipse的TCP/IP工具
- handler.post和handler.sendMessage的区别和联系
- 01、分页功能
- spring引入properties文件,并在controller等层获得引入文件中属性的值
- pt,px,rem和em之间区别总结
- Android动画使用
- hdu 3861 The King’s Problem【强连通Kosaraju+最小路径覆盖】
- CSS笔记二