內容簡介
作為可復用面向對象軟體開發的基礎,設計模式集合了很多軟體開發領域專家的寶貴經驗和良好實踐,其重要性不言而喻。但是,學習設計模式的過程並不是一帆風順的,對於缺乏面向對象開發經驗的設計者尤其如此。就像在游泳池中不能學會海戰一樣,我們不應該指望理解幾個似是而非的例子就可以在實際開發中靈活運用設計模式。學習設計模式最好的方式就是在實際的開發中邊學邊練邊體會。本書利用Python 的標準GUI 工具包tkinter,通過可執行的示例對23 個設計模式逐個進行說明。這樣一方面可以使讀者了解真實的軟體開發工作中每個設計模式的運用場景和想要解決的問題;另一方面通過對這些問題的解決過程進行說明,讓讀者明白在編寫代碼時如何判斷使用設計模式的利弊,併合理運用設計模式。對設計模式感興趣而且希望隨學隨用的讀者通過本書可以快速跨越從理解到運用的門檻;希望學習Python GUI 編程的讀者可以將本書中的示例作為設計和開發的參考;使用Python 語言進行圖像分析、數據處理工作的讀者可以直接以本書中的示例為基礎,迅速構建自己的系統架構。
目錄
第1 章 示例軟體介紹 ··········································································.1
1.1 準備工作 ···········································································.1
1.1.1 原始碼下載 ································································.1
1.1.2 安裝開發環境 ·····························································.1
1.2 啟動 ·················································································.1
1.3 功能說明 ···········································································.2
1.4 功能概要 ···········································································.3
1.4.1 編輯 ·········································································.3
1.4.2 邏輯調試 ···································································.6
1.4.3 檔案管理 ···································································.6
1.4.4 代碼生成 ···································································.6
第2 章 創建型模式 ·············································································.7
2.1
抽象工廠模式 ·····································································.7
2.1.1 問題說明 ···································································.7
2.1.2 模式結構 ···································································.8
2.1.3 示例代碼 ···································································.9
2.1.4 實戰運用 ··································································.12
2.1.5 效果討論 ··································································.19
2.1.6 提示和技巧 ·······························································.19
2.2 工廠方法模式 ····································································.19
2.2.1 問題說明 ··································································.19
2.2.2 模式結構 ··································································.21
2.2.3 示例代碼 ··································································.22
2.2.4 實戰運用 ··································································.24
2.2.5 效果討論 ··································································.26
2.2.6 提示和技巧 ·······························································.26
2.3 原型模式 ··········································································.27
2.3.1 問題說明 ··································································.27
2.3.2 模式結構 ··································································.28
2.3.3 示例代碼 ··································································.29
2.3.4 實戰運用 ··································································.30
2.3.5 效果討論 ··································································.34
2.3.6 提示和技巧 ·······························································.34
2.4 生成器模式 ·······································································.35
2.4.1 問題說明 ··································································.35
2.4.2 模式結構 ··································································.37
2.4.3 示例代碼 ··································································.38
2.4.4 實戰運用 ··································································.42
2.4.5 效果討論 ··································································.43
2.4.6 提示和技巧 ·······························································.44
2.5 單例模式 ··········································································.44
2.5.1 問題說明 ··································································.44
2.5.2 模式結構 ··································································.45
2.5.3 示例代碼 ··································································.46
2.5.4 實戰運用 ··································································.47
2.5.5 效果討論 ··································································.50
2.5.6 提示和技巧 ·······························································.50
2.6 創建型模式小結 ·································································.50
第3 章 結構型模式 ············································································.52
3.1 組合模式 ··········································································.52
3.1.1 問題說明 ··································································.52
3.1.2 模式結構 ··································································.53
3.1.3 示例代碼 ··································································.54
3.1.4 實戰運用 ··································································.57
3.1.5 效果討論 ··································································.60
3.1.6 提示和技巧 ·······························································.61
3.2 適配器模式 ·······································································.61
3.2.1 問題說明 ··································································.61
3.2.2 模式結構 ··································································.64
3.2.3 示例代碼 ··································································.64
3.2.4 實戰運用 ··································································.66
3.2.5 效果討論 ··································································.69
3.2.6 提示和技巧 ·······························································.69
3.3 橋接模式 ··········································································.70
3.3.1 問題說明 ··································································.70
3.3.2 模式構造 ··································································.72
3.3.3 示例代碼 ··································································.73
3.3.4 實戰運用 ··································································.76
3.3.5 效果討論 ··································································.79
3.3.6 提示和技巧 ·······························································.80
3.4 裝飾器模式 ·······································································.80
3.4.1 問題說明 ··································································.80
3.4.2 模式結構 ··································································.81
3.4.3 示例代碼 ··································································.82
3.4.4 實戰運用 ··································································.84
3.4.5 效果討論 ··································································.87
3.4.6 提示和技巧 ·······························································.88
3.5 代理模式 ··········································································.88
3.5.1 問題說明 ··································································.88
3.5.2 模式結構 ··································································.90
3.5.3 示例代碼 ··································································.91
3.5.4 實戰運用 ··································································.94
3.5.5 效果討論 ··································································.96
3.5.6 提示和技巧 ·······························································.96
3.6 享元模式 ··········································································.96
3.6.1 問題說明 ··································································.96
3.6.2 模式結構 ··································································.97
3.6.3 示例代碼 ··································································.98
3.6.4 實戰運用 ································································.100
3.6.5 效果討論 ································································.104
3.6.6 提示和技巧 ·····························································.104
3.7 外觀模式 ········································································.104
3.7.1 問題說明 ································································.104
3.7.2 模式結構 ································································.106
3.7.3 實戰運用 ································································.106
3.7.4 效果討論 ································································.109
3.7.5 提示和技巧 ·····························································.110
3.8 結構型模式小結 ·······························································.110
第4 章 行為模式 ·············································································.111
4.1 職責鏈模式 ·····································································.111
4.1.1 問題說明 ································································.111
4.1.2 模式結構 ································································.113
4.1.3 示例代碼 ································································.113
4.1.4 實戰運用 ································································.115
4.1.5 效果討論 ································································.119
4.1.6 提示和技巧 ·····························································.119
4.2 觀察者模式 ·····································································.119
4.2.1 問題說明 ································································.119
4.2.2 模式結構 ································································.120
4.2.3 示例代碼 ································································.121
4.2.4 實戰運用 ································································.123
4.2.5 效果討論 ································································.127
4.2.6 提示和技巧 ·····························································.127
4.3 中介者模式 ·····································································.128
4.3.1 問題說明 ································································.128
4.3.2 模式結構 ································································.129
4.3.3 示例代碼 ································································.130
4.3.4 實戰運用 ································································.132
4.3.5 效果討論 ································································.133
4.3.6 提示和技巧 ·····························································.134
4.4 策略模式 ········································································.134
4.4.1 問題說明 ································································.134
4.4.2 模式結構 ································································.136
4.4.3 示例代碼 ································································.137
4.4.4 實戰運用 ································································.139
4.4.5 效果討論 ································································.144
4.4.6 提示和技巧 ·····························································.145
4.5 命令模式 ········································································.145
4.5.1 問題說明 ································································.145
4.5.2 模式結構 ································································.146
4.5.3 示例代碼 ································································.148
4.5.4 實戰運用 ································································.150
4.5.5 效果討論 ································································.154
4.5.6 提示和技巧 ·····························································.154
4.6 備忘錄模式 ·····································································.155
4.6.1 問題說明 ································································.155
4.6.2 模式結構 ································································.156
4.6.3 示例代碼 ································································.157
4.6.4 實戰運用 ································································.159
4.6.5 效果討論 ································································.161
4.6.6 提示和技巧 ·····························································.161
4.7 模板方法模式 ··································································.162
4.7.1 問題說明 ································································.162
4.7.2 模式結構 ································································.163
4.7.3 示例代碼 ································································.164
4.7.4 實戰運用 ································································.168
4.7.5 效果討論 ································································.170
4.7.6 提示和技巧 ·····························································.170
4.8
疊代器模式 ·····································································.171
4.8.1 問題說明 ································································.171
4.8.2 模式結構 ································································.172
4.8.3 示例代碼 ································································.173
4.8.4 實戰運用 ································································.176
4.8.5 效果討論 ································································.179
4.8.6 提示和技巧 ·····························································.179
4.9 訪問者模式 ·····································································.180
4.9.1 問題說明 ································································.180
4.9.2 模式結構 ································································.182
4.9.3 示例代碼 ································································.183
4.9.4 實戰運用 ································································.187
4.9.5 效果討論 ································································.191
4.9.6 提示和技巧 ·····························································.192
4.10 狀態模式 ·······································································.193
4.10.1 問題說明 ······························································.193
4.10.2 模式結構 ······························································.194
4.10.3 示例代碼 ······························································.195
4.10.4 實戰運用 ······························································.203
4.10.5 效果討論 ······························································.206
4.10.6 提示和技巧 ···························································.206
4.11 解釋器模式 ····································································.206
4.11.1 問題說明 ······························································.206
4.11.2 模式結構 ······························································.207
4.11.3 示例代碼 ······························································.208
4.11.4 實戰運用 ······························································.214
4.11.5 效果討論 ······························································.219
4.11.6 提示和技巧 ···························································.220
4.12 行為模式小結 ·································································.220,
第1 章 示例軟體介紹 ··········································································.1
1.1 準備工作 ···········································································.1
1.1.1 原始碼下載 ································································.1
1.1.2 安裝開發環境 ·····························································.1
1.2 啟動 ·················································································.1
1.3 功能說明 ···········································································.2
1.4 功能概要 ···········································································.3
1.4.1 編輯 ·········································································.3
1.4.2 邏輯調試 ···································································.6
1.4.3 檔案管理 ···································································.6
1.4.4 代碼生成 ···································································.6
第2 章 創建型模式 ·············································································.7
2.1
抽象工廠模式 ·····································································.7
2.1.1 問題說明 ···································································.7
2.1.2 模式結構 ···································································.8
2.1.3 示例代碼 ···································································.9
2.1.4 實戰運用 ··································································.12
2.1.5 效果討論 ··································································.19
2.1.6 提示和技巧 ·······························································.19
2.2 工廠方法模式 ····································································.19
2.2.1 問題說明 ··································································.19
2.2.2 模式結構 ··································································.21
2.2.3 示例代碼 ··································································.22
2.2.4 實戰運用 ··································································.24
2.2.5 效果討論 ··································································.26
2.2.6 提示和技巧 ·······························································.26
2.3 原型模式 ··········································································.27
2.3.1 問題說明 ··································································.27
2.3.2 模式結構 ··································································.28
2.3.3 示例代碼 ··································································.29
2.3.4 實戰運用 ··································································.30
2.3.5 效果討論 ··································································.34
2.3.6 提示和技巧 ·······························································.34
2.4 生成器模式 ·······································································.35
2.4.1 問題說明 ··································································.35
2.4.2 模式結構 ··································································.37
2.4.3 示例代碼 ··································································.38
2.4.4 實戰運用 ··································································.42
2.4.5 效果討論 ··································································.43
2.4.6 提示和技巧 ·······························································.44
2.5 單例模式 ··········································································.44
2.5.1 問題說明 ··································································.44
2.5.2 模式結構 ··································································.45
2.5.3 示例代碼 ··································································.46
2.5.4 實戰運用 ··································································.47
2.5.5 效果討論 ··································································.50
2.5.6 提示和技巧 ·······························································.50
2.6 創建型模式小結 ·································································.50
第3 章 結構型模式 ············································································.52
3.1 組合模式 ··········································································.52
3.1.1 問題說明 ··································································.52
3.1.2 模式結構 ··································································.53
3.1.3 示例代碼 ··································································.54
3.1.4 實戰運用 ··································································.57
3.1.5 效果討論 ··································································.60
3.1.6 提示和技巧 ·······························································.61
3.2 適配器模式 ·······································································.61
3.2.1 問題說明 ··································································.61
3.2.2 模式結構 ··································································.64
3.2.3 示例代碼 ··································································.64
3.2.4 實戰運用 ··································································.66
3.2.5 效果討論 ··································································.69
3.2.6 提示和技巧 ·······························································.69
3.3 橋接模式 ··········································································.70
3.3.1 問題說明 ··································································.70
3.3.2 模式構造 ··································································.72
3.3.3 示例代碼 ··································································.73
3.3.4 實戰運用 ··································································.76
3.3.5 效果討論 ··································································.79
3.3.6 提示和技巧 ·······························································.80
3.4 裝飾器模式 ·······································································.80
3.4.1 問題說明 ··································································.80
3.4.2 模式結構 ··································································.81
3.4.3 示例代碼 ··································································.82
3.4.4 實戰運用 ··································································.84
3.4.5 效果討論 ··································································.87
3.4.6 提示和技巧 ·······························································.88
3.5 代理模式 ··········································································.88
3.5.1 問題說明 ··································································.88
3.5.2 模式結構 ··································································.90
3.5.3 示例代碼 ··································································.91
3.5.4 實戰運用 ··································································.94
3.5.5 效果討論 ··································································.96
3.5.6 提示和技巧 ·······························································.96
3.6 享元模式 ··········································································.96
3.6.1 問題說明 ··································································.96
3.6.2 模式結構 ··································································.97
3.6.3 示例代碼 ··································································.98
3.6.4 實戰運用 ································································.100
3.6.5 效果討論 ································································.104
3.6.6 提示和技巧 ·····························································.104
3.7 外觀模式 ········································································.104
3.7.1 問題說明 ································································.104
3.7.2 模式結構 ································································.106
3.7.3 實戰運用 ································································.106
3.7.4 效果討論 ································································.109
3.7.5 提示和技巧 ·····························································.110
3.8 結構型模式小結 ·······························································.110
第4 章 行為模式 ·············································································.111
4.1 職責鏈模式 ·····································································.111
4.1.1 問題說明 ································································.111
4.1.2 模式結構 ································································.113
4.1.3 示例代碼 ································································.113
4.1.4 實戰運用 ································································.115
4.1.5 效果討論 ································································.119
4.1.6 提示和技巧 ·····························································.119
4.2 觀察者模式 ·····································································.119
4.2.1 問題說明 ································································.119
4.2.2 模式結構 ································································.120
4.2.3 示例代碼 ································································.121
4.2.4 實戰運用 ································································.123
4.2.5 效果討論 ································································.127
4.2.6 提示和技巧 ·····························································.127
4.3 中介者模式 ·····································································.128
4.3.1 問題說明 ································································.128
4.3.2 模式結構 ································································.129
4.3.3 示例代碼 ································································.130
4.3.4 實戰運用 ································································.132
4.3.5 效果討論 ································································.133
4.3.6 提示和技巧 ·····························································.134
4.4 策略模式 ········································································.134
4.4.1 問題說明 ································································.134
4.4.2 模式結構 ································································.136
4.4.3 示例代碼 ································································.137
4.4.4 實戰運用 ································································.139
4.4.5 效果討論 ································································.144
4.4.6 提示和技巧 ·····························································.145
4.5 命令模式 ········································································.145
4.5.1 問題說明 ································································.145
4.5.2 模式結構 ································································.146
4.5.3 示例代碼 ································································.148
4.5.4 實戰運用 ································································.150
4.5.5 效果討論 ································································.154
4.5.6 提示和技巧 ·····························································.154
4.6 備忘錄模式 ·····································································.155
4.6.1 問題說明 ································································.155
4.6.2 模式結構 ································································.156
4.6.3 示例代碼 ································································.157
4.6.4 實戰運用 ································································.159
4.6.5 效果討論 ································································.161
4.6.6 提示和技巧 ·····························································.161
4.7 模板方法模式 ··································································.162
4.7.1 問題說明 ································································.162
4.7.2 模式結構 ································································.163
4.7.3 示例代碼 ································································.164
4.7.4 實戰運用 ································································.168
4.7.5 效果討論 ································································.170
4.7.6 提示和技巧 ·····························································.170
4.8
疊代器模式 ·····································································.171
4.8.1 問題說明 ································································.171
4.8.2 模式結構 ································································.172
4.8.3 示例代碼 ································································.173
4.8.4 實戰運用 ································································.176
4.8.5 效果討論 ································································.179
4.8.6 提示和技巧 ·····························································.179
4.9 訪問者模式 ·····································································.180
4.9.1 問題說明 ································································.180
4.9.2 模式結構 ································································.182
4.9.3 示例代碼 ································································.183
4.9.4 實戰運用 ································································.187
4.9.5 效果討論 ································································.191
4.9.6 提示和技巧 ·····························································.192
4.10 狀態模式 ·······································································.193
4.10.1 問題說明 ······························································.193
4.10.2 模式結構 ······························································.194
4.10.3 示例代碼 ······························································.195
4.10.4 實戰運用 ······························································.203
4.10.5 效果討論 ······························································.206
4.10.6 提示和技巧 ···························································.206
4.11 解釋器模式 ····································································.206
4.11.1 問題說明 ······························································.206
4.11.2 模式結構 ······························································.207
4.11.3 示例代碼 ······························································.208
4.11.4 實戰運用 ······························································.214
4.11.5 效果討論 ······························································.219
4.11.6 提示和技巧 ···························································.220
4.12 行為模式小結 ·································································.220