×
CoffeeScript 关于

CoffeeScript 语法

服务端和客户端的代码重用CoffeeScript 比较范围CoffeeScript 嵌入 JavaScriptCoffeeScript For 循环

CoffeeScript 类和对象

CoffeeScript 对象的链式调用CoffeeScript 类方法和实例方法CoffeeScript 类变量和实例变量CoffeeScript 克隆对象CoffeeScript 类的混合CoffeeScript 创建对象字面值CoffeeScript type 函数

CoffeeScript 字符串

CoffeeScript 大写单词首字母CoffeeScript 查找子字符串CoffeeScript 生成唯一 IDCoffeeScript 字符串插值CoffeeScript 字符串小写转换CoffeeScript 匹配字符串CoffeeScript 重复字符串CoffeeScript 拆分字符串CoffeeScript 清理字符串前后CoffeeScript 字符串大写转换

CoffeeScript 数组

CoffeeScript 检查变量是否数组CoffeeScript 将数组连接CoffeeScript 数组创建对象词典CoffeeScript 数组创建字符串CoffeeScript 定义数组范围CoffeeScript 筛选数组CoffeeScript 列表推导CoffeeScript 映射数组CoffeeScript 数组最大值CoffeeScript 归纳数组CoffeeScript 删除数组相同元素CoffeeScript 反转数组CoffeeScript 打乱数组中的元素CoffeeScript 检测每个元素CoffeeScript 用数组来交换变量CoffeeScript 对象数组类似 Python 的 zip 函数

CoffeeScript 日期和时间

CoffeeScript 计算复活节的日期CoffeeScript 计算感恩节日期计算两个日期中间的天数CoffeeScript 算1个月中最后1天CoffeeScript 找到上/下一个月CoffeeScript 计算月球的相位

CoffeeScript 数学

CoffeeScript 数学常数更快的 Fibonacci 算法平方根倒数快速算法生成可预测的随机数CoffeeScript 生成随机数CoffeeScript 转换弧度和度CoffeeScript 一个随机整数函数CoffeeScript 指数对数运算CoffeeScript 方法CoffeeScript 去抖动函数CoffeeScript 当函数括号不可选CoffeeScript 递归函数CoffeeScript 提示参数CoffeeScript 元编程检测与构建丢失函数CoffeeScript 扩展内置对象

CoffeeScript jQuery

CoffeeScript AJAXCoffeeScript 回调绑定CoffeeScript 创建 jQuery 插件CoffeeScript AJAXCoffeeScript 无jQuery的Ajax

CoffeeScript 正则表达式

CoffeeScript 使用 HeregexesHTML实体替换 HTML 标签CoffeeScript 替换子字符串CoffeeScript 查找子字符串CoffeeScript 网络CoffeeScript 客户端CoffeeScript HTTP 客户端CoffeeScript 基本HTTP服务器CoffeeScript 服务器CoffeeScript 双向客户端CoffeeScript 双向服务器

CoffeeScript 设计模式

CoffeeScript 适配器模式CoffeeScript 桥接模式CoffeeScript 生成器模式CoffeeScript 命令模式CoffeeScript 修饰模式CoffeeScript 工厂方法模式CoffeeScript 解释器模式CoffeeScript 备忘录模式CoffeeScript 观察者模式CoffeeScript 单件模式CoffeeScript 策略模式CoffeeScript 模板方法模式

CoffeeScript 数据库

CoffeeScript MongoDBCoffeeScript SQLite

CoffeeScript 测试

使用 Jasmine 测试使用 Nodeunit 测试

CoffeeScript 适配器模式


问题

想象你去国外旅行,一旦你意识到你的电源线插座与酒店房间墙上的插座不兼容时,幸运的是你记得带你的电源适配器。它将一边连接你的电源线插座另一边连接墙壁插座,允许它们之间进行通信。

同样的情况也可能会出现在代码中,当两个 ( 或更多 ) 实例 ( 类、模块等 ) 想跟对方通信,但其通信协议 ( 例如,他们所使用的语言交流 ) 不同。在这种情况下,Adapter模式更方便。它会充当翻译,从一边到另一边。

解决方案

# a fragment of 3-rd party grid component
class AwesomeGrid
    constructor: (@datasource)->
        @sort_order = 'ASC' 
        @sorter = new NullSorter # in this place we use NullObject pattern (another useful pattern)
    setCustomSorter: (@customSorter) ->
        @sorter = customSorter
    sort: () ->
        @datasource = @sorter.sort @datasource, @sort_order
        # don't forget to change sort order

class NullSorter
    sort: (data, order) -> # do nothing; it is just a stub

class RandomSorter
    sort: (data)->
        for i in [data.length-1..1] #let's shuffle the data a bit
                j = Math.floor Math.random() * (i + 1)
                [data[i], data[j]] = [data[j], data[i]]
        return data

class RandomSorterAdapter
    constructor: (@sorter) ->
    sort: (data, order) ->
        @sorter.sort data

agrid = new AwesomeGrid ['a','b','c','d','e','f']
agrid.setCustomSorter new RandomSorterAdapter(new RandomSorter)
agrid.sort() # sort data with custom sorter through adapter

讨论

当你要组织两个具有不同接口的对象之间的交互时,适配器是有用的。它可以当你使用第三方库或者使用遗留代码时使用。在任何情况下小心使用适配器:它可以是有用的,但它也可以导致设计错误。


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)