JavaScript设计模式--实现接口Interface
2015-03-05 17:15
375 查看
文章原封不动转自:
http://www.cnblogs.com/jsjrjcj/archive/2011/05/25/2056627.html
如何用面向对象的思想来写JavaScript,对于初学者应该是比较难的,我们经常用的JQuery其实也是用面向对象的思想去封装的,今天我们来看看如何在Javascript中用Interface,在C#还是JAVA中都应该面向接口设计我们的程序,在C#和Java中都Interface这样的关键字,但是JavaScript中没有相应的机制,但是Javascript很灵活,我们可以用它的特性去模仿Interface,但是我们需要加入一些methods来做check的动作。
我们来看下一个Interface的作用: 继承了这个Interface就必须要实现这个Interface中定义的方法(方法签名)//JavaScript 现在还做不到方法的签名的约束
我们来分析一下code,我们现在的做法是用来比较一个Instance中的方法名在接口中是否定义了。
我先定义一个接口(2个参数),第二个参数是接口中的方法名。Check方法用简单的2层for循环来做比较动作。
我们来看下如何去用这个接口:
如果我们注释了上面的GetAge方法,在执行的时候就会出错。在ensureImplement的时候发现并没有去实现这个方法。
说实话,这样的排版确实很uglily, 大家看的时候原谅,没这么写过,以后一定多写点东西。
By the way, I'm Alan_chen in MSDN forums for C# and DataPlatform(Entity Framework).
如需转发请注明原文出处:http://www.cnblogs.com/jsjrjcj/archive/2011/05/25/2056627.html
http://www.cnblogs.com/jsjrjcj/archive/2011/05/25/2056627.html
如何用面向对象的思想来写JavaScript,对于初学者应该是比较难的,我们经常用的JQuery其实也是用面向对象的思想去封装的,今天我们来看看如何在Javascript中用Interface,在C#还是JAVA中都应该面向接口设计我们的程序,在C#和Java中都Interface这样的关键字,但是JavaScript中没有相应的机制,但是Javascript很灵活,我们可以用它的特性去模仿Interface,但是我们需要加入一些methods来做check的动作。
我们来看下一个Interface的作用: 继承了这个Interface就必须要实现这个Interface中定义的方法(方法签名)//JavaScript 现在还做不到方法的签名的约束
var Interface = function (name, methods) { if (arguments.length != 2) { throw new Error("the interface length is bigger than 2"); } this.Name = name; this.Method = []; for (var i = 0; i < methods.length; i++) { if(typeof methods[i]!== 'string') { throw new Error("the method name is not string"); } this.Method.push(methods[i]); } } /*static method in interface*/ Interface.ensureImplement = function (object) { if (arguments.length < 2) { throw new Error("there is not Interface or the instance"); } for (var i = 1; i < arguments.length; i++) { var interface1 = arguments[i]; if (interface1.constructor !== Interface) { throw new Error("the argument is not interface"); } for (var j = 0; j < interface1.Method.length; j++) { var method = interface1.Method[j]; if (!object[method] || typeof object[method] !== 'function') { throw new Error("you instance doesn't implement the interface"); } } } }
我们来分析一下code,我们现在的做法是用来比较一个Instance中的方法名在接口中是否定义了。
我先定义一个接口(2个参数),第二个参数是接口中的方法名。Check方法用简单的2层for循环来做比较动作。
我们来看下如何去用这个接口:
var Person = new Interface("Person", ["GetName", "GetAge"]); var Man = function (name, age) { this.Name = name; this.Age = age; } Man.prototype = { GetName: function () { return this.Name; }, // GetAge: function () { return this.Age; } } var test = function (instance) { Interface.ensureImplement(instance, Person); var name = instance.GetName(); alert(name); } test(new Man("Alan",20));
如果我们注释了上面的GetAge方法,在执行的时候就会出错。在ensureImplement的时候发现并没有去实现这个方法。
说实话,这样的排版确实很uglily, 大家看的时候原谅,没这么写过,以后一定多写点东西。
By the way, I'm Alan_chen in MSDN forums for C# and DataPlatform(Entity Framework).
如需转发请注明原文出处:http://www.cnblogs.com/jsjrjcj/archive/2011/05/25/2056627.html
相关文章推荐
- C#中的显式接口成员实现(explicit interface member implementation)
- 面向对象多态的实现(抽象类Abstract和接口Interface的用法区别)
- Oracle 调用接口 (Orale Call Interface,OCI) 【实现各种语言操作Oracle数据库】简介
- Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)?
- 接口(Interface)和实现(implementation)
- [翻译] Effective C++, 3rd Edition, Item 34: 区分 inheritance of interface(接口继承)和 inheritance of implementation(实现继承)(上)
- 9AnonymousInnerClass(匿名内部类)是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)?
- JavaScript设计模式之一Interface接口
- Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)
- 匿名内部类implements(实现)interface(接口)
- Java 学习笔记 (3) -抽象类 abstract 接口 interface 内部类 inner class 继承 extends 实现 implements 包 package
- 显示接口成员实现(explicit interface member implementation)总结
- Static Nested Class 和 Inner Class的不同。Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)?
- 无法实现接口成员,因为它不是公共的——interface
- 无法实现接口成员,因为它不是公共的-----------interface
- 11.22 模板同虚函数(继承)在实现接口(interface)上的异同
- 安全接口 interface --显示实现接口
- Polynom-Interface通过接口实现多态
- USE Interface CallBack 通过接口实现回调
- 第二,Anonymous Inner Class(匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)?