JavaScript学习之设计模式->代理模式

代理模式概念:

           为其他对象提供访问一个代理对象,以控制对对象本身的访问, 在某些情况下,一个客户不想或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用。

代理模式的角色:

                  代理对象,和本体对象,代理对象和本体对象的抽象接口。代理对象和本体对象共同实现一个接口,代理对象包含本体对象的引用,当要调用本体对象的时候,调用本体对象。代理对象代替本体对象去做很多事情。做这些事情的时候,是调用本体对象本身。

                  比如:在现代战争或者危险的场景中,可以用机器人代替人实现爆破。。。。。等等。。。

以下以图书馆和图书馆代理为例子,编写的JavaScript,假设声明一个图书馆对象要耗费很多资源,那么在页面加载时,就可以先不声明对象,使浏览器更快速地加载完成,这样给用户一个良好的体验。直到使用该对象时,才声明对象

<!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>代理模式</title>
		<script type=text/javascript charset=utf-8>	
			
			
				//代理模式(proxy):代理也是对象,他的目的就是为了节制(控制)对本体对象的访问
				
				//代理对象和对象本身的共同借口
				// var LibraryInterface = function(){
				// 	this.addbook = function(){

				// 	}
				// 	this.findbook = function(){
				// 	}
				// 	this.checkoutbook = function(){

				// 	}
				// 	this.returnbook = function(){

				// 	}
				// }
				
				var Book = function(id , title , author){
					this.id = id;
					this.title = title ; 
					this.author = author;
				};
				
				//图书馆(本体对象 , 实例化图书馆需要消耗很多的资源)
				var Library = function(books){
					this.books = books;
				};
				Library.prototype = {
					constructor:Library , 
					addbook:function(book){
						this.books[book.id] = book;
					},
					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(返回代理对象)
				window.onload = function(){
					document.getElementById("btn").onclick = function(){
						var proxy = new LibraryProxy({
						"01":new Book('01','java','z3'),
						"02":new Book('02','js','z4')
					});
					alert(proxy.findbook('01').title);
					}
				}	
				
		</script>
				
	</head>
	<body>
		<input type="button" id="btn" value="借书"/>
	</body>
</html>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值