<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>javascript高级语法5-接口</title>
</head>
<body>
<script>
/*
* 接口:
* 1.注解的方法
* 最简单,功能最弱,利用interface和implement“文字”
* 把他们用注解的方式显式表现出来。
* 2.属性检验法
* 3.鸭式变形法 * */ //1.注解方法
function demo1(){
(function b(){
/*
* 用注释来定义一个接口
* interface PersonDao(){
* function add(obj);
* function remove(obj);
* function find(id);
* }
*/
//用注释的方式实现它
/*
* PersonDaoImpl implement interface
* */
var PersonDaoImpl = function(){};
PersonDaoImpl.prototype.add = function(obj){
//
}
PersonDaoImpl.prototype.remove = function(obj){
//
}
PersonDaoImpl.prototype.find = function(id){
//
}
/*
* 千万不要感觉它是没有任何意义的
* 1.大型项目靠的就是标准和规范
* 2.这样的写法会让你的程序员在没有写实现之前有充分时间做代码的设计和架构
* 3.缺点:要人为的遵守 * */
})();
}
//2.属性检验法
function demo2(){
(function(){
/*
* 用注释来定义一个接口
* interface PersonDao(){
* function add(obj);
* function remove(obj);
* function find(id);
* }
*/
/*
* PersonDaoImpl implement interface
* */
var PersonDaoImpl = function(){
this.implementInterface=["PersonDao"];
};
PersonDaoImpl.prototype.add = function(obj){
//
alert(obj);
}
PersonDaoImpl.prototype.remove = function(obj){
//
}
PersonDaoImpl.prototype.find = function(id){
//
} function addObj(obj){
var PersonDao = new PersonDaoImpl();
//开始检查
if(!impl(PersonDao,"PersonDao")){
throw new Error("类PersonDaoImpl没有实现接口PersonDao")
}else{
PersonDao.add(obj);
}
}
addObj("张三")
//他接收一个不定参数:属性检验函数
function impl(obj){
//遍历传入对象的属性
for(var i=1;i<arguments.length;i++){
var interfaceName = arguments[i];
var interfaceFound = false;
for(var j=0;j<obj.implementInterface.length;j++){
if(obj.implementInterface[j] == interfaceName){
interfaceFound=true;
break;
}
}
if(!interfaceFound){
return false;
}
}
return true;
}
})()
} //3. 鸭式变形法:
/*来源于一个国外老头,他有句名言:
* "像鸭子一样走路,并且会嘎嘎叫的东西"
* 换言之:
* 如果对象具有与接口定义的方法名字的所有方法(同名的所有方法),那么就认为实现了接口。
*/ function demo3(){
(function(){
//定义一个接口类
var Interface = function(name ,methods){
if(arguments.length!=2){
alert("interface must have two params")
}
this.name = name; //接口名字
this.methods = [];//定义一个空数组来装载函数名
for(var i=0;i<methods.length;i++){
if(typeof methods[i] != "string"){
alert("method name must be string")
}else{
this.methods.push(methods[i]);
}
}
}
// 定义接口的一个静态方法来实现接口与实现类的直接检验
//静态方法不要写成Interface.prototype.* 因为这是写到接口的原型链上的
//我们要把静态的函数直接写到类层次上。
Interface.ensureImplements = function(obj){
if(arguments.length<2){
alert("至少两个参数");
return false;
}
//遍历
for(var i=1;i<arguments.length;i++){
var inter = arguments[i];
//如果你是接口,就必须是interface类型的,
if(inter.constructor != Interface){
throw new Error("必须是接口类型")
}
//遍历函数集合并分析
for(var j=0;j<inter.methods.length;j++){
var method = inter.methods[j];
//实现类中必须有方法名 和 接口中所有的方法名相同
if(!obj[method] || typeof obj[method]!="function"){
throw new Error("类中实现类并没有完全实现接口中的所有方法")
}
}
}
}; //应用
//定义自己的接口
var GridManager = new Interface("GridManager",["add","remove","list"]);
var FormManager = new Interface("FormManager",["save"]); function commonManager(){
//先实现方法
this.add = function(){
alert("ok");
}
this.list = function(){}
this.remove = function(){}
this.save = function(){}
//检验
Interface.ensureImplements(this,GridManager,FormManager)
} var c = new commonManager();
c.add();
})()
}
/* 接口的重要性:
* 1.大型项目提高代码的灵活度
* 2.松耦合
* 3.在团队开发的时候,有时候在你真正编码之前就可以写API(自己的类库)。
* 这些类库就可以事后进行实现。
* 开始的时候我们就可以对整个项目是否可行,通过接口就可以模拟出来。
*/ </script>
</body>
</html>

最新文章

  1. Java设计模式之-----策略模式
  2. BZOJ 2595 斯坦那树
  3. 【PowerOJ1740&amp;网络流24题 圆桌聚餐】(最大流)
  4. 新手接触java
  5. Java--常用类summary(二)
  6. leetcode 138. Copy List with Random Pointer ----- java
  7. Theano FCN实现与训练经验与教训小结
  8. Linux Increase The Maximum Number Of Open Files / File Descriptors (FD)
  9. ios 运行模式
  10. Hibernate映射1
  11. iscroll使用之页面卡顿问题
  12. 在linux内核中修改TCP MSS值
  13. scrapy爬取全部知乎用户信息
  14. python中的赋值操作和复制操作
  15. Git合并不同url的项目
  16. poj 1679 判断MST是不是唯一的 (次小生成树)
  17. 如何消除“为帮助保护您的安全,Internet Explorer 已经限制此文件显示可能访问您计算机
  18. 百度编辑器插入视频、iframe 失败
  19. conv1d UpSampling1D aotoencoder 自编码代码摘录
  20. (LeetCode 78)SubSets

热门文章

  1. CodeForces - 468A ——(思维题)
  2. 更改SQL Server中默认备份文件夹
  3. Web 协议 HTTP1.0 HTTP1.1 SPDY HTTP2.0
  4. Linux中连接mysql执行sql文件
  5. .netcore Swagger 生成 api接口文档
  6. Android 屏幕适应
  7. “全栈2019”Java多线程第九章:判断线程是否存活isAlive()详解
  8. Java中Arrays工具类
  9. 0基础浅谈反射型xss(2)
  10. jenkins详解(一)