[分享]鸭式辩型法实现javascript中的"接口"_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 1907 | 回复: 0   主题: [分享]鸭式辩型法实现javascript中的"接口"        下一篇 
    本主题由 hui.chen 于 2016-2-1 10:18:34 移动
nengqiang.wang
注册用户
等级:中校
经验:1658
发帖:34
精华:0
注册:1970-1-1
状态:离线
发送短消息息给nengqiang.wang 加好友    发送短消息息给nengqiang.wang 发消息
发表于: IP:您无权察看 2016-2-1 10:16:33 | [全部帖] [楼主帖] 楼主

JAVASCRIPT设计模式书上讲的接口的定义,鸭式辩型法
下面是JAVASCRIPT设计模式书上讲的接口的定义。
鸭式辩型法
<!DOCTYPE HTML>
<html>
	<head>
		<script type="text/javascript">
			var Interface = function(name, methods){
				if(arguments.length != 2){
					throw new Error('Interface constructor 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 constructor expected method names to be '
							+ 'passed in as a string.');
					}
					this.methods.push(methods[i]);
				}
			};
			
			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.constructor != 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 implements the " + interface.name + " interface.Method " + 
							  	method + " was not found.");  
						}
					}
				}
			};
			
			// 定义接口
			var DynamicMap = new Interface('DynamicMap', ['centerOnPoint', 'zoom', 'draw']);
			function displayRoute(mapInstance){
				// 接口实现检查
				Interface.ensureImplements(mapInstance, DynamicMap);
				mapInstance.centerOnPoint(24, 13);
				mapInstance.zoom(5);
				mapInstance.draw();
			}
			
			// 对象
			function Foo(){
			}
			Foo.prototype = {
				centerOnPoint : function(x, y){
					document.writeln('<br />centerOnPoint: ' + x + ', ' + y);
				},
				zoom : function(level){
					document.writeln('<br />zoom: ' + level);
				},
				draw : function(){
					document.writeln('<br />draw');
				}
			};
			
			var foo = new Foo();
			displayRoute(foo);
			
		</script>
	</head>
</html>	
	


该贴由hui.chen转至本版2016-2-1 10:18:33


人们都向往和寻找快乐,其实快乐就是一种心情,秘密隐藏于人的心中。快乐从不曾远离我们,而是我们远离了快乐。原本单纯的心,一旦复杂起来,快乐和幸福就 会远离而去,烦恼和忧愁就会随之而来。






赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论