<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type=text/javascript charset=utf-8 src=../commons/CommonUtil.js ></script>
<script type=text/javascript charset=utf-8>
//代理模式(proxy):代理也是对象,他的目的就是为了节制(控制)对本体对象的访问 var LibraryInterface = new BH.Interface('LibraryInterface' ,['addbook','findbook','checkoutbook','returnbook']); var Book = function(id , title , author){
this.id = id;
this.title = title ;
this.author = author;
}; //图书馆(本体对象 , 实例化图书馆需要消耗很多的资源)
var Library = function(books){
this.books = books;//{bookid : book}
};
Library.prototype = {
constructor:Library ,
addbook:function(book){
this.books[book.id] = book;//访问json用中括号
},
findbook:function(id){
if(this.books[id]){
return this.books[id];
}
return null;
},
checkoutbook:function(id){
//电脑登记..交押金
return this.findbook(id);
},
returnbook:function(book){
//电脑登记(...已还)
//计算费用(计算余额)
this.books[book.id] = book;
}
}; //图书馆的代理对象
var LibraryProxy = function(books){
alert('产生代理对象,但是并没有产生真正的本体对象!');
this.books = books;
this.library = null; //定义一个空对象
};
LibraryProxy.prototype = {
constructor:LibraryProxy ,
initializeLibrary:function(){
if(this.library == null){
alert('真正的本体对象!');
this.library = new Library(this.books);
}
},
addbook:function(book){
this.initializeLibrary();
//实际上具体做事情的还是本体对象自己本身
this.library.addbook(book);
},
findbook:function(id){
this.initializeLibrary();
return this.library.findbook(id);
},
checkoutbook:function(id){
this.initializeLibrary();
return this.findbook(id);
},
returnbook:function(book){
this.initializeLibrary();
this.library.returnbook(book);
}
}; //实例化的是代理对象:推迟本体对象实例化的时间,什么时候具体去做事情了,再去实例化它
// hibernate: get(全查询出来) load(返回代理对象)
var proxy = new LibraryProxy({
"01":new Book('01','java','z3'),
"02":new Book('02','js','z4')
}); alert(proxy.findbook('01').title); </script>
</head>
<body>
</body>
</html>

最新文章

  1. entity Framework codefirst Migrations
  2. 代码规范、GitHub提交源码的标准 答题人-杨宇杰
  3. Google Nexus5在linux下刷原生安卓 android6.0
  4. bzoj1103: [POI2007]大都市meg
  5. cd dirname $0
  6. C# - Try catch 中 使用 End()
  7. 004. 线程间操作无效: 从不是创建控件“textBox1”的线程访问它
  8. sqlserver 误删数据恢复
  9. python通过代理刷网页点击量
  10. PHP命名空间(Namespace)的使用详解(转)
  11. PHP中计算时间差(上周,上月,去年,昨天等)
  12. mfc添加气球式提示栏
  13. SQL Server Log文件对磁盘的写操作大小是多少
  14. (python)数据结构---元组
  15. VS2015(Xamarin)开发安卓WebApp笔记
  16. Chrome浏览器如何调试移动端网页信息
  17. Eclipse-快捷键大全(转载)
  18. 逻辑备份,mysqldump,SELECT…INTO OUTFILE,恢复
  19. PRML学习笔记第一章
  20. Node.js的开源博客系统Ghost搭建教程

热门文章

  1. 紫书 例题 10-7 UVa 10820 (欧拉函数)
  2. ArcGIS api for javascript——加入地图并显示x,y坐标
  3. HDU 5303 Delicious Apples(贪心 + 背包 2015多校啊)
  4. Azure 配置高可用的准备系列工作-建立不同区域的存储账户和建立网络!
  5. Install the IIS 6.0 Management Compatibility Components in Windows 7 or in Windows Vista from Control Panel
  6. Entity Framework之Code First开发方式
  7. spring security oauth2 架构---官方
  8. 清空/var/adm/wtmp 文件内容
  9. React开发实时聊天招聘工具 -第六章 登陆注册(2)
  10. Js 中的i++ 和 ++i 的区别