mirror of
https://github.com/zhwei820/learn.lianglianglee.com.git
synced 2025-11-15 21:53:50 +08:00
u
This commit is contained in:
@@ -25,13 +25,7 @@
|
||||
<meta name="generator" content="Hexo 4.2.0">
|
||||
|
||||
</head>
|
||||
|
||||
|
||||
|
||||
<body>
|
||||
|
||||
|
||||
|
||||
<div class="book-container">
|
||||
|
||||
<div class="book-sidebar">
|
||||
@@ -55,373 +49,190 @@
|
||||
<li><a href="/" class="current-tab">首页</a></li>
|
||||
|
||||
</ul>
|
||||
|
||||
|
||||
|
||||
<ul class="uncollapsible">
|
||||
|
||||
<li><a href="../">上一级</a></li>
|
||||
|
||||
</ul>
|
||||
|
||||
|
||||
|
||||
<ul class="uncollapsible">
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/00 生活中的设计模式:启程之前,请不要错过我.md.html">00 生活中的设计模式:启程之前,请不要错过我.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/01 监听模式:坑爹的热水器.md.html">01 监听模式:坑爹的热水器.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/02 适配模式:身高不够鞋来凑.md.html">02 适配模式:身高不够鞋来凑.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/03 状态模式:人与水的三态.md.html">03 状态模式:人与水的三态.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/04 单例模式:你是我生命的唯一.md.html">04 单例模式:你是我生命的唯一.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/05 职责模式:我的假条去哪了.md.html">05 职责模式:我的假条去哪了.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/06 中介模式:找房子问中介.md.html">06 中介模式:找房子问中介.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/07 代理模式:帮我拿一下快递.md.html">07 代理模式:帮我拿一下快递.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/08 装饰模式:你想怎么穿就怎么穿.md.html">08 装饰模式:你想怎么穿就怎么穿.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/09 工厂模式:你要拿铁还是摩卡.md.html">09 工厂模式:你要拿铁还是摩卡.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/10 迭代模式:下一个就是你了.md.html">10 迭代模式:下一个就是你了.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/11 组合模式:自己组装电脑.md.html">11 组合模式:自己组装电脑.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/12 构建模式:想要车还是庄园.md.html">12 构建模式:想要车还是庄园.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/13 克隆模式:给你一个分身术.md.html">13 克隆模式:给你一个分身术.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
<a class="current-tab" href="/专栏/白话设计模式 28 讲(完)/14 策略模式:怎么来不重要,人到就行.md.html">14 策略模式:怎么来不重要,人到就行.md.html</a>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/15 命令模式:大闸蟹,走起!.md.html">15 命令模式:大闸蟹,走起!.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/16 备忘模式:好记性不如烂笔头.md.html">16 备忘模式:好记性不如烂笔头.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/17 享元模式:颜料很贵必须充分利用.md.html">17 享元模式:颜料很贵必须充分利用.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/18 外观模式:学妹别慌,学长帮你.md.html">18 外观模式:学妹别慌,学长帮你.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/19 访问模式:一千个读者一千个哈姆雷特.md.html">19 访问模式:一千个读者一千个哈姆雷特.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/20 生活中的设计模式:与经典设计模式的不解渊源.md.html">20 生活中的设计模式:与经典设计模式的不解渊源.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/21 生活中的设计模式:那些未完待续的设计模式.md.html">21 生活中的设计模式:那些未完待续的设计模式.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/22 深入解读过滤器模式:制作一杯鲜纯细腻的豆浆.md.html">22 深入解读过滤器模式:制作一杯鲜纯细腻的豆浆.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/23 深入解读对象池技术:共享让生活更便捷.md.html">23 深入解读对象池技术:共享让生活更便捷.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/24 深入解读回调机制:把你技能亮出来.md.html">24 深入解读回调机制:把你技能亮出来.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/25 谈谈我对设计模式的理解.md.html">25 谈谈我对设计模式的理解.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/26 谈谈我对设计原则的思考.md.html">26 谈谈我对设计原则的思考.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<a href="/专栏/白话设计模式 28 讲(完)/27 谈谈我对项目重构的看法.md.html">27 谈谈我对项目重构的看法.md.html</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<div class="sidebar-toggle" onclick="sidebar_toggle()" onmouseover="add_inner()" onmouseleave="remove_inner()">
|
||||
|
||||
<div class="sidebar-toggle-inner"></div>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<script>
|
||||
|
||||
function add_inner() {
|
||||
@@ -431,9 +242,6 @@
|
||||
inner.classList.add('show')
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function remove_inner() {
|
||||
|
||||
let inner = document.querySelector('.sidebar-toggle-inner')
|
||||
@@ -441,9 +249,6 @@
|
||||
inner.classList.remove('show')
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function sidebar_toggle() {
|
||||
|
||||
let sidebar_toggle = document.querySelector('.sidebar-toggle')
|
||||
@@ -473,9 +278,6 @@
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
function open_sidebar() {
|
||||
|
||||
let sidebar = document.querySelector('.book-sidebar')
|
||||
@@ -499,13 +301,7 @@ function hide_canvas() {
|
||||
overlay.classList.remove('show')
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
|
||||
|
||||
<div class="off-canvas-content">
|
||||
|
||||
<div class="columns">
|
||||
@@ -579,77 +375,41 @@ function hide_canvas() {
|
||||
<pre><code class="language-python">class IVehicle:
|
||||
|
||||
"交通工具的抽象类"
|
||||
|
||||
|
||||
|
||||
def running(self):
|
||||
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class SharedBicycle(IVehicle):
|
||||
|
||||
"共享单车"
|
||||
|
||||
|
||||
|
||||
def running(self):
|
||||
|
||||
print("骑共享单车(轻快便捷)", end='')
|
||||
|
||||
|
||||
|
||||
class ExpressBus(IVehicle):
|
||||
|
||||
"快速公交"
|
||||
|
||||
|
||||
|
||||
def running(self):
|
||||
|
||||
print("坐快速公交(经济绿色)", end='')
|
||||
|
||||
|
||||
|
||||
class Express(IVehicle):
|
||||
|
||||
"快车"
|
||||
|
||||
|
||||
|
||||
def running(self):
|
||||
|
||||
print("打快车(快速方便)", end='')
|
||||
|
||||
|
||||
|
||||
class Subway(IVehicle):
|
||||
|
||||
"地铁"
|
||||
|
||||
|
||||
|
||||
def running(self):
|
||||
|
||||
print("坐地铁(高效安全)", end='')
|
||||
|
||||
|
||||
|
||||
class Classmate:
|
||||
|
||||
"参加聚餐的同学"
|
||||
|
||||
|
||||
|
||||
def __init__(self, name, vechicle):
|
||||
|
||||
self.__name = name
|
||||
|
||||
self.__vechicle = vechicle
|
||||
|
||||
|
||||
|
||||
def attendTheDinner(self):
|
||||
|
||||
print(self.__name + " ", end='')
|
||||
@@ -657,9 +417,6 @@ class Classmate:
|
||||
self.__vechicle.running()
|
||||
|
||||
print(" 来参加聚餐!")
|
||||
|
||||
|
||||
|
||||
</code></pre>
|
||||
|
||||
<p>测试代码:</p>
|
||||
@@ -683,9 +440,6 @@ class Classmate:
|
||||
ruby = Classmate("Ruby", Express())
|
||||
|
||||
ruby.attendTheDinner()
|
||||
|
||||
|
||||
|
||||
</code></pre>
|
||||
|
||||
<p>输出结果:</p>
|
||||
@@ -697,9 +451,6 @@ Helen 坐地铁(高效安全) 来参加聚餐!
|
||||
Henry 坐快速公交(经济绿色) 来参加聚餐!
|
||||
|
||||
Ruby 打快车(快速方便) 来参加聚餐!
|
||||
|
||||
|
||||
|
||||
</code></pre>
|
||||
|
||||
<p>上面示例的类图如下:</p>
|
||||
@@ -729,9 +480,6 @@ Ruby 打快车(快速方便) 来参加聚餐!
|
||||
henry = Classmate("Henry", Express())
|
||||
|
||||
henry.attendTheDinner()
|
||||
|
||||
|
||||
|
||||
</code></pre>
|
||||
|
||||
<h3>策略模式的模型抽象</h3>
|
||||
@@ -795,9 +543,6 @@ henry.attendTheDinner()
|
||||
<pre><code class="language-python">class Person:
|
||||
|
||||
"人类"
|
||||
|
||||
|
||||
|
||||
def __init__(self, name, age, weight, height):
|
||||
|
||||
self.name = name
|
||||
@@ -807,65 +552,35 @@ henry.attendTheDinner()
|
||||
self.weight = weight
|
||||
|
||||
self.height = height
|
||||
|
||||
|
||||
|
||||
def showMysef(self):
|
||||
|
||||
print(self.name + " " + str(self.age) + " years old, " + str(self.weight) + "kg, " + str(self.height) + "m.")
|
||||
|
||||
|
||||
|
||||
class ICompare:
|
||||
|
||||
"比较算法"
|
||||
|
||||
|
||||
|
||||
def comparable(self, person1, person2):
|
||||
|
||||
"person1 > person2 返回值>0,person1 == person2 返回0, person1 < person2 返回值小于0"
|
||||
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class CompareByAge(ICompare):
|
||||
|
||||
"通过年龄排序"
|
||||
|
||||
|
||||
|
||||
def comparable(self, person1, person2):
|
||||
|
||||
return person1.age - person2.age
|
||||
|
||||
|
||||
|
||||
class CompareByHeight(ICompare):
|
||||
|
||||
"通过身高进行排序"
|
||||
|
||||
|
||||
|
||||
def comparable(self, person1, person2):
|
||||
|
||||
return person1.height - person2.height
|
||||
|
||||
|
||||
|
||||
class SortPerson:
|
||||
|
||||
"Person的排序类"
|
||||
|
||||
|
||||
|
||||
def __init__(self, compare):
|
||||
|
||||
self.__compare = compare
|
||||
|
||||
|
||||
|
||||
def sort(self, personList):
|
||||
|
||||
"排序算法,这里采用最简单的冒泡排序"
|
||||
@@ -887,9 +602,6 @@ class SortPerson:
|
||||
j += 1
|
||||
|
||||
i += 1
|
||||
|
||||
|
||||
|
||||
</code></pre>
|
||||
|
||||
<p><strong>测试代码:</strong></p>
|
||||
@@ -921,9 +633,6 @@ class SortPerson:
|
||||
person.showMysef()
|
||||
|
||||
print()
|
||||
|
||||
|
||||
|
||||
sorter1 = SortPerson(CompareByHeight())
|
||||
|
||||
sorter1.sort(personList)
|
||||
@@ -933,9 +642,6 @@ class SortPerson:
|
||||
for person in personList:
|
||||
|
||||
person.showMysef()
|
||||
|
||||
|
||||
|
||||
</code></pre>
|
||||
|
||||
<p><strong>输出结果:</strong></p>
|
||||
@@ -951,9 +657,6 @@ Eric 23 years old, 62.0kg, 1.78m.
|
||||
Jack 31 years old, 74.5kg, 1.8m.
|
||||
|
||||
Nick 54 years old, 44.5kg, 1.59m.
|
||||
|
||||
|
||||
|
||||
根据身高进行排序后的结果:
|
||||
|
||||
Tony 2 years old, 54.5kg, 0.82m.
|
||||
@@ -965,9 +668,6 @@ Helen 16 years old, 45.7kg, 1.6m.
|
||||
Eric 23 years old, 62.0kg, 1.78m.
|
||||
|
||||
Jack 31 years old, 74.5kg, 1.8m.
|
||||
|
||||
|
||||
|
||||
</code></pre>
|
||||
|
||||
<p>上面的代码可用类图表示如下:</p>
|
||||
@@ -977,15 +677,9 @@ Jack 31 years old, 74.5kg, 1.8m.
|
||||
<p>看到这,熟悉 Python 的读者肯定要吐槽了!Python 是一个简洁明了的语言,使用十几行代码就能解决的问题(如下面的实现代码),为什么要写上面这一大堆的东西。</p>
|
||||
|
||||
<pre><code class="language-python">from operator import itemgetter,attrgetter
|
||||
|
||||
|
||||
|
||||
def testPersonListInPython():
|
||||
|
||||
"用Python的方式对Person进行排序"
|
||||
|
||||
|
||||
|
||||
personList = [
|
||||
|
||||
Person("Tony", 2, 54.5, 0.82),
|
||||
@@ -999,9 +693,6 @@ def testPersonListInPython():
|
||||
Person("Helen", 16, 45.7, 1.60)
|
||||
|
||||
]
|
||||
|
||||
|
||||
|
||||
# 使用使用operator模块根据年龄进行排序
|
||||
|
||||
print("根据年龄进行排序后的结果:")
|
||||
@@ -1013,9 +704,6 @@ def testPersonListInPython():
|
||||
person.showMysef()
|
||||
|
||||
print()
|
||||
|
||||
|
||||
|
||||
print("根据身高进行排序后的结果:")
|
||||
|
||||
sortedPerons1 = sorted(personList, key=attrgetter('height'))
|
||||
@@ -1023,9 +711,6 @@ def testPersonListInPython():
|
||||
for person in sortedPerons1:
|
||||
|
||||
person.showMysef()
|
||||
|
||||
|
||||
|
||||
</code></pre>
|
||||
|
||||
<p>输出的结果和上面是一模一样的,这里不再赘述。</p>
|
||||
@@ -1045,9 +730,6 @@ def testPersonListInPython():
|
||||
<pre><code class="language-python">class CompareByHeightAndWeight(ICompare):
|
||||
|
||||
"根据身高和体重的综合情况来排序(身高和体重的权重分别是0.6和0.4)"
|
||||
|
||||
|
||||
|
||||
def comparable(self, person1, person2):
|
||||
|
||||
value1 = person1.height * 0.6 + person1.weight * 0.4
|
||||
@@ -1055,9 +737,6 @@ def testPersonListInPython():
|
||||
value2 = person2.height * 0.6 + person2.weight * 0.4
|
||||
|
||||
return value1 - value2
|
||||
|
||||
|
||||
|
||||
</code></pre>
|
||||
|
||||
<h3>应用场景</h3>
|
||||
@@ -1091,9 +770,6 @@ def testPersonListInPython():
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
@@ -1101,9 +777,6 @@ def testPersonListInPython():
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<a class="off-canvas-overlay" onclick="hide_canvas()"></a>
|
||||
|
||||
</div>
|
||||
@@ -1119,17 +792,11 @@ def testPersonListInPython():
|
||||
<script>
|
||||
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
|
||||
|
||||
|
||||
function gtag() {
|
||||
|
||||
dataLayer.push(arguments);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
gtag('js', new Date());
|
||||
|
||||
gtag('config', 'G-NPSEEVD756');
|
||||
@@ -1155,9 +822,6 @@ def testPersonListInPython():
|
||||
setCookie("lastPath", path)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function setCookie(cname, cvalue) {
|
||||
|
||||
var d = new Date();
|
||||
@@ -1169,9 +833,6 @@ def testPersonListInPython():
|
||||
document.cookie = cname + "=" + cvalue + "; " + expires + ";path = /";
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function getCookie(cname) {
|
||||
|
||||
var name = cname + "=";
|
||||
@@ -1189,12 +850,6 @@ def testPersonListInPython():
|
||||
return "";
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
|
||||
|
||||
</html>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user