设计模式学习笔记——装饰(Decorator)模式
设计模式学习笔记——装饰(Decorator)模式
@(设计模式)[设计模式, 装饰模式, decorator]
- 设计模式学习笔记装饰Decorator模式
- 基本介绍
- 装饰案例
- 类图
- 实现代码
- Display抽象类
- StringDisplay类
- Border抽象类
- SideBorder类
- FullBorder类
- 测试类
- 运行结果
- 装饰模式中的角色
- Component
- ConcreteComponent
- Decorator装饰物
- ConcreteDecorator具体的装饰物
- 类图
基本介绍
装饰模式主要是通过委托机制,继承被装饰者的父类或者实现其接口,来做到不断的为被装饰者添加装饰。
java
中的io
包就是使用装饰者模式的很好的案例。
装饰案例
类图
实现代码
Display抽象类
package com.pc.decorator.example;/*** 显示抽象类* Created by Switch on 2017-02-21.*/
public abstract class Display {/*** 获取每行的字符数** @return 每行的字符数*/public abstract int getColumns();/*** 获取行数** @return 行数*/public abstract int getRows();/*** 获取指定行的文本** @param row 行数* @return 文本*/public abstract String getRowText(int row);/*** 全部显示*/public final void show() {for (int i = 0; i < this.getRows(); i++) {System.out.println(getRowText(i));}}
}
StringDisplay类
package com.pc.decorator.example;/*** 字符串显示类* Created by Switch on 2017-02-21.*/
public class StringDisplay extends Display {/*** 带显示的字符串*/private String string;/*** 构造方法,传入字符串** @param string 字符串*/public StringDisplay(String string) {this.string = string;}@Overridepublic int getColumns() {return this.string.getBytes().length;}@Overridepublic int getRows() {return 1;}@Overridepublic String getRowText(int row) {if (row == 0) {return this.string;}return null;}
}
Border抽象类
package com.pc.decorator.example;/*** 边框装饰抽象类* Created by Switch on 2017-02-21.*/
public abstract class Border extends Display {/*** 待装饰的对象*/protected Display display;/*** 构造方法,传入待装饰的显示类对象** @param display 显示类对象*/public Border(Display display) {this.display = display;}
}
SideBorder类
package com.pc.decorator.example;/*** 侧面边框装饰类* Created by Switch on 2017-02-21.*/
public class SideBorder extends Border {/*** 装饰字符*/private char decorateChar;/*** 构造方法,传入待装饰的显示类对象** @param display 显示类对象*/public SideBorder(Display display, char decorateChar) {super(display);this.decorateChar = decorateChar;}@Overridepublic int getColumns() {return this.display.getColumns() + 2;}@Overridepublic int getRows() {return this.display.getRows();}@Overridepublic String getRowText(int row) {// 在两边加上装饰字符return this.decorateChar + display.getRowText(row) + this.decorateChar;}
}
FullBorder类
package com.pc.decorator.example;/*** 完全边框装饰类* Created by Switch on 2017-02-21.*/
public class FullBorder extends Border {/*** 构造方法,传入待装饰的显示类对象** @param display 显示类对象*/public FullBorder(Display display) {super(display);}@Overridepublic int getColumns() {return display.getColumns() + 2;}@Overridepublic int getRows() {return display.getRows() + 2;}@Overridepublic String getRowText(int row) {if (row == 0) { // 上边框return "+" + makeLine('-', display.getColumns()) + "+";} else if (row == display.getRows() + 1) { // 下边框return "+" + makeLine('-', display.getColumns()) + "+";} else { // 内容return "|" + this.display.getRowText(row - 1) + "|";}}/*** 生成一个重复的count次的ch字符串** @param c 字符* @param count 次数* @return 重复的count次的ch字符串*/private String makeLine(char c, int count) {StringBuilder builder = new StringBuilder();for (int i = 0; i < count; i++) {builder.append(c);}return builder.toString();}
}
测试类
package com.pc.decorator.example.test;import com.pc.decorator.example.Display;
import com.pc.decorator.example.FullBorder;
import com.pc.decorator.example.SideBorder;
import com.pc.decorator.example.StringDisplay;
import org.junit.Test;/*** Display Tester.** @author Switch* @version 1.0*/
public class DisplayTest {/*** 测试装饰模式*/@Testpublic void testDisplay() {Display display1 = new StringDisplay("Hello World!");Display display2 = new SideBorder(display1, '#');Display display3 = new FullBorder(display2);display1.show();display2.show();display3.show();System.out.println();Display display4 = new FullBorder(new SideBorder(new FullBorder(new StringDisplay("Hello")), '#'));display4.show();}
}
运行结果
Hello World!
#Hello World!#
+--------------+
|#Hello World!#|
+--------------++---------+
|#+-----+#|
|#|Hello|#|
|#+-----+#|
+---------+
装饰模式中的角色
Component
增加功能时的核心角色。Component
角色只是定义了接口(API
)。在案例中,由Display
类扮演此角色。
ConcreteComponent
该角色实现了Component
角色所定义的接口(API
) 。在案例中,由StringDisplay
类扮演此角色。
Decorator(装饰物)
该角色具有与Component
角色相同的接口(API
)。在它内部保存了被装饰对象——Component
角色。Decorator
角色知道自己要装饰的对象。在案例中,由Border
类扮演此角色。
ConcreteDecorator(具体的装饰物)
该角色是具体的Decorator
角色。在案例中,由SideBorder
类和FullBorder
类扮演此角色。
类图
GitHub:DesignPatternStudy
——————参考《图解设计模式》
设计模式学习笔记——装饰(Decorator)模式相关推荐
- 设计模式学习笔记——解释器(Interpreter)模式
设计模式学习笔记--解释器(Interpreter)模式 @(设计模式)[设计模式, 解释器模式, Interpreter] 设计模式学习笔记解释器Interpreter模式 基本介绍 解释器案例 类 ...
- 设计模式学习笔记——命令(Command)模式
设计模式学习笔记--命令(Command)模式 @(设计模式)[设计模式, 命令模式, command] 设计模式学习笔记命令Command模式 基本介绍 命令案例 类图 实现代码 Command接口 ...
- 设计模式学习笔记——代理(Proxy)模式
设计模式学习笔记--代理(Proxy)模式 @(设计模式)[设计模式, 代理模式, proxy] 设计模式学习笔记代理Proxy模式 基本介绍 代理案例 类图 实现代码 Printable接口 Pri ...
- 设计模式学习笔记——状态(State)模式框架
设计模式学习笔记--状态(State)模式框架 @(设计模式)[设计模式, 状态模式, State] 设计模式学习笔记状态State模式框架 基本介绍 状态案例 类图 实现代码 State接口 Day ...
- 设计模式学习笔记——备忘录(Memento)模式
设计模式学习笔记--备忘录(Memento)模式 @(设计模式)[设计模式, 备忘录模式, memento] 设计模式学习笔记备忘录Memento模式 基本介绍 备忘录案例 类图 实现代码 Memen ...
- 设计模式学习笔记——观察者(Observer)模式
设计模式学习笔记--观察者(Observer)模式 @(设计模式)[设计模式, 观察者模式, Observer] 设计模式学习笔记观察者Observer模式 基本介绍 观察者案例 类图 实现代码 Ob ...
- 设计模式学习笔记——外观(Facade)模式
设计模式学习笔记--外观(Facade)模式 @(设计模式)[设计模式, 外观模式, facade] 设计模式学习笔记外观Facade模式 基本介绍 外观案例 类图 实现代码 Database类 ma ...
- 设计模式学习笔记——访问者(Visitor)模式
设计模式学习笔记--访问者(Visitor)模式 @(设计模式)[设计模式, 访问者模式, visitor] 设计模式学习笔记访问者Visitor模式 基本介绍 访问者案例 类图 实现代码 Visit ...
- 设计模式学习笔记——组合(Composite)模式
设计模式学习笔记--组合(Composite)模式 @(设计模式)[设计模式, 组合模式, composite] 设计模式学习笔记组合Composite模式 基本介绍 组合案例 类图 实现代码 Ent ...
最新文章
- android都图片mat_Android内存泄露整理
- python datetime 加一个月_Python 如何计算当前时间减少或增加一个月
- 张娟娟(为奥运冠军名字作诗)
- IOS学习笔记07---C语言函数-printf函数
- IBM连续20个季度营收下滑,但这可能是个好信号
- Windows 下80端口被进程 System PID=4 占用的解决方法
- In-Stream Big Data Processing
- video标签:以视频为背景的网页
- Android自定义View之刻度尺
- C# WPF 高级教程
- 用202种编程语言输出hello world,你会几种?(3)
- Word控件Spire.Doc 转换教程(二十一):将非标准字体的word文档转换为PDF
- JSP中应用eWebEditor在线编辑器
- SQL语句(五) 索引建立
- 小米五怎么设置锁屏显示无服务器,小米手机怎么设置锁屏状态下不能关机 - 卡饭网...
- mysql的填充因子_确定SQL Server填充因子(FILLFACTOR)值的方法
- 个人资源小仓库之【工具】!
- CLH(Craig, Landin, and Hagersten locks)机制
- 车辆识别码VIN校验位计算方法及实现-Java
- python中倒背如流_倒背如流中倒背是什么意思古代有种背书方法是倒背,倒
热门文章
- 安卓加java完成登录_从零学习安卓自动化(java+appium方向):完成登录操作+一个主流程(四)...
- C#LeetCode刷题-动态规划
- python unittest
- 亚马逊云服务开通指南_亚马逊弹性容器服务初学者指南
- cms核心功能_如何根据这些重要功能选择合适的CMS
- 145_Power BI Report Server自定义Form登录
- SpringMVC整合MongoDB开发 架构搭建
- java springMVC生成二维码
- python+selenium 爬携程机票价格
- Python 爬虫是什么