示例:工具提示对象---享元模式应用
生活随笔
收集整理的這篇文章主要介紹了
示例:工具提示对象---享元模式应用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<!DOCTYPE html>
<html>
<head>
<title>工具提示(享元模式)</title>
<meta charset="utf-8">
<script src="Library.js"></script>
</head>
<body>
<a id="link-id1" href="">1111111</a><a id="link-id2" href="">222222</a>
<script>
/**
* 示例:工具提示對象
*
* 在HS對象需要創建HTML內容這種情況下,享元模式特別有用。那種會生成DOM元素的對象如果數目眾多的話,會占用過多內存,使網頁陷入泥沼。采用享元模式后,只需創建少許這種對象即可,所有需要這種對象的地方都可以共享它們。工具提示就是一個典型的例子。
*/// 未經優化的Tooltip類
// Tooltip class, un-optimized
var Tooltip = function (targetElement, text) {
this.target = targetElement;
this.text = text;
this.delayTimeout = null;
this.delay = 500;this.element = document.createElement('div');
this.element.style.display = 'none';
this.element.style.position = 'absolute';
this.element.className = 'tooltip';
document.body.appendChild(this.element);
this.element.innerHTML = this.text;var that = this;
addEvent(this.target, 'mouseover', function (e) {
that.startDelay(e);
});
addEvent(this.target, 'mouseout', function (e) {
that.hide();
});
};
Tooltip.prototype = {
startDelay: function (e) {
if (this.delayTimeout === null) {
var that = this,
x = e.clientX,
y = e.clientY;
this.delayTimeout = setTimeout(function () {
that.show(x, y);
}, this.delay);
}
},
show: function (x, y) {
clearTimeout(this.delayTimeout);
this.delayTimeout = null;
this.element.style.left = x + 'px';
this.element.style.top = (y + 20) + 'px';
this.element.style.display = 'block';
},
hide: function () {
clearTimeout(this.delayTimeout);
this.delayTimeout = null;
this.element.style.display = 'none';
}
};var link1 = $('link-id1'),
link2 = $('link-id2');
var tt = new Tooltip(link1, 'Lorem ipsum....');// 作為享元的Tooltip
/*
把Tooltip類轉化為享元需要做三件事:把外在數據從Tooltip對象中刪除;創建一個用來實例化Tooltip的工廠;創建一個用來保存外在數據的管理器。在這個例子,我們可以用一個單體同時扮演工廠和管理器的角色。此外,由于外在數據可以作為事件偵聽器一部分保存,因此沒有必要使用一個中心數據庫。
*/// TooltipManager singleton, a flyweight factory and manager
var TooltipManager = (function () {
var storedInstance = null;// Tooltip class, as aflyweight
var Tooltip = function () {
this.delayTimeout = null;
this.delay = 500;this.element = document.createElement('div');
this.element.style.display = 'none';
this.element.style.position = 'absolute';
this.element.className = 'tooltip';
document.body.appendChild(this.element);
};
Tooltip.prototype = {
startDelay: function (e, text) {
if (this.delayTimeout === null) {
var that = this,
x = e.clientX,
y = e.clientY;
this.delayTimeout = setTimeout(function () {
that.show(x, y, text);
}, this.delay);
}
},
show: function (x, y, text) {
clearTimeout(this.delayTimeout);
this.delayTimeout = null;
this.element.innerHTML = text;
this.element.style.left = x + 'px';
this.element.style.top = (y + 20) + 'px';
this.element.style.display = 'block';
},
hide: function () {
clearTimeout(this.delayTimeout);
this.delayTimeout = null;
this.element.style.display = 'none';
}
};return {
addTooltip: function (targetElement, text) {
// Get the tooltip object
var tt = this.getTooltip();
// Attach the events
addEvent(targetElement, 'mouseover', function (e) {
tt.startDelay(e, text);
});
addEvent(targetElement, 'mouseout', function (e) {
tt.hide();
});
},
getTooltip: function () {
if (storedInstance === null) {
storedInstance = new Tooltip();
}
return storedInstance;
}
};
})();// Tooltip usage
TooltipManager.addTooltip($('link-id2'),'fuck your ass');
/*
上面的Tooltip類刪除了原來的構造函數的所有參數以及注冊事件處理器的代碼。而startDelay和show方法則各增加了一個新的參數,這樣一來,要顯示的文字就可以作為外在數據傳給他們。這個單體有兩個方法,分別體現了他的兩種角色,getTooltip是工廠方法,它與你之前見到過的其他享元的生成方法差不多。addTooltip則是管理器方法,它先獲取一個Tooltip對象,然后后分別把兩個匿名函數注冊為目標元素的mouseover和mouseout事件偵聽器。這個例子用不著創建中心數據庫,因為那兩個匿名函數中生成的閉包已經保存了外在數據。
*//*
現在生成的DOM元素已減至一個。這很重要,假如你想為工具提示添加陰影或iframe墊片等特性,那么每個Tooltip對象需要生成5-10個DOM元素。要是不把它實現為享元的話,網頁將被成百上千個工具提示壓垮。此外,享元模式的應用還減少了對箱內部保存的數據。
*/
</script>
</body>
</html>
?
轉載于:https://www.cnblogs.com/webFrontDev/archive/2013/03/24/2978523.html
總結
以上是生活随笔為你收集整理的示例:工具提示对象---享元模式应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ActionBarSherlock V
- 下一篇: [微软面试100题]61-70