无标题文章

JavaScript 设计模式学习笔记二(共十七章)

第二章 接口

什么是接口

接口提供了一种说明以一个对象应该具有哪些方法的手段。

接口之利

接口具有自我描述性,并能促进代码重用。有助于稳定不同类之间的通信方式。

接口之弊

JavaScript 的弱类型特定,使得它具有极强的语言表现力。而接口的使用则在一定程度上强化了类的作用,这降低了语言的灵活性。

其他面向对象语言处理接口的方式

java

public interface DataOutput{
    void writeBoolean(boolean value) throws IOException;
    void writeChar(int value) throws IOException;
    void writeShort(int value) throws IOException;
    void writeInt(int value) throws IOException;
}

/*接口的实现*/
public class DataOutputSteam extends filterDataOutputStream implements DataOutPut{
    public final void writeBoolean (boolean value) throws IOException{
        write(value ? 1 : 0 );
    }
}

php

interface MyInterface{
    public function interfaceMethod($argumentOne,$argumentTwo);    
}
class MyClass implements MyInterface{
    public function interfaceMethod($argumentOne,$argumentTwo){
        return $argumentOne . $argumentTwo
    }
}

C#

interface MyInterface{
    string interfaceMethod(string argumentOne,string argumentTwo);
}
class MyClass : MyInterface{
    public string interfaceMethod(string argumentOne,string argumentTwo){
        return argumentOne + argumentTwo;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 3,281评论 0 4
  • 设计模式之六大原则(转载) 设计模式之六大原则(转载) 关于设计模式的六大设计原则的资料网上很多,但是很多地方解释...
    天涯人1196阅读 349评论 0 0
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,857评论 0 9
  • 任务: 阅读永澄老师关于反思复盘的6份资料 我的学习成果: 1. 明确了复盘的目的、关键点、底层逻辑及概念 复盘的...
    爱笑的布袋子阅读 287评论 0 0
  • 一钟醉,黛眉桃面兰心惠,伊人浅吟梦已碎。寒夜沉沉,青烟缕缕。痴心何人会? 二钟醉,玉肌红唇容颜悴,佳人轻抚千秋岁。...
    Todd_雲轩阅读 435评论 17 15