当前位置:网站首页>装饰器模式:Swift 实现
装饰器模式:Swift 实现
2022-08-11 07:44:00 【大头鑫】
装饰器 Decorator
The components and decorators are departed, we can add new components and decorators freely. And we can make different compositions(with different decorations) to be different products.
组件模块和装饰器是分开的,我们可以自由地添加组件和装饰器。我们也可使用不同的组合形式(基于不同装饰方式),来得到不同的产品。

// Base interface between classes.
protocol Component {
var text: String? {
get set}
func execute()
}
// Concrete Component
class Bike: Component {
var text: String?
func execute() {
text = "bike"
}
}
// Concrete Component
class Moto: Component {
var text: String?
func execute() {
text = "moto"
}
}
// Base Decorator
class BaseDecorator: Component {
var text: String?
private var c: Component
func execute() {
c.execute()
if let t = c.text {
text = "<" + t + ">" // Here the text is the property of the current concrete decorator.
} else {
text = "<>"
}
}
func getText() -> String {
if let s = text {
return s
} else {
return ""
}
}
func setText(_ str: String?) {
text = str
}
func getC() -> Component {
return c
}
// inject an instance
init(_ c: Component) {
self.c = c
}
}
// Concrete Decorator
class PaintDecorator: BaseDecorator {
override func execute() {
super.execute()
extra()
}
func extra() {
let s = getText()
setText("(Paint)\(s)(Paint)")
}
}
// Concrete Decorator
class AttachTagDecorator: BaseDecorator {
override func execute() {
super.execute()
extra()
}
func extra() {
let s = getText()
setText("(Tag)\(s)(Tag)")
}
}
let a = Bike()
let tag = AttachTagDecorator(a)
let paint = PaintDecorator(tag)
paint.execute() // (Paint)<(Tag)<bike>(Tag)>(Paint)
print(paint.getText())
let b = Moto()
let bpaint = PaintDecorator(b)
let bTag = AttachTagDecorator(bpaint)
bTag.execute() // (Tag)<(Paint)<moto>(Paint)>(Tag)
print(bTag.getText())
上面代码中,组件有 Bike 和 Moto,装饰器有 AttachTagDecorator 和 PaintDecorator。对 Bike 先贴标签再绘图,而对 Moto 是先画图再贴标签,获得的结果中可以看出两个组件被包装的方式不一样。
// (Paint)<(Tag)<bike>(Tag)>(Paint)
// (Tag)<(Paint)<moto>(Paint)>(Tag)
边栏推荐
- 我的创作纪念日丨感恩这365天来有你相伴,不忘初心,各自精彩
- XXL-JOB 分布式任务调度中心搭建
- TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
- Find the latest staff salary and the last staff salary changes
- 1091 N-自守数 (15 分)
- Activity的四种启动模式
- 【TA-霜狼_may-《百人计划》】图形3.7.2 command buffer简
- 1071 Small Gamble (15 points)
- 1051 复数乘法 (15 分)
- 美术2.4 UV原理基础
猜你喜欢

Project 1 - PM2.5 Forecast

机器学习(一)数据的预处理

tf.cast(), reduce_min(), reduce_max()

【TA-霜狼_may-《百人计划》】图形3.7.2 command buffer简
几何EX3 功夫牛宣布停售,入门级纯电产品为何总成弃子

C语言操作符详解

【LeetCode】链表题解汇总

go-grpc TSL authentication solution transport: authentication handshake failed: x509 certificate relies on ... ...

FPGA 20个例程篇:11.USB2.0接收并回复CRC16位校验

TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
随机推荐
Service的两种启动方式与区别
1101 How many times B is A (15 points)
Four startup modes of Activity
1.2-误差来源
1076 Wifi密码 (15 分)
1106 2019数列 (15 分)
oracle19c does not support real-time synchronization parameters, do you guys have any good solutions?
TF通过feature与label生成(特征,标签)集合,tf.data.Dataset.from_tensor_slices
【BM87 合并两个有序的数组】
One-hot in TF
Machine Learning Summary (2)
Unity开发者必备的C#脚本技巧
欢迎加入sumarua网络安全交流社区
1.2 - error sources
4.1 - Support Vector Machines
AcWing 272. 最长公共上升子序列
1096 大美数 (15 分)
CIKM 2022 AnalytiCup Competition: Federal Heterogeneous Task Learning
JRS303-数据校验
【C语言】每日一题,求水仙花数,求变种水仙花数