scala实现设计模式之装饰者模式
来源:互联网 发布:吾生而有涯而知而无涯 编辑:程序博客网 时间:2024/05/29 02:02
package com.linewell.modeldesgin.decorator/** * 抽象界面构件类 * Created by ctao on 2015/8/16. */trait Component { def display(): Unit}/** * 窗口具体构件类 */class Window extends Component { override def display(): Unit = println("显示窗口")}/** * 文本框具体构件类 */class TextBox extends Component { override def display(): Unit = println("显示文本框")}/** * 列表具体构件类 */class ListBox extends Component { override def display(): Unit = println("显示列表框")}/** * 构件装饰样例类 * @param component 抽象构件 */case class ComponentDecorator(component: Component) extends Component { /** * 复写display方法,调用抽象构建的方法 */ override def display(): Unit = component.display()}/** * 滚动条装饰类 * @param component 抽象构件 */class ScrollBarDecorator(component: Component) extends ComponentDecorator(component) { /** * 复写父类方法,在复写的方法中调用自己的独有方法 */ override def display() = { scrollBar() super.display() } /** * 自己独有方法 */ def scrollBar() = println("为构件增加滚动条")}/** * 黑色边框装饰类 * @param component 抽象构件 */class BlackBorderDecorator(component: Component) extends ComponentDecorator(component) { override def display() = { blackBorder() super.display() } def blackBorder() = println("为构件增加黑色边框")}
package com.linewell.modeldesgin.decorator/** * 测试客户端 * Created by ctao on 2015/8/16. */object Client extends App { /** * 窗口 */ val component: Component = new Window /** * 滚动条来装饰窗口 */ val componentSB: Component = new ScrollBarDecorator(component) /** * 黑色边框装饰滚动条装饰类 */ val componentBB: Component = new BlackBorderDecorator(componentSB) componentBB.display()}
0 0
- scala实现设计模式之装饰者模式
- 设计模式之装饰模式(C++实现)
- 设计模式之装饰模式Java实现
- scala实现设计模式之原型模式
- scala实现设计模式之适配器模式
- scala实现设计模式之组合模式
- scala实现设计模式之外观模式
- scala实现设计模式之代理模式
- scala实现设计模式之命令模式
- scala实现设计模式之备忘录模式
- scala实现设计模式之迭代器模式
- scala实现设计模式之状态模式
- scala实现设计模式之观察者模式
- scala实现设计模式之访问者模式
- 设计模式之装饰者模式 及 c++实现
- 设计模式之装饰者模式(c++实现)
- 设计模式之装饰者模式C++实现
- 设计模式之装饰者设计模式
- 回溯--迷宫问题
- Java内存分配和String类型的深度解析
- Android 生成正式签名的APK文件
- Android实战简易教程-第三十七枪(ListView中点击button跳转到拨号界面实例)
- hdu 1864 最大报销额 动态规划
- scala实现设计模式之装饰者模式
- uC/OS-II 函数之信号量相关函数
- NOIP2010 引水入城(BFS+贪心)
- 再次开始学习python-Python Web实战 第一课
- linux下tomcat的安装
- 理解协议委托者代理者问题
- ZOJ 1654 Place the Robots (二分匹配 )
- Spring---AOP简介
- 九度oj 1053