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 class="current-tab" 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 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">
@@ -565,53 +361,29 @@ function hide_canvas() {
<p>源码示例:</p>
<pre><code class="language-python">from copy import copy, deepcopy
class Person:
&quot;&quot;
def __init__(self, name, age):
self.__name = name
self.__age = age
def showMyself(self):
print(&quot;我是&quot; + self.__name + &quot;,年龄&quot; + str(self.__age) + &quot;.&quot;)
def coding(self):
print(&quot;我是码农我在Coding改变世界...&quot;)
def reading(self):
print(&quot;阅读使我快乐!知识使我成长!如饥似渴地阅读是生活的一部分...&quot;)
def fallInLove(self):
print(&quot;春风吹,月亮明,花前月下好相约...&quot;)
def clone(self):
return copy(self)
</code></pre>
<p>测试代码:</p>
@@ -623,25 +395,16 @@ class Person:
tony.showMyself()
tony.coding()
tony1 = tony.clone()
tony1.showMyself()
tony1.reading()
tony2 = tony.clone()
tony2.showMyself()
tony2.fallInLove()
</code></pre>
<p>输出结果:</p>
@@ -657,9 +420,6 @@ class Person:
我是Tony,年龄:26
春风吹,月亮明,花前月下好相约...
</code></pre>
<p>在上面的例子中Tony 克隆出了两个自己 tony1 和 tony2因为是克隆出来的所有姓名和年龄都一样这样 Tony 就可以同时去敲代码、读书和约会了。</p>
@@ -685,9 +445,6 @@ class Person:
<pre><code class="language-python">class Person:
&quot;&quot;
def __init__(self, name, age):
self.__name = name
@@ -695,9 +452,6 @@ class Person:
self.__age = age
self.__petList = []
def showMyself(self):
print(&quot;我是&quot; + self.__name + &quot;,年龄&quot; + str(self.__age) + &quot;. 我养了这些宠物:&quot;)
@@ -707,27 +461,15 @@ class Person:
print(pet + &quot;\t&quot;, end=&quot;&quot;)
print()
def addPet(self, pet):
self.__petList.append(pet)
def clone(self):
return copy(self)
def deepClone(self):
return deepcopy(self)
</code></pre>
<p>测试代码:</p>
@@ -741,9 +483,6 @@ class Person:
print(&quot;父本tony&quot;, end=&quot;&quot;)
tony.showMyself()
tony1 = tony.deepClone()
tony1.addPet(&quot;小猫Amy&quot;)
@@ -755,9 +494,6 @@ class Person:
print(&quot;父本tony&quot;, end=&quot;&quot;)
tony.showMyself()
tony2 = tony.clone()
tony2.addPet(&quot;小兔Ricky&quot;)
@@ -769,9 +505,6 @@ class Person:
print(&quot;父本tony&quot;, end=&quot;&quot;)
tony.showMyself()
</code></pre>
<p>输出结果:</p>
@@ -795,9 +528,6 @@ class Person:
父本tony我是Tony,年龄26. 我养了这些宠物:
小狗Coco 小兔Ricky
</code></pre>
<p>在上面这个例子中,我们看到“副本 tony1”是通过深拷贝的方式创建的我们对 tony1 对象增加宠物,不会影响 tony 对象。而副本 tony2 是通过浅拷贝的方式创建的,我们对 tony2 对象增加宠物时tony 对象也更着改变。这是因为 Person 类<code>__petList</code>成员是一个可变的引用类型,<strong>浅拷贝只拷贝引用类型对象的指针(指向),而不拷贝引用类型对象指向的值;深拷贝到同时拷贝引用类型对象及其指向的值。</strong></p>
@@ -827,9 +557,6 @@ class Person:
print(&quot;list:&quot;, list)
print(&quot;list1:&quot;, list1)
</code></pre>
<p>结果:</p>
@@ -849,9 +576,6 @@ list1: [1, 2, 3]
list: [1, 2, 3, 4]
list1: [1, 2, 3, 4]
</code></pre>
<p>通过 Clone 的方式创建对象时,浅拷贝往往是很危险的,因为一个对象的改变另一个对象也同时改变。深拷贝会对一个对象的发生进行完全拷贝,这样两个对象之间就不会相互影响了,你改你的,我改我的。</p>
@@ -865,31 +589,19 @@ list1: [1, 2, 3, 4]
<p>克隆模式非常简单,我们可以对它进行进一步的重构和优化,抽象出克隆模式的框架模型。</p>
<pre><code class="language-python">from copy import copy, deepcopy
class Clone:
&quot;克隆的基类&quot;
def clone(self):
&quot;浅拷贝的方式克隆对象&quot;
return copy(self)
def deepClone(self):
&quot;深拷贝的方式克隆对象&quot;
return deepcopy(self)
</code></pre>
<h4>类图</h4>
@@ -905,9 +617,6 @@ class Clone:
<pre><code class="language-python">class Person(Clone):
&quot;&quot;
def __init__(self, name, age):
self.__name = name
@@ -915,9 +624,6 @@ class Clone:
self.__age = age
self.__petList = []
def showMyself(self):
print(&quot;我是&quot; + self.__name + &quot;,年龄&quot; + str(self.__age) + &quot;. 我养了这些宠物:&quot;)
@@ -927,33 +633,18 @@ class Clone:
print(pet + &quot;\t&quot;, end=&quot;&quot;)
print()
def addPet(self, pet):
self.__petList.append(pet)
def coding(self):
print(&quot;我是码农我在Coding改变世界...&quot;)
def reading(self):
print(&quot;阅读使我快乐!知识使我成长!如饥似渴地阅读是生活的一部分...&quot;)
def fallInLove(self):
print(&quot;春风吹,月亮明,花前月下好相约...&quot;)
</code></pre>
<p>测试代码不用变(同 testProtoType2()),自己跑一下,会发现输出结果和之前的是一样的。</p>
@@ -1005,9 +696,6 @@ class Clone:
</div>
</div>
</div>
</div>
@@ -1015,9 +703,6 @@ class Clone:
</div>
</div>
<a class="off-canvas-overlay" onclick="hide_canvas()"></a>
</div>
@@ -1033,17 +718,11 @@ class Clone:
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-NPSEEVD756');
@@ -1069,9 +748,6 @@ class Clone:
setCookie("lastPath", path)
}
function setCookie(cname, cvalue) {
var d = new Date();
@@ -1083,9 +759,6 @@ class Clone:
document.cookie = cname + "=" + cvalue + "; " + expires + ";path = /";
}
function getCookie(cname) {
var name = cname + "=";
@@ -1103,12 +776,6 @@ class Clone:
return "";
}
</script>
</html>