<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>javascript高级语法19-代理模式</title>
</head>
<body>
<script>
/*代理是一个对象(proxy),用它来控制对 目标对象的访问
* 他要实现与目标对象相同的接口,但不同于装饰者模式。
* 它对目标对象不进行任何修改。
* 它的目的在于:延缓复杂对象的初始化时间,
* 这样可以再用刀这个目标对象的时候再初始化它。
* (对于单例来讲,更是重要。)
*/
function demo1(){
//图书类
var Book = function(id,name,price){
this.id = id;
this.name = name;
this.price = price;
}
//目标类
var myBookShop = (function(){
//书店里的书
var books = {}
return function(bks){
if(typeof bks == "object"){
books = bks;
}
//加书
this.addBook = function(book){
books[book.id] = book;
}
this.findBook = function(bid){
//这块后边用责任链模式改写。
if(books[bid]){
return books[bid];
}else{
return null;
}
}
this.returnBook = function(book){
this.addBook(book);
}
this.checkBook = function(bid){
var book = this.findBook(bid);
return book;
}
// Interface.ensureImplements(this,bookShop);
}
})();
// 普通代理:没什么意义,一般不用
var myBookShopProxy = function(bks){
var obj = new myBookShop(bks);
//加书
this.addBook = function(book){
obj.addBook(book);
}
this.findBook = function(bid){
return obj.findBook(bid);
}
this.returnBook = function(book){
obj.returnBook(book);
}
this.checkBook = function(bid){
return obj.checkBook(bid);
}
}
var proxy = new myBookShopProxy({
"001":new Book("001","extjs","45"),
"002":new Book("002","javascript","22")
})
alert(proxy.checkBook("002").name);
/*这个代理是严格按照定义来写的
* 一般开发中不会用到,没什么意义
*/
}
// demo1();
//惰性代理
function demo2(){
//图书类
var Book = function(id,name,price){
this.id = id;
this.name = name;
this.price = price;
}
//目标类
var myBookShop = (function(){
//书店里的书
var books = {}
return function(bks){
if(typeof bks == "object"){
books = bks;
}
//加书
this.addBook = function(book){
books[book.id] = book;
}
this.findBook = function(bid){
//这块后边用责任链模式改写。
if(books[bid]){
return books[bid];
}else{
return null;
}
}
this.returnBook = function(book){
this.addBook(book);
}
this.checkBook = function(bid){
var book = this.findBook(bid);
return book;
}
// Interface.ensureImplements(this,bookShop);
}
})();
//惰性代理
var myBookShopProxy = function(bks){
var obj = null;
this._init = function(){
alert("这时候初始化本类");
obj = new myBookShop(bks);
}
//加书
this.addBook = function(book){
this._init();
obj.addBook(book);
}
this.findBook = function(bid){
this._init();
return obj.findBook(bid);
}
this.returnBook = function(book){
this._init();
obj.returnBook(book);
}
this.checkBook = function(bid){
this._init();
return obj.checkBook(bid);
}
}
var proxy = new myBookShopProxy({
"001":new Book("001","extjs","45"),
"002":new Book("002","javascript","22")
})
alert(proxy.checkBook("001").name);
//调用方法的时候,才会初始化本类。
}
// demo2();
function demo3(){
/*模仿extjs store proxy之间的关系
*/
//定义命名空间
var Ext = Ext || {};
Ext.data = Ext.data || {};
//建立model
Ext.data.Model = function(fields){
this.fields = fields;
}
//model模型 proxy代理
Ext.data.Store = function(model,proxy){
//数据载体
var data = [];
this.model = model;
this.proxy = proxy;
//加载数据
this.load = function(){
var d = this.proxy.request();
//数据操作
for(var i=0;i<d.length;i++){
var o = {};
for(var k=0;k<model.fields.length;k++){
o[model.fields[k]["name"]] =
d[i][model.fields[k]["name"]];
}
data.push(o);
}
}
//根据索引得到model
this.getAt = function(index){
return data[index];
}
//得到所有数据的count
this.getCount = function(){
return data.length;
}
//清除所有数据
this.removeAll = function(){
data = [];
}
//遍历
this.each = function(fn,scope){
for(var i=0;i<data.length;i++){
if(scope){
fn.call(scope,data[i]);
}else{
fn.call(this,data[i]);
}
}
}
}
//定义ajax的本体
Ext.Ajax = Ext.Ajax || function(){};
Ext.Ajax.prototype.request = function(type,extraParams,method,url){
//1.得到跨浏览器的xhr对象,发送请求
//2.验证请求的状态等等复杂的操作。
//3.我们认为这个本体是一个大型的复杂的对象。
//4.应该在这里使用惰性代理。
return [{'id':001,name:"extjs"},{id:"002",name:"js"}];
}
//代理类
Ext.Ajax.proxy = function(){
var ajax = null;
//构造函数
this._init = function(){
ajax = new Ext.Ajax();
}
this.request = function(type,extraParams,method,url){
this._init();
return ajax.request(type,extraParams,method,url);
}
}
//测试
var person = new Ext.data.Model([{
name:"name"
},{
name:"id"
}]);
var personStore = new Ext.data.Store(person,new Ext.Ajax.proxy());
personStore.load();
alert(personStore.getCount());
alert(personStore.getAt(0).name);
personStore.each(function(model){
document.write(model.name + "<br>");
})
}
demo3();
</script>
</body>
</html>