-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathContents.swift
65 lines (50 loc) · 1.12 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns
import UIKit
// 协议
protocol Person {
func show()
}
// 遵守协议
struct Boy: Person {
var name = ""
init() {}
init(_ name: String) {
self.name = name
}
func show() {
print("\(name)")
}
}
// 饰物遵守协议
class Finery: Person {
var component: Person
init(_ component: Person) {
self.component = component
}
func show() {
component.show()
}
}
// 继承
class TShirt: Finery {
override func show() {
print("T 恤", separator: "", terminator: " + ")
super.show()
}
}
// 继承
class ChineseTunicSuit: Finery {
override func show() {
print("中山装", separator: "", terminator: " + ")
super.show()
}
}
var b = Boy("Kingcos")
// 按顺序装饰
let tShirtA = TShirt(b)
let chineseTunicSuitA = ChineseTunicSuit(tShirtA)
chineseTunicSuitA.show()
let chineseTunicSuitB = ChineseTunicSuit(b)
let tShirtB = TShirt(chineseTunicSuitB)
tShirtB.show()