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 class="current-tab" 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">
|
||||
@@ -573,221 +369,122 @@ function hide_canvas() {
|
||||
<pre><code class="language-python">class Person:
|
||||
|
||||
"人"
|
||||
|
||||
|
||||
|
||||
def __init__(self, name):
|
||||
|
||||
self.__name = name
|
||||
|
||||
|
||||
|
||||
def getName(self):
|
||||
|
||||
return self.__name
|
||||
|
||||
|
||||
|
||||
def wear(self):
|
||||
|
||||
print("我的着装是:")
|
||||
|
||||
|
||||
|
||||
class Engineer(Person):
|
||||
|
||||
"工程师"
|
||||
|
||||
|
||||
|
||||
def __init__(self, name, skill):
|
||||
|
||||
super().__init__(name)
|
||||
|
||||
self.__skill = skill
|
||||
|
||||
|
||||
|
||||
def getSkill(self):
|
||||
|
||||
return self.__skill
|
||||
|
||||
|
||||
|
||||
def wear(self):
|
||||
|
||||
print("我是" + self.getSkill() + "工程师" + self.getName())
|
||||
|
||||
super().wear()
|
||||
|
||||
|
||||
|
||||
class Teacher(Person):
|
||||
|
||||
"教师"
|
||||
|
||||
|
||||
|
||||
def __init__(self, name, title):
|
||||
|
||||
super().__init__(name)
|
||||
|
||||
self.__title = title
|
||||
|
||||
|
||||
|
||||
def getTitle(self):
|
||||
|
||||
return self.__title
|
||||
|
||||
|
||||
|
||||
def wear(self):
|
||||
|
||||
print("我是" + self.getName() + self.getTitle())
|
||||
|
||||
super().wear()
|
||||
|
||||
|
||||
|
||||
class ClothingDecorator(Person):
|
||||
|
||||
"服装装饰器"
|
||||
|
||||
|
||||
|
||||
def __init__(self, person):
|
||||
|
||||
self._decorated = person
|
||||
|
||||
|
||||
|
||||
def wear(self):
|
||||
|
||||
self._decorated.wear()
|
||||
|
||||
|
||||
|
||||
class CasualPantDecorator(ClothingDecorator):
|
||||
|
||||
"休闲裤"
|
||||
|
||||
|
||||
|
||||
def __init__(self, person):
|
||||
|
||||
super().__init__(person)
|
||||
|
||||
|
||||
|
||||
def wear(self):
|
||||
|
||||
super().wear()
|
||||
|
||||
print("一条卡其色休闲裤")
|
||||
|
||||
|
||||
|
||||
class BeltDecorator(ClothingDecorator):
|
||||
|
||||
"腰带"
|
||||
|
||||
|
||||
|
||||
def __init__(self, person):
|
||||
|
||||
super().__init__(person)
|
||||
|
||||
|
||||
|
||||
def wear(self):
|
||||
|
||||
super().wear()
|
||||
|
||||
print("一条银色针扣头的黑色腰带")
|
||||
|
||||
|
||||
|
||||
class LeatherShoesDecorator(ClothingDecorator):
|
||||
|
||||
"皮鞋"
|
||||
|
||||
|
||||
|
||||
def __init__(self, person):
|
||||
|
||||
super().__init__(person)
|
||||
|
||||
|
||||
|
||||
def wear(self):
|
||||
|
||||
super().wear()
|
||||
|
||||
print("一双深色休闲皮鞋")
|
||||
|
||||
|
||||
|
||||
class KnittedSweaterDecorator(ClothingDecorator):
|
||||
|
||||
"针织毛衣"
|
||||
|
||||
|
||||
|
||||
def __init__(self, person):
|
||||
|
||||
super().__init__(person)
|
||||
|
||||
|
||||
|
||||
def wear(self):
|
||||
|
||||
super().wear()
|
||||
|
||||
print("一件紫红色针织毛衣")
|
||||
|
||||
|
||||
|
||||
class WhiteShirtDecorator(ClothingDecorator):
|
||||
|
||||
"白色衬衫"
|
||||
|
||||
|
||||
|
||||
def __init__(self, person):
|
||||
|
||||
super().__init__(person)
|
||||
|
||||
|
||||
|
||||
def wear(self):
|
||||
|
||||
super().wear()
|
||||
|
||||
print("一件白色衬衫")
|
||||
|
||||
|
||||
|
||||
class GlassesDecorator(ClothingDecorator):
|
||||
|
||||
"眼镜"
|
||||
|
||||
|
||||
|
||||
def __init__(self, person):
|
||||
|
||||
super().__init__(person)
|
||||
|
||||
|
||||
|
||||
def wear(self):
|
||||
|
||||
super().wear()
|
||||
|
||||
print("一副方形黑框眼镜")
|
||||
|
||||
|
||||
|
||||
</code></pre>
|
||||
|
||||
<p>测试代码:</p>
|
||||
@@ -809,25 +506,16 @@ class GlassesDecorator(ClothingDecorator):
|
||||
glasses = GlassesDecorator(sweater)
|
||||
|
||||
glasses.wear()
|
||||
|
||||
|
||||
|
||||
print()
|
||||
|
||||
decorateTeacher = GlassesDecorator(WhiteShirtDecorator(LeatherShoesDecorator(Teacher("wells", "教授"))))
|
||||
|
||||
decorateTeacher.wear()
|
||||
|
||||
|
||||
|
||||
</code></pre>
|
||||
|
||||
<p>上面的测试代码中:</p>
|
||||
|
||||
<pre><code>decorateTeacher = GlassesDecorator(WhiteShirtDecorator(LeatherShoesDecorator(Teacher("wells", "教授"))))
|
||||
|
||||
|
||||
|
||||
</code></pre>
|
||||
|
||||
<p>这个写法,大家不要觉得奇怪,它其实就是将多个对象的创建过程合在了一起,其实是一种优雅的写法(是不是少了好几行代码?)。创建的 Teacher 对象又通过参数传给 LeatherShoesDecorator 的构造函数,而创建的 LeatherShoesDecorator 对象又通过参数传给 WhiteShirtDecorator 的构造函数,以此类推……</p>
|
||||
@@ -849,9 +537,6 @@ class GlassesDecorator(ClothingDecorator):
|
||||
一件紫红色针织毛衣
|
||||
|
||||
一副方形黑框眼镜
|
||||
|
||||
|
||||
|
||||
我是wells教授
|
||||
|
||||
我的着装是:
|
||||
@@ -861,9 +546,6 @@ class GlassesDecorator(ClothingDecorator):
|
||||
一件白色衬衫
|
||||
|
||||
一副方形黑框眼镜
|
||||
|
||||
|
||||
|
||||
</code></pre>
|
||||
|
||||
<h3>从剧情中思考装饰模式</h3>
|
||||
@@ -931,9 +613,6 @@ class GlassesDecorator(ClothingDecorator):
|
||||
glasses = GlassesDecorator(shirt)
|
||||
|
||||
glasses.wear()
|
||||
|
||||
|
||||
|
||||
</code></pre>
|
||||
|
||||
<p>结果如下:</p>
|
||||
@@ -953,9 +632,6 @@ class GlassesDecorator(ClothingDecorator):
|
||||
一件白色衬衫
|
||||
|
||||
一副方形黑框眼镜
|
||||
|
||||
|
||||
|
||||
</code></pre>
|
||||
|
||||
<h3>装饰模式的模型抽象</h3>
|
||||
@@ -1013,17 +689,11 @@ class GlassesDecorator(ClothingDecorator):
|
||||
<pre><code class="language-java">DataInputStream dataInputStream = new DataInputStream(new FileInputStream("C:/text.txt"));
|
||||
|
||||
DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream("C:/text.txt"));
|
||||
|
||||
|
||||
|
||||
</code></pre>
|
||||
|
||||
<p>这个写法与上面 Demo 中的</p>
|
||||
|
||||
<pre><code>decorateTeacher = GlassesDecorator(WhiteShirtDecorator(LeatherShoesDecorator(Teacher("wells", "教授"))))
|
||||
|
||||
|
||||
|
||||
</code></pre>
|
||||
|
||||
<p>是不是很相似?都是一个对象套一个对象的方式进行创建。</p>
|
||||
@@ -1049,9 +719,6 @@ DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(&q
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
@@ -1059,9 +726,6 @@ DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(&q
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<a class="off-canvas-overlay" onclick="hide_canvas()"></a>
|
||||
|
||||
</div>
|
||||
@@ -1077,17 +741,11 @@ DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(&q
|
||||
<script>
|
||||
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
|
||||
|
||||
|
||||
function gtag() {
|
||||
|
||||
dataLayer.push(arguments);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
gtag('js', new Date());
|
||||
|
||||
gtag('config', 'G-NPSEEVD756');
|
||||
@@ -1113,9 +771,6 @@ DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(&q
|
||||
setCookie("lastPath", path)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function setCookie(cname, cvalue) {
|
||||
|
||||
var d = new Date();
|
||||
@@ -1127,9 +782,6 @@ DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(&q
|
||||
document.cookie = cname + "=" + cvalue + "; " + expires + ";path = /";
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function getCookie(cname) {
|
||||
|
||||
var name = cname + "=";
|
||||
@@ -1147,12 +799,6 @@ DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(&q
|
||||
return "";
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
|
||||
|
||||
</html>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user