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 class="current-tab" 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 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">
@@ -575,9 +371,6 @@ function hide_canvas() {
__instance = None
__isFirstInit = False
def __new__(cls, name):
if not cls.__instance:
@@ -585,9 +378,6 @@ function hide_canvas() {
MyBeautifulGril.__instance = super().__new__(cls)
return cls.__instance
def __init__(self, name):
if not self.__isFirstInit:
@@ -601,15 +391,9 @@ function hide_canvas() {
else:
print(&quot;遇见&quot; + name + &quot;,我置若罔闻!&quot;)
def showMyHeart(self):
print(self.__name + &quot;就我心中的唯一!&quot;)
</code></pre>
<p>测试代码:</p>
@@ -625,9 +409,6 @@ function hide_canvas() {
kimi.showMyHeart()
print(&quot;id(jenny):&quot;, id(jenny), &quot; id(kimi):&quot;, id(kimi))
</code></pre>
<p>输出结果:</p>
@@ -641,9 +422,6 @@ Jenny就我心中的唯一
Jenny就我心中的唯一
id(jenny): 47127888 id(kimi): 47127888
</code></pre>
<p>看到了没,一旦你初次选定了 Jenny不管换几个女人你心中念叨的还是 Jenny这才是真爱啊哈哈……</p>
@@ -683,9 +461,6 @@ id(jenny): 47127888 id(kimi): 47127888
__instance = None
__isFirstInit = False
def __new__(cls, name):
if not cls.__instance:
@@ -693,9 +468,6 @@ id(jenny): 47127888 id(kimi): 47127888
Singleton1.__instance = super().__new__(cls)
return cls.__instance
def __init__(self, name):
if not self.__isFirstInit:
@@ -703,15 +475,9 @@ id(jenny): 47127888 id(kimi): 47127888
self.__name = name
Singleton1.__isFirstInit = True
def getName(self):
return self.__name
# Test
tony = Singleton1(&quot;Tony&quot;)
@@ -723,9 +489,6 @@ print(tony.getName(), karry.getName())
print(&quot;id(tony):&quot;, id(tony), &quot;id(karry):&quot;, id(karry))
print(&quot;tony == karry:&quot;, tony == karry)
</code></pre>
<p>输出结果:</p>
@@ -735,9 +498,6 @@ print(&quot;tony == karry:&quot;, tony == karry)
id(tony): 46050320 id(karry): 46050320
tony == karry: True
</code></pre>
<p>在 Python 3 的类中,<em>*new</em>* 负责对象的创建,而 <em>*init</em>* 负责对象的初始化;<em>*new</em>* 是一个类方法,而 <em>*init</em>* 是一个对象方法。</p>
@@ -757,21 +517,12 @@ tony == karry: True
public class Singleton {
private static Singleton instance = null;
private String name;
private Singleton(String name) {
this.name = name;
}
public static Singleton getInstance(String name) {
if (instance == null) {
@@ -785,9 +536,6 @@ public class Singleton {
}
}
</code></pre>
<p>Python 中 <em>*new</em>* 和 <em>*init</em>* 都是 public 的,所以我们需要通过重写 <em>*new</em>* 和 <em>*init</em>* 方法来改造对象的创建过来,从而实现单例模式。如果你要更详细地了解 Python 中 <em>*new</em>* 和 <em>*init</em>* 的原理和用法,请参见《<a href="https://blog.csdn.net/luoweifu/article/details/82732313">深入理解 Python 中的 <strong>new</strong><strong>init</strong></a>》。</p>
@@ -797,17 +545,11 @@ public class Singleton {
<pre><code class="language-python">class Singleton2(type):
&quot;&quot;&quot;单例实现方式二&quot;&quot;&quot;
def __init__(cls, what, bases=None, dict=None):
super().__init__(what, bases, dict)
cls._instance = None # 初始化全局变量cls._instance为None
def __call__(cls, *args, **kwargs):
# 控制对象的创建过程如果cls._instance为None则创建否则直接返回
@@ -817,27 +559,15 @@ public class Singleton {
cls._instance = super().__call__(*args, **kwargs)
return cls._instance
class CustomClass(metaclass=Singleton2):
&quot;&quot;&quot;用户自定义的类&quot;&quot;&quot;
def __init__(self, name):
self.__name = name
def getName(self):
return self.__name
tony = CustomClass(&quot;Tony&quot;)
karry = CustomClass(&quot;Karry&quot;)
@@ -847,9 +577,6 @@ print(tony.getName(), karry.getName())
print(&quot;id(tony):&quot;, id(tony), &quot;id(karry):&quot;, id(karry))
print(&quot;tony == karry:&quot;, tony == karry)
</code></pre>
<p>输出结果:</p>
@@ -859,9 +586,6 @@ print(&quot;tony == karry:&quot;, tony == karry)
id(tony): 50794608 id(karry): 50794608
tony == karry: True
</code></pre>
<p>在上面的代码中,我们定义了一个 metaclassSingleton2来控制对象的实例化过程。在定义自己的类时我们通过 class CustomClass(metaclass=Singleton2) 来显示地指定 metaclass 为 Singleton2。如果你还不太熟悉 metaclass想了解更多关于它的原理请参见《[附录 Python 中 metaclass 的原理](》。</p>
@@ -873,9 +597,6 @@ tony == karry: True
&quot;&quot;&quot;定义一个单例装饰器&quot;&quot;&quot;
instance = {}
def wrapperSingleton(*args, **kwargs):
if cls not in instance:
@@ -883,33 +604,18 @@ tony == karry: True
instance[cls] = cls(*args, **kwargs)
return instance[cls]
return wrapperSingleton
@singletonDecorator
class Singleton3:
&quot;&quot;&quot;使用单例装饰器修饰一个类&quot;&quot;&quot;
def __init__(self, name):
self.__name = name
def getName(self):
return self.__name
tony = Singleton3(&quot;Tony&quot;)
karry = Singleton3(&quot;Karry&quot;)
@@ -919,9 +625,6 @@ print(tony.getName(), karry.getName())
print(&quot;id(tony):&quot;, id(tony), &quot;id(karry):&quot;, id(karry))
print(&quot;tony == karry:&quot;, tony == karry)
</code></pre>
<p>输出结果:</p>
@@ -931,9 +634,6 @@ print(&quot;tony == karry:&quot;, tony == karry)
id(tony): 46206704 id(karry): 46206704
tony == karry: True
</code></pre>
<p>装饰器的实质就是对传进来的参数进行补充,可以在原有的类不做任何代码变动的前提下增加额外的功能,使用装饰器可以装饰多个类。用装饰器的方式来实现单例模式,通用性非常好,在实际项目中用的非常多。</p>
@@ -953,9 +653,6 @@ tony == karry: True
class MyBeautifulGril(object):
&quot;&quot;&quot;我的漂亮女神&quot;&quot;&quot;
def __init__(self, name):
self.__name = name
@@ -967,15 +664,9 @@ class MyBeautifulGril(object):
else:
print(&quot;遇见&quot; + name + &quot;,我置若罔闻!&quot;)
def showMyHeart(self):
print(self.__name + &quot;就我心中的唯一!&quot;)
</code></pre>
<p>输出结果:</p>
@@ -987,9 +678,6 @@ Jenny就我心中的唯一
Jenny就我心中的唯一
id(jenny): 58920752 id(kimi): 58920752
</code></pre>
<h3>应用场景</h3>
@@ -1021,9 +709,6 @@ id(jenny): 58920752 id(kimi): 58920752
</div>
</div>
</div>
</div>
@@ -1031,9 +716,6 @@ id(jenny): 58920752 id(kimi): 58920752
</div>
</div>
<a class="off-canvas-overlay" onclick="hide_canvas()"></a>
</div>
@@ -1049,17 +731,11 @@ id(jenny): 58920752 id(kimi): 58920752
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-NPSEEVD756');
@@ -1085,9 +761,6 @@ id(jenny): 58920752 id(kimi): 58920752
setCookie("lastPath", path)
}
function setCookie(cname, cvalue) {
var d = new Date();
@@ -1099,9 +772,6 @@ id(jenny): 58920752 id(kimi): 58920752
document.cookie = cname + "=" + cvalue + "; " + expires + ";path = /";
}
function getCookie(cname) {
var name = cname + "=";
@@ -1119,12 +789,6 @@ id(jenny): 58920752 id(kimi): 58920752
return "";
}
</script>
</html>