您现在的位置是:Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款 >
02 js訂閱者模式和觀察者模式代碼(Javascript如何實現接口?)
Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款2024-06-02 03:14:58【】2人已围观
简介on(child){...};CompositeForm.prototype.remove=function(child){...};CompositeForm.prototype.getChild=
...
};
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"
+ "two and above to be instances of Interface.");
}
for(var j = 0, methodsLen = interface.methods.length; j < methodsLen; j++) {
var method = interface.methods[j];
if(!object[method] || typeof object[method] !== 'function') {
throw new Error("Function Interface.ensureImplements: object "
+ "does not implement the " + interface.name + " interface. Method " + method + " was not found.");
}
}
}
};
何時使用接口?
一直使用嚴格的類型驗證并不適合,因為大多數javascript程序員已經在沒有接口和接口驗證的情況下編程多年。當你用設計模式開始設計一個很復雜的系統的時候,使用接口更有益處。看起來使用接口好像限制了javascript的靈活性,但實際上他讓你的代碼變得更加的松耦合。他使你的代碼變得更加靈活,你可以傳送任何類型的變量,并且保證他有你想要的方法。有很多場景接口非常適合使用。
在一個大型系統里,很多程序員一起參與開發項目,接口就變得非常必要了。程序員經常要訪問一個還沒有實現的api,或者為其他程序員提供別人依賴的一個方法存根,在這種情況下,接口變得相當的有價值。他們可以文檔化api,并作為編程的契約。當存根被實現的api替換的時候你能立即知道,如果在開發過程中api有所變動,他能被另一個實現該接口的方法無縫替換。
如何使用接口?
首先要解決的問題是,在你的代碼中是否適合使用接口。如果是小項目,使用接口會增加代碼的復雜度。所以你要確定使用接口的情況下,是否是益處大于弊端。如果要使用接口,下面有幾條建議:
1.引用Interface 類到你的頁面文件。interface的源文件你可以再如下站點找到: 買粉絲://jsdesignpatterns.買粉絲/.
2.檢查你的代碼,確定哪些方法需要抽象到接口里面。 <
很赞哦!(6)
相关文章
- 01 女生個性簽名ins高級質感八字(女生網名ins高級質感)
- 01 youtube官網買粉絲下載圖片插件下載安裝(如何在手機上下載網頁中的視頻?)
- 01 女生頭像慵懶偏冷淡風ins全身(女生網名ins冷淡風)
- 01 youtube官網買粉絲下載圖片怎么不加(請問最好用的在線二維碼生成器有哪些)
- 01 youtube官網買粉絲下載器磁盤空間不(在手機通過無線升級Youtube時 提示下載完成 點擊安裝 但是一打開就顯示解析包時錯誤 應該怎么辦)
- 01 youtube官網買粉絲下載圖片怎么回事女性(YouTube是一個什么樣的網站)
- 01 youtube官網買粉絲下載圖片怎么才能減掉(求這張圖片上的翻譯)
- 01 女生頭像ins高級質感部位(女生頭像ins高級質感 真人高冷御姐又可愛仙女)
- 01 youtube官網買粉絲下載圖片在哪個文件里(you tu be的官網是多少?)
- 01 youtube官網買粉絲下載圖片在線免費編輯(請問最好用的在線二維碼生成器有哪些)
热门文章
站长推荐
01 女生頭像干凈ins風小眾(有哪些適合情侶佩戴的飾品品牌?)
01 女生頭像ins高級質感不露臉動漫(女生頭像ins高級質感 真人高冷御姐又可愛仙女)
01 女漫ins高級質感(女生昵稱簡短高級 女生網名ins高級質感十足)
01 女壁紙ins高級感紫色背景圖(臥室裝修用什么顏色好?)
01 女生頭像ins高級質感2023(女生頭像ins高級質感 真人高冷御姐又可愛仙女)
01 youtube官網買粉絲下載圖片失敗請(you tu be的官網是多少?)
01 女生頭像ins高級質感圖片側面(女生頭像ins高級質感 真人高冷御姐又可愛仙女)
01 youtube官網買粉絲下載圖片在線編輯免費(請問最好用的在線二維碼生成器有哪些)