Kotlin设计模式
来源:互联网 发布:skype聊天软件下载 编辑:程序博客网 时间:2024/06/10 10:21
装饰器模式
动态地给对象添加行为(职责)
假设我们要装饰 Text这个类:
class Text(val text: String) { fun draw(){ print(text) }}
为这个Text “装饰” 即 拓展行为
fun main(args: Array<String>) { Text("Hello").apply { background { underline { draw() } } }}class Text(val text: String) { fun draw(){ print(text) }}//用扩展函数 拓展行为fun Text.underline(decorated: Text.() -> Unit) { print("_") this.decorated() print("_")}fun Text.background(decorated: Text.() -> Unit) { print("\u001B[43m") this.decorated() print("\u001B[0m")}
阅读全文
0 0
- Kotlin设计模式
- Kotlin设计模式
- Kotlin设计模式
- Kotlin设计模式
- Kotlin 设计模式-工厂
- Kotlin 设计模式-策略
- Kotlin设计模式-状态
- Kotlin 设计模式-命令
- Kotlin 设计模式-访问者
- Kotlin 设计模式-适配器
- Kotlin 设计模式-观察者
- Kotlin设计模式-外观
- Kotlin 设计模式-建造者
- Kotlin实现MVP设计模式
- Kotlin设计模式-单例
- Kotlin 设计模式-装饰器
- Kotlin特色风格实现gof设计模式
- Kotlin设计模式实现之行为型设计模式
- dup/dup2实现客户端
- Spark RDD存储开销分析
- .11四种属性范围
- DTO – 服务实现中的核心数据
- MariaDB yum安装
- Kotlin设计模式
- intellji idea 创建java文件
- logback日志
- Netflix archaius动态配置工具使用
- ajax请求过程
- roketmq单机版本
- Effective C++第三章-资源管理-2
- <NOIP> 21 . P1914 小书童——密码
- Mybatis配置文件报错“元素类型为 "configuration" 的内容必须匹配……”