当前位置:网站首页>装饰器模式: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)
边栏推荐
- 1046 划拳 (15 分)
- 1081 Check Password (15 points)
- 初级软件测试工程师笔试试题,你知道答案吗?
- The easiest trick to support quick renaming of various files
- There may be fields that cannot be serialized in the abnormal object of cdc and sqlserver. Is there anyone who can understand it? Help me to answer
- 欢迎加入sumarua网络安全交流社区
- 1101 How many times B is A (15 points)
- My creative anniversary丨Thank you for being with you for these 365 days, not forgetting the original intention, and each is wonderful
- 通过记账,了解当月收支情况
- Hibernate 的 Session 缓存相关操作
猜你喜欢
1.1-Regression

leetcode: 69. Square root of x

Mysql JSON对象和JSON数组查询

Find the latest staff salary and the last staff salary changes

基于微信小程序的租房小程序

matrix multiplication in tf

关于#sql#的问题:怎么将下面的数据按逗号分隔成多行,以列的形式展示出来

Square, multi-power, square root calculation in Tf

Project 1 - PM2.5 Forecast

Write a resume like this, easy to get the interviewer
随机推荐
pyqt5实现仪表盘
Activity的四种状态
About # SQL problem: how to set the following data by commas into multiple lines, in the form of column display
4.1-支持向量机
1002 写出这个数 (20 分)
1051 Multiplication of Complex Numbers (15 points)
Tensorflow中使用tf.argmax返回张量沿指定维度最大值的索引
机器学习(三)多项式回归
Service的两种启动方式与区别
2022-08-10 mysql/stonedb-慢SQL-Q16-耗时追踪
Pico neo3 Unity打包设置
Machine Learning Summary (2)
2022-08-10 mysql/stonedb-slow SQL-Q16-time-consuming tracking
Conditional statements in TF; where()
TF通过feature与label生成(特征,标签)集合,tf.data.Dataset.from_tensor_slices
JRS303-数据校验
Use tf.argmax in Tensorflow to return the index of the maximum value of the tensor along the specified dimension
1056 Sum of Combinations (15 points)
Decrement operation in tf; tf.assign_sub()
tf.cast(), reduce_min(), reduce_max()