觀察者模式主要應用于對象之間一對多的依賴關系,當一個對象發生改變時,多個對該對象有依賴的其他對象也會跟著做出相應改變,這就非常適合用觀察者模式來實現。使用觀察者模式可以根據需要增加或刪除對象,解決一對多對象間的耦合關系,使程序更易于擴展和維護。
基礎知識:
觀察者模式定義了對象間的一種一對多依賴關系,每當一個對象發生改變時,其相關依賴對象皆得到通知并被進行相應的改變。觀察者模式又叫做發布-訂閱模式。生活中有很多類似的關系,比如買粉絲買粉絲訂閱,多個讀者訂閱一個買粉絲買粉絲,一旦買粉絲有更新,多個讀者都會收到更新,而這種情況在應用程序中也非常常見,js綁定各種事件本質上就是觀察者模式的實現。
觀察者模式是一個非常有用的設計模式,它主要有兩個角色組成:
(1)目標對象:作為一對多關系中的一,可以用來管理觀察者的增加和刪除。
(2)觀察者對象:觀察目標對象,一旦目標發生改變則做出相應的反應。
在javascript中并沒有原生的創建或者實現接口的方式,或者判定一個類型是否實現了某個接口,我們只能利用js的靈活性的特點,模擬接口。
在javascript中實現接口有三種方式:注釋描述、屬性驗證、鴨子模型。
note:因為我看的是英文書,翻譯水平有限,不知道有些詞匯如何翻譯,大家只能領會精神了。
1. 注釋描述 (Describing Interfaces with Comments)
例子:
復制代碼 代碼如下:
/*
interface Composite {
function add(child);
function remove(child);
function getChild(index);
}
interface FormItem {
function save();
}
*/
var CompositeForm = function(id, method, action) { // implements Composite, FormItem
...
};
//Implement the Composite interface.
CompositeForm.prototype.add = function(child) {
...
};
CompositeForm.prototype.remove = function(child) {
...
};
CompositeForm.prototype.getChild = function(index) {
...
};
// Implement the FormItem interface.
CompositeForm.prototype.save = function() {
...
};
模擬其他面向對象語言,使用interface 和 implements關鍵字,但是需要將他們注釋起來,這樣就不會有語法錯誤。
這樣做的目的,只是為了告訴其他編程人員,這些類需要實現什么方法,需要在編程的時候加以注意。但是沒有提供一種驗證方式,這些類是否正確實現了這些接口中的方法,這種方式就是一種文檔化的作法。
2. 屬性驗證(Emulating Interfaces with Attribute Checking)
例子:
復制代碼 代碼如下:
/* interface
Composite {
function add(child);
function remove(child);
function getChild(index);
}
interface FormItem {
function save();
}
*/
var CompositeForm = function(id, method, action) {
this.implementsInterfaces = ['Composite', 'FormItem'];
...
};
...
function addForm(formInstance) {
if(!implements(formInstance, 'Composite', 'FormItem')) {
throw new Error("Object does not implement a required interface.");
}
...
}
// The implements function, which checks to see if an object declares that it
// implements the required interfaces.
function implements(object) {
for(var i = 1; i < arguments.length; i++) {
// Looping through all arguments
// after the first one.
var interfaceName = arguments[i];
var interfaceFound = false;
for(var j = 0; j < object.implementsInterfaces.length; j++) {
if(object.implementsInterfaces[j] == interfaceName) {
interfaceFound = true;
break;
}
}
if(!interfaceFound) {
return false;
// An interface was not found.
}
}
return true;
// All interfaces were found.
}
這種方式比第一種方式有所改進,接口的定義仍然以注釋的方式實現,但是添加了驗證方法,判斷一個類型是否實現了某個接口。
3.鴨子類型(Emulating Interfaces with Duck Typing)
復制代碼 代碼如下:
// Interfaces.
var Composite = new Interface('Composite', ['add', 'remove', 'getChild']);
var FormItem = new Interface('FormItem', ['save']);
// CompositeForm class
var CompositeForm = function(id, method, action) {
...
};
...
function addForm(formInstance) {
ensureImplements(formInstance, Composite, FormItem);
// This function will throw an error if a required method is not implemented.
...
}
// Constructor.
var Interface = function(name, methods) {
if(arguments.length != 2) {
throw new Error("Interface 買粉絲nstructor called with "
+ arguments.length + "arguments, but expected exactly 2.");
}
this.name = name;
this.methods = [];
for(var i = 0, len = methods.length; i < len; i++) {
if(typeof methods[i] !== 'string') {
throw new Error("Interface 買粉絲nstructor expects method names to be "
+ "passed in as a string.");
}
this.methods.push(methods[i]);
}
};
// Static class method.
Interface.ensureImplements = function(object) {
if(arguments.length < 2) {
throw new Error("Function Interface.ensureImplements called with "
+arguments.length + "arguments, but expected at least 2.");
}
for(var i = 1, len = arguments.length; i < len; i++) {
var interface = arguments[i];
if(interface.買粉絲nstructor !== Interface) {
throw new Error("Function Interface.ensureImplements expects arguments&qu
2024-07-29 02:10
2024-07-29 01:53
2024-07-29 01:48
2024-07-29 00:50
2024-07-29 00:46
2024-07-29 00:22