<style>
.reveal, .reveal h1, .reveal h2, .reveal h3, .reveal h4, .reveal h5, .reveal h6 {
font-family: "Source Sans Pro", "Helvetica Neue", Helvetica, Arial, "Microsoft JhengHei", Meiryo, "MS ゴシック", "MS Gothic", sans-serif;
}
h1, h2, h3, h4, h5, h6 {
text-transform: none !important;
}
.js-img {
background: url(https://upload.wikimedia.org/wikipedia/commons/thumb/6/6a/JavaScript-logo.png/480px-JavaScript-logo.png) center/contain;
width: 350px;
height: 350px;
display: block;
margin: 20px auto !important;
border: 4px solid white !important;
}
.hfbook-img {
background: url(http://images.thebookstores.net/imagesnew5c/9780596007126.jpg) center/contain;
width: 350px;
height: 350px;
display: block;
margin: 20px auto !important;
border: 4px solid white !important;
}
.sun0-img {
background: url(http://img.blog.163.com/photo/L56rCU_JmE3g5rkd3TjfTg==/1449877605037188949.jpg) center/contain;
width: 330px;
height: 238px;
display: block;
margin: 20px auto !important;
border: 4px solid white !important;
}
.sun-img {
background: url(http://www.tv543.com/images/kr/descendants-of-sun.jpg) center/contain;
width: 350px;
height: 260px;
display: block;
margin: 20px auto !important;
border: 4px solid white !important;
}
</style>
# Design Patterns - Swift
## Strategy Pattern
Michael-Chen<br />[@MichaelChen](https://www.facebook.com/helloWorldMichaelChen)
**參考書**
[Head First Design Patterns](https://www.tenlong.com.tw/items/9867794524?item_id=33235)
[Pro Design Patterns in Swift](https://www.tenlong.com.tw/items/9863477303?item_id=1008264)
---
<img src="https://scontent.ftpe1-1.fna.fbcdn.net/v/t1.0-9/10514653_298411263670726_6815406755312229312_n.jpg?oh=d2940e3c341c3c46efefd5c7c7a5ed0c&oe=58987C82" style="width:150px;height:150px;border-radius:50%"/>
Michael-Chen
Taipei, Taiwan
https://github.com/helloworldsmart
---
# Design Patterns ??
1. UI 設計
2. 抽象化問題, 用解耦,組裝來解決問題
3. Call in 美工姊姊 help
----
## Java 神悟 swift
![Head First Design Patterns Book](http://images.thebookstores.net/imagesnew5c/9780596007126.jpg)
----
## swift 1.2 神悟 swift 3.0
![Pro Design Patterns in Swift](http://pcmagazinepdf.com/uploads/posts/2015-01/thumbs/-3559_3833.jpg)
----
## What the hell !!
![cat](http://www.webuzzy.com/wp-content/uploads/2015/03/d837606.jpg)
----
# fork這本書
![Head First Design Patterns Book](http://images.thebookstores.net/imagesnew5c/9780596007126.jpg)
----
# Strategy Pattern
<br/>[@範例](https://github.com/helloworldsmart/DesignPatterns-Swift)
---
![sun0](http://img.blog.163.com/photo/L56rCU_JmE3g5rkd3TjfTg==/1449877605037188949.jpg)
## 太陽的后羿
----
![sun](http://www.tv543.com/images/kr/descendants-of-sun.jpg)
## 太陽的後裔
----
## 起手式
```swift=
var str = "Hello, 太陽的後裔"
```
----
### Soldier角色藍圖
```swift=
class Soldier {
//敬禮口號
func salute() {
print("團結 (단결)")
}
func backpacking() {
print("背大杯包, 裡面一堆裝備")
}
func display() {
}
}
```
----
## 創立各角色藍圖
```swift=
class TitleRoleSoldier: Soldier {
override func display() {
print("劉時鎮報到😎")
}
}
class SupportingRoleSoldier: Soldier {
override func display() {
print("徐大英報到😎")
}
}
class FemaleSupportingRoleSoldier: Soldier {
override func display() {
print("尹明珠報到😏")
}
}
```
---
## 這一幕場景
![sun](http://www.tv543.com/images/kr/descendants-of-sun.jpg)
----
#### 現在導演要為劉時鎮加幫女主角綁鞋帶的劇情, 這時要怎麼寫代碼呢?
#### issue: 劉時鎮,徐大英,尹明珠搶著幫女主角綁鞋帶 ?
#### 其他角色都不需執行幫女主角綁鞋帶的Behavior !!
----
1. 在基類(soldier類)中添加 TieShoelaces(), 其他演員重新改寫TieShoelaces code,這樣的話會被其他演員抱怨, 背這麼多台詞卻沒有用到, 一直mark(註解)掉方法
2. 使用protocol接口, 需要實現TieShoelaces()的演員,實現接口即可,頂多產生重複的code。
----
## 方案一
### Protocol Extensions Pattern
```swift=
protocol SweetBehavioral {
func shoesAction()
}
//為Protocol添加, 默認實現
extension SweetBehavioral {
func shoesAction() {
print("幫女主角綁鞋帶~👢")
}
}
```
----
## 劉時鎮實作SweetBehavioral協定, 再Extensions
## 參見WWDC 2015
```swift=
class TitleRoleSoldier: Soldier, SweetBehavioral {
override func display() {
print("劉時鎮報到😎")
}
}
let titleRoleSoldier: TitleRoleSoldier = TitleRoleSoldier()
titleRoleSoldier.shoesAction()
```
----
## 方案二
### Strategy Pattern (多型 + 代理)
----
#### 設計原則
1. 找出應用中可能需要變化之處,把它们獨立出来,不要和那些不變的code混在一起。
2. 針對接口编程(protocol),而不是針對實現编程(Copy-Paste寫法導致不同viewController, 重複好幾個同func)
3. 多用组合,少用繼承 ("團結 (단결)")
----
## 劉時鎮的行為協定
```swift=
protocol SweetBehavioral {
func shoesAction()
}
```
----
```swift=
////使用雙手綁鞋帶的類
class TieWithHands: SweetBehavioral {
func shoesAction() {
print("幫女主角綁鞋帶~👢")
}
}
class TieMySelfWithHands: SweetBehavioral {
func shoesAction() {
print("綁自己鞋帶~👞")
}
}
////使用雙手脫鞋子的類
class TakeOffShoesWithHands: SweetBehavioral {
func shoesAction() {
print("幫女主角脫鞋~👢")
}
}
```
----
```swift=
class Soldier {
//添加行為委托代理者
var handleShoesActionBehavior: SweetBehavioral! = nil
func setShoesActionBehavior(handleShoesActionBehavior: SweetBehavioral) {
self.handleShoesActionBehavior = handleShoesActionBehavior
}
//敬禮口號
func salute() {
print("團結 (단결)")
}
func backpacking() {
print("背大背包, 裡面一堆裝備")
}
func display() {
}
func performShoesAction() {
guard (self.handleShoesActionBehavior != nil) else {
return
}
self.handleShoesActionBehavior.shoesAction()
}
}
```
----
### 編劇: 劉時鎮角色藍圖
```swift=
class TitleRoleSoldier: Soldier {
override init() {
super.init()
self.setShoesActionBehavior(handleShoesActionBehavior: TieWithHands())
}
override func display() {
print("劉時鎮報到😎")
}
}
```
----
### 導演: 仲基哥這場景該你出場
```swift=
var 宋仲基:Soldier = TitleRoleSoldier()
宋仲基.performShoesAction()
```
----
### 導演: 仲基哥這次場景你要換TakeOffShoesWithHands()行為, 然後遇見伯母的劇情
```swift=
宋仲基.setShoesActionBehavior(handleShoesActionBehavior: TakeOffShoesWithHands())
宋仲基.performShoesAction()
```
---
![Winter Soldier](http://nerdist.com/wp-content/uploads/2014/03/winter-soldier-concept-art-6.jpg)
# Winter Soldier
----
```swift=
class WinterSoldier: Soldier {
override init() {
super.init()
self.setShoesActionBehavior(handleShoesActionBehavior: TieWithHands())
}
override func display() {
print("The thing is, you don't have to. I'm with you till the end of the line")
}
}
宋仲基 = WinterSoldier()
宋仲基.performShoesAction()
```
----
{%youtube -7OACKizgVU %}
> The thing is, you don't have to. I'm with you till the end of the line
----
# 廣告
![E](https://scontent.ftpe1-1.fna.fbcdn.net/v/t1.0-9/14457366_10209197139040014_2288973482208625740_n.jpg?oh=a1c479dc669d8c0af0d741499ae3e559&oe=588F3B0A)
----
### 現在給妳當編劇機會自訂SweetBehavioral需求
```swift=
class SomeSweetAction: SweetBehavioral {
func shoesAction() {
print("函式名稱設不好, 反正sweetAction, 自訂SweetBehavioral需求po在issue區, Thank you :D")
}
}
宋仲基.setShoesActionBehavior(handleShoesActionBehavior: SomeSweetAction())
宋仲基.performShoesAction()
```
<!-- .element: class="fragment" data-fragment-index="1" -->
----
## 策略模式
### 定義了演算法(就是上面仲基的各種sweet行為),分別封裝了起來,讓他們之間可以相互替換,此模式讓演算法的變化獨立於使用演算法的函式,演算法功能分開好以後換來換去
----
## 策略模式 VS Protocol Extensions Pattern
---
# 學習法則
## 作業
### 如果你第一次學到這知識點, 那就馬上寫作業吧 :D
----
# 作業
1. 馬上Star 按下去 (50分)
2. 馬上Fork 改寫 (25分)
3. 有問題提issue (25分) https://github.com/helloworldsmart/DesignPatterns-Swift
----
## 徵戰友
https://cn.udacity.com/
---
# 參考資料(待更新)
- [google大神](https://www.google.com.tw/)
- [OOD-Principles-In-Swift](https://github.com/ochococo/OOD-Principles-In-Swift)
---
## Thank you!歡迎指教
{"metaMigratedAt":"2023-06-14T12:00:54.999Z","metaMigratedFrom":"Content","title":"Design Patterns - Swift","breaks":true,"contributors":"[]"}