This commit is contained in:
周伟
2022-05-11 19:04:14 +08:00
parent 9440ac7291
commit d9c5ffd627
826 changed files with 0 additions and 481675 deletions

View File

@@ -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 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 class="current-tab" 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">
@@ -567,9 +363,6 @@ function hide_canvas() {
<pre><code class="language-python">from abc import ABCMeta, abstractmethod
# 引入ABCMeta和abstractmethod来定义抽象类和抽象方法
class DesignPatternBook:
&quot;《从生活的角度解读设计模式》一书&quot;
@@ -577,57 +370,30 @@ class DesignPatternBook:
def getName(self):
return &quot;《从生活的角度解读设计模式》&quot;
class Reader(metaclass=ABCMeta):
&quot;访问者,也就是读者&quot;
@abstractmethod
def read(self, book):
pass
class Engineer(Reader):
def read(self, book):
print(&quot;技术狗读&quot; + book.getName() + &quot;一书后的感受:能抓住模式的核心思想,深入浅出,很有见地!&quot;)
class ProductManager(Reader):
&quot;产品经理&quot;
def read(self, book):
print(&quot;产品经理读&quot; + book.getName() + &quot;一书后的感受:配图非常有趣,文章很有层次感!&quot;)
class OtherFriend(Reader):
&quot;IT圈外的朋友&quot;
def read(self, book):
print(&quot;IT圈外的朋友读&quot; + book.getName() + &quot;一书后的感受:技术的内容一脸蒙蔽,但故事很精彩,像是看小说或是故事集!&quot;)
</code></pre>
<p>测试代码:</p>
@@ -641,9 +407,6 @@ class OtherFriend(Reader):
for fan in fans:
fan.read(book)
</code></pre>
<p>输出结果:</p>
@@ -653,9 +416,6 @@ class OtherFriend(Reader):
产品经理读《从生活的角度解读设计模式》一书后的感受:配图非常有趣,文章很有层次感!
IT 圈外的朋友读《从生活的角度解读设计模式》一书后的感受:技术的内容一脸蒙蔽,但故事很精彩,像是看小说或是故事集!
</code></pre>
<h3>从剧情中思考访问模式</h3>
@@ -683,29 +443,17 @@ IT 圈外的朋友读《从生活的角度解读设计模式》一书后的感
<pre><code class="language-python">from abc import ABCMeta, abstractmethod
# 引入ABCMeta和abstractmethod来定义抽象类和抽象方法
class DataNode(metaclass=ABCMeta):
&quot;数据结构类&quot;
def accept(self, visitor):
&quot;接受访问者的访问&quot;
visitor.visit(self)
class Visitor(metaclass=ABCMeta):
&quot;访问者&quot;
@abstractmethod
def visit(self, data):
@@ -713,27 +461,15 @@ class Visitor(metaclass=ABCMeta):
&quot;对数据对象的访问操作&quot;
pass
class ObjectStructure:
&quot;数据结构的管理类,也是数据对象的一个容器,可遍历容器内的所有元素&quot;
def __init__(self):
self.__datas = []
def add(self, dataElement):
self.__datas.append(dataElement)
def action(self, visitor):
&quot;进行数据访问的操作&quot;
@@ -741,9 +477,6 @@ class ObjectStructure:
for data in self.__datas:
visitor.visit(data)
</code></pre>
<p>这里 Visitor 的访问方法只有一个 visit(),是因为 Python 不支持方法的重载。在一些强类型的语言(如 Java、C++)中,应该有多个方法,针对每一个 DataNode 子类定义一个重载方法。</p>
@@ -763,49 +496,25 @@ class ObjectStructure:
<pre><code class="language-python">class DesignPatternBook:
&quot;《从生活的角度解读设计模式》一书&quot;
def getName(self):
return &quot;《从生活的角度解读设计模式》&quot;
class Engineer(Visitor):
def visit(self, book):
print(&quot;技术狗读&quot; + book.getName() + &quot;一书后的感受:能抓住模式的核心思想,深入浅出,很有见地!&quot;)
class ProductManager(Visitor):
&quot;产品经理&quot;
def visit(self, book):
print(&quot;产品经理读&quot; + book.getName() + &quot;一书后的感受:配图非常有趣,文章很有层次感!&quot;)
class OtherFriend(Visitor):
&quot;IT圈外的朋友&quot;
def visit(self, book):
print(&quot;IT圈外的朋友读&quot; + book.getName() + &quot;一书后的感受:技术的内容一脸蒙蔽,但故事很精彩,像是看小说或是故事集!&quot;)
</code></pre>
<p>测试代码也得相应改动一下:</p>
@@ -823,9 +532,6 @@ class OtherFriend(Visitor):
objMgr.action(ProductManager())
objMgr.action(OtherFriend())
</code></pre>
<p>自己跑一下,会发现输出结果和之前的是一样的。</p>
@@ -889,9 +595,6 @@ class OtherFriend(Visitor):
<pre><code class="language-python">class Animal(DataNode):
&quot;动物类&quot;
def __init__(self, name, isMale, age, weight):
self.__name = name
@@ -901,75 +604,39 @@ class OtherFriend(Visitor):
self.__age = age
self.__weight = weight
def getName(self):
return self.__name
def isMale(self):
return self.__isMale
def getAge(self):
return self.__age
def getWeight(self):
return self.__weight
class Cat(Animal):
&quot;&quot;
def __init__(self, name, isMale, age, weight):
super().__init__(name, isMale, age, weight)
def speak(self):
print(&quot;miao~&quot;)
class Dog(Animal):
&quot;&quot;
def __init__(self, name, isMale, age, weight):
super().__init__( name, isMale, age, weight)
def speak(self):
print(&quot;wang~&quot;)
class GenderCounter(Visitor):
&quot;性别统计&quot;
def __init__(self):
self.__maleCat = 0
@@ -979,9 +646,6 @@ class GenderCounter(Visitor):
self.__maleDog = 0
self.__femalDog = 0
def visit(self, data):
if isinstance(data, Cat):
@@ -1007,21 +671,12 @@ class GenderCounter(Visitor):
else:
print(&quot;Not support this type&quot;)
def getInfo(self):
print(str(self.__maleCat) + &quot;只雄猫,&quot; + str(self.__femaleCat) + &quot;只雌猫,&quot;
+ str(self.__maleDog) + &quot;只雄狗,&quot; + str(self.__femalDog) + &quot;只雌狗。&quot;)
class WeightCounter(Visitor):
def __init__(self):
self.__catNum = 0
@@ -1031,9 +686,6 @@ class WeightCounter(Visitor):
self.__dogNum = 0
self.__dogWeight = 0
def visit(self, data):
if isinstance(data, Cat):
@@ -1051,29 +703,17 @@ class WeightCounter(Visitor):
else:
print(&quot;Not support this type&quot;)
def getInfo(self):
print(&quot;猫的平均体重是:%0.2fkg 狗的平均体重是:%0.2fkg&quot; %
((self.__catWeight / self.__catNum),(self.__dogWeight / self.__dogNum)))
class AgeCounter(Visitor):
def __init__(self):
self.__catMaxAge = 0
self.__dogMaxAge = 0
def visit(self, data):
if isinstance(data, Cat):
@@ -1091,15 +731,9 @@ class AgeCounter(Visitor):
else:
print(&quot;Not support this type&quot;)
def getInfo(self):
print(&quot;猫的最大年龄是:&quot; + str(self.__catMaxAge) + &quot; 狗的最大年龄是:&quot; + str(self.__dogMaxAge))
</code></pre>
<p><strong>测试代码:</strong></p>
@@ -1129,9 +763,6 @@ class AgeCounter(Visitor):
genderCounter.getInfo()
print()
weightCounter = WeightCounter()
animals.action(weightCounter)
@@ -1139,33 +770,18 @@ class AgeCounter(Visitor):
weightCounter.getInfo()
print()
ageCounter = AgeCounter()
animals.action(ageCounter)
ageCounter.getInfo()
</code></pre>
<p><strong>输出结果:</strong></p>
<pre><code>1只雄猫2只雌猫2只雄狗2只雌狗。
猫的平均体重是4.07kg 狗的平均体重是26.50kg
猫的最大年龄是1.2 狗的最大年龄是3
</code></pre>
<p>使用访问模式后,代码结构是不是清爽了很多!</p>
@@ -1201,9 +817,6 @@ class AgeCounter(Visitor):
</div>
</div>
</div>
</div>
@@ -1211,9 +824,6 @@ class AgeCounter(Visitor):
</div>
</div>
<a class="off-canvas-overlay" onclick="hide_canvas()"></a>
</div>
@@ -1229,17 +839,11 @@ class AgeCounter(Visitor):
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-NPSEEVD756');
@@ -1265,9 +869,6 @@ class AgeCounter(Visitor):
setCookie("lastPath", path)
}
function setCookie(cname, cvalue) {
var d = new Date();
@@ -1279,9 +880,6 @@ class AgeCounter(Visitor):
document.cookie = cname + "=" + cvalue + "; " + expires + ";path = /";
}
function getCookie(cname) {
var name = cname + "=";
@@ -1299,12 +897,6 @@ class AgeCounter(Visitor):
return "";
}
</script>
</html>