javascript面向对象编程(三)

作者:canque 来源:CanQue@RSSIDEA 时间:2008-03-07 13:19:00 

阅读上一篇:javascript面向对象编程(二)

[Interface,Class.implement 接口及实现]

接口规定了一些方法,如果一个类实现了接口所定义的所有方法,就叫做实现了这个接口。诚然,javascript来模拟接口会带来一些效率上的损失,但是在大型项目特别是团队开发的时候,接口将带来很大的方便。使项目代码更加规范,更方便地查找错误信息。很多设计模式,像工厂模式、合成模式、装饰模式、命令模式等都依赖于接口来实现。

javascript模拟接口包括两部分:接口的模拟和接口实现的模拟。

[Interface接口类]

这段代码定义一个接口类,同样来自于《javascript design patterns》,略有改动。

// Constructor.
    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 expects method names to be " +
                "passed in as a string.");
            }
            this.methods.push(methods[i]);
        }
    }; 

[Interface.ensureImplements 接口实现检查]

这段代码检查一个对象是否实现了所要实现接口。

// Static class method.
    Interface.ensureImplements = function(object){
        if (arguments.length <2) {
            throw new Error('Interface.ensureImplements:Wrong arguments number');
        }
        for (var i = 1, len = arguments.length; i <len; i++) {
            var interfaces = arguments[i];
            if (interfaces.constructor !== Interface) {
                throw new Error("Function Interface.ensureImplements expects arguments " +
                "two and above to be instances of Interface.");
            }
            for (var j = 0, methodLen = interfaces.methods.length; j <methodLen; j++) {
                var method = interfaces.methods[j];
                if (!object.prototype[method] || typeof(object.prototype[method]) !== 'function') {
                    throw new Error("Function Interface.ensureImplements: object " +
                    "does not implement the " +
                    interfaces.name +
                    " interface. Method " +
                    method +
                    " was not found.");
                }
            }
        }
    } 

[Class.implement类实现接口]

接口定义后,还不能带来任何好处,需要在类中体现出来。看下面的两段代码就很清楚了:程序演示页面(3)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script language="JavaScript" src="http://rssidea.com/labs/OOP/OOP.js"></script>
<title>labs@RSSIDEA javascript OOP</title>
</head>
<body>
<script language="JavaScript">
    //定义一个computer的接口,要求实现motherborad,memory,cpu,harddisk的方法。
    var computer = new Interface('computer', ['motherboard', 'memory', 'cpu', 'harddisk'])
    //定义一个PC类,实现computer接口
    var PC = Class.create({
        motherboardInfo: '',
        memoryInfo: '',
        cpuInfo: '',
        harddiskInfo: '',
        init: function(motherboard, memory, cpu, harddisk){
            this.motherboardInfo = motherboard;
            this.memoryInfo = memory;
            this.cpuInfo = cpu;
            this.harddiskInfo = harddisk;
        },
        motherboard: function(){
            return this.motherboardInfo;
        },
        memory: function(){
            return this.memoryInfo;
        },
        cpu: function(){
            return this.cpuInfo;
        },
        mouse:function(){
            return this.mouseInfo;
        }
    }).implement(computer);//浏览器报错,(没有硬盘电脑怎么转啊?)
    var myComputer = new PC('微星主板', '现代2G', 'AMD3600+', '三星80G');
    //输出电脑配置信息
    trace(myComputer);
</script>
</body>
</html> 

但是这段代码我们却得到一个浏览器错误,方法harddisk没有实现。(没有硬盘叫什么电脑啊?)

好吧,好吧!我承认是我拿买硬盘的钱去买了一个鼠标……(这鼠标不错吧?)

我原以为可以混过去,却被检查出来了。接口的工作和作用和上面的情况类似。现在,我需要再买个硬盘(harddisk)。程序演示页面(4)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script language="JavaScript" src="http://rssidea.com/labs/OOP/OOP.js"></script>
<title>labs@RSSIDEA javascript OOP</title>
</head>
<body>
<script language="JavaScript">
    //定义一个computer的接口,要求实现motherborad,memory,cpu,harddisk的方法。
    var computer = new Interface('computer', ['motherboard', 'memory', 'cpu', 'harddisk'])
    //定义一个PC类,实现computer接口
    var PC = Class.create({
        motherboardInfo: '',
        memoryInfo: '',
        cpuInfo: '',
        harddiskInfo: '',
        init: function(motherboard, memory, cpu, harddisk){
            this.motherboardInfo = motherboard;
            this.memoryInfo = memory;
            this.cpuInfo = cpu;
            this.harddiskInfo = harddisk;
        },
        motherboard: function(){
            return this.motherboardInfo;
        },
        memory: function(){
            return this.memoryInfo;
        },
        cpu: function(){
            return this.cpuInfo;
        },
        mouse:function(){
            return this.mouseInfo;
        },
        harddisk:function(){
            return this.harddiskInfo;
        }//这是新买的硬盘
    }).implement(computer);//实现了computer接口
    var myComputer = new PC('微星主板', '现代2G', 'AMD3600+', '三星80G');
    //输出电脑配置信息
    trace(myComputer);
</script>
</body>
</html> 

太好了!浏览器不再报错,这样,我们就实现了computer接口。

下一篇:javascript面向对象编程(四)

标签:面向对象,编程,oop,javascript
0
投稿

猜你喜欢

  • 关于Javascript的内存泄漏问题

    2008-04-15 07:46:00
  • 什么是网站灵魂?

    2007-09-08 08:25:00
  • CSS背景图片的运用优化HTTP连接数

    2008-09-04 21:38:00
  • ASP实现防止网站被采集代码

    2011-03-25 10:40:00
  • CSS阴影详解

    2009-12-04 18:31:00
  • 多级联动下拉选择框,动态获取下一级

    2008-09-04 10:34:00
  • 解读HTML:大厦的基石

    2008-12-01 12:57:00
  • 做设计还是做产品

    2009-06-11 13:01:00
  • 避免重复写代码的小函数

    2008-09-21 13:41:00
  • ASP利用TCPIP.DNS组件实现域名IP查询

    2010-02-26 11:25:00
  • IE7异常CSS 导致内存破坏漏洞

    2009-11-30 12:52:00
  • css清除浮动的最优方法

    2008-04-25 22:33:00
  • Asp模板制作方法详解

    2007-10-11 19:05:00
  • PHP结构型模式之代理模式

    2023-05-25 06:55:34
  • ORACLE 正则解决初使化数据格式不一致

    2009-05-24 19:44:00
  • 5个css布局的常见问题及解决方法

    2009-11-19 13:21:00
  • 详细了解 MySQL锁机制

    2010-08-08 09:04:00
  • 为JavaScript程序添加客户端不可见的注释

    2008-05-31 08:02:00
  • 如何取得表中字段的属性?

    2010-01-18 20:52:00
  • ORCLE 表中列的修改

    2009-07-28 10:42:00
  • asp之家 网络编程 m.aspxhome.com