版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
設(shè)計(jì)模式精華
《大話設(shè)計(jì)模式》讀書總結(jié)
薛棟
2015年4月1日
目錄
設(shè)計(jì)模式基本概念................................................................2
設(shè)計(jì)模式空間:..............................................................2
UML標(biāo)記....................................................................3
設(shè)計(jì)原則.....................................................................3
一,簡(jiǎn)單工場(chǎng)(simpleFactory)..............................................................................................4
l.lUML..........................................................................................................................................4
1.2代碼....................................................................4
1.3總結(jié)....................................................................6
二,策略模式(strategy)....................................................................................................................6
2.1UML..........................................................................................................................................6
2.2代碼....................................................................6
2.3總結(jié)....................................................................7
2.4用處....................................................................8
三,裝飾著模式(Decorator)............................................................................................................8
3.1UML..........................................................................................................................................8
3.2代碼....................................................................8
3.3總結(jié)...................................................................10
3.4用處...................................................................10
四,代理模式(Proxy)......................................................................................................................11
4.1UML........................................................................................................................................11
4.2代碼...................................................................11
4.3總結(jié)...................................................................12
4.4用處...................................................................13
五,工場(chǎng)方法模式(FactoryMethod)...........................................................................................13
5.1UML........................................................................................................................................13
52代碼....................................................................13
5.3總結(jié)....................................................................15
5.4用處...................................................................15
六,原型模式(Prototype)...............................................................................................................16
6.1UML........................................................................................................................................16
6.2代碼...................................................................16
6.2.1基本原型模式.....................................................16
6.2.2淺復(fù)制...........................................................17
6.2.3深復(fù)制............................................................17
6.3總結(jié)...................................................................19
6.4用處...................................................................19
七,模版方法(templateMethod).................................................................................................20
7.1UML........................................................................................................................................20
7.2代碼...................................................................20
7.3總結(jié)...................................................................22
7.4用處...................................................................22
八,外觀模式(Facade)...................................................................................................................23
8.1UML........................................................................................................................................23
8.2代碼...................................................................23
8.3總結(jié)...................................................................25
8.4用處...................................................................25
九,建造者模式(builder)..............................................................................................................26
9.1UML........................................................................................................................................26
9.2代碼...................................................................26
9.3總結(jié)...................................................................28
9.4用處...................................................................29
十,觀察者模式..................................................................29
10.1UML.....................................................................................................................................29
10.2代碼..................................................................29
10.3總結(jié)..................................................................32
10.4用處..................................................................32
H—?,抽象工廠模式(AbstractFactory)......................................................................................33
11.1UML.....................................................................................................................................33
11.2代碼..................................................................33
11.3總結(jié)..................................................................36
11.4用處..................................................................36
11.5注意..................................................................36
11.6反射機(jī)制.............................................................37
十二,狀態(tài)模式(State).................................................................................................................37
12.1UML.....................................................................................................................................37
12.2代碼..................................................................37
12.3總結(jié)..................................................................40
12.4用處..................................................................40
十三,適配器模式(Adapter).........................................................................................................41
13.1UML.....................................................................................................................................41
13.2代碼..................................................................41
13.3總結(jié)..................................................................43
13.4用處..................................................................44
十四,備忘錄模式(Memento)......................................................................................................44
14.1UML.....................................................................................................................................44
14.2代碼..................................................................44
14.3總結(jié)..................................................................46
14.4用處..................................................................46
十五,組合模式(Composite)........................................................................................................46
15.1UML.....................................................................................................................................46
15.2代碼..................................................................46
15.3總結(jié)..................................................................48
15.4用處..................................................................48
十六,迭代器模式(Iterator).........................................................................................................49
16.1UML.....................................................................................................................................49
16.2代碼..................................................................49
16.3總結(jié)..................................................................51
16.4用處..................................................................51
十七,單例模式(singleton)...........................................................................................................51
17.1UML.....................................................................................................................................51
17.2代碼..................................................................52
17.3總結(jié)..................................................................52
17.4用處..................................................................52
十八,橋接模式(bridge)...............................................................................................................53
18.1UML.....................................................................................................................................53
18.2代碼..................................................................53
18.3總結(jié)..................................................................55
18.4用處..................................................................55
十九,命令模式..................................................................55
19.1UML.....................................................................................................................................55
19.2代碼..................................................................55
19.3總結(jié)..................................................................57
19.4用處以及優(yōu)點(diǎn)..........................................................58
二十,職責(zé)鏈模式(ChainofResponsibility)................................................................................58
20.1UML.....................................................................................................................................58
20.2代碼..................................................................58
20.3總結(jié)..................................................................61
20.4用處..................................................................61
二十一,中介者模式(Mediator)...................................................................................................62
21.1UML.....................................................................................................................................62
21.2代碼..................................................................62
21.3總結(jié)..................................................................64
21.4用處..................................................................65
二十二,享元模式(Flyweight)......................................................................................................65
22.1UML.....................................................................................................................................65
22.2代碼..................................................................65
22.3總結(jié)..................................................................66
22.4用處..................................................................67
二十三,解釋器模式(interpreter)................................................................................................67
23.1UML.....................................................................................................................................67
23.2代碼..................................................................67
23.3總結(jié)..................................................................70
23.4用處..................................................................70
二十四,訪問者模式(Visitor).......................................................................................................70
24.1UML.....................................................................................................................................70
24.2代碼..................................................................71
24.3總結(jié)..................................................................73
24.4用處..................................................................73
總結(jié)............................................................................73
設(shè)計(jì)模式基本概念
設(shè)計(jì)模式空間:
目的
創(chuàng)建型結(jié)構(gòu)型行為型
范圍類FactionMethodAdapter(類)Interpreter
TemplateMethod
對(duì)象AbstractAdapter(對(duì)ChainofResponsibility
Factory象)Command
BuilderBridgeIterator
PrototypeCompositeMediator
SingletonDecoratorMemento
FacadeObserver
FlyweightState
ProxyStrategy
Visitor
UML標(biāo)記
企?關(guān)聯(lián)關(guān)第
二麗50
…也EtfO
—■承L■
?interface?實(shí)境接口
MU
矩形我示法怎■看<<h>t-e?
,行;排n名稱圈?旁為接口名稱
一:行;接口方法推口方法在實(shí)境美中出現(xiàn)
設(shè)計(jì)原則
?單一職責(zé)
?開閉原則(多擴(kuò)展,少修改)(對(duì)于變化的需求是通過增加新代碼解決的,而不是修改
原代碼,這就是開閉原則的精髓。
?依賴倒置原則
?迪米特法則(最少知識(shí)原則):如果兩個(gè)類不彼此直接通信,那么這兩個(gè)類就不應(yīng)當(dāng)發(fā)
生直接的相互作用。如果其中一個(gè)類需要調(diào)用某一個(gè)類的另一個(gè)方法的話,則可以通
過第三者轉(zhuǎn)發(fā)這個(gè)調(diào)用。
?里氏代換原則:只有當(dāng)子類代換掉父類,而軟件的單位功能不受影響時(shí),父類才能真
正被復(fù)用,子類才能在父類上添加功能。
?合成/聚合復(fù)用原則
簡(jiǎn)單工場(chǎng)(simpleFactory)
1.1UML
CashSi4tarCashFactary
*acceptCash0:double*createCashAccept0:CashSuper
¥
CashNornalCashRebateCashRetum
>acceptCash0:double*acceptCash0:double*acceptCash0:double
1.2代碼
packagecom.design.counter;
publicclassCounter{
publicstaticvoidmain(String[]args){
Opratero=OpraterationFactory.createOprateration(*+*);
System.out.printin(o.count());
)
)
abstractclassOprater{
publicOprater(intnumlrintnum2){
this.numl=numl;
this.num2=num2;
)
doublenuml,num2;
abstractdoublecount();
)
classAddOpraterextendsOprater{
publicAddOprater(intnuml,intnum2){
super(numl,num2);
doublecount(){
returnnuml+num2;
classDecOpraterextendsOprater{
publicDecOprater(intnumlrintnum2){
super(numl,num2);
}
doublecount(){
returnnuml-num2;
classMutOpraterextendsOprater{
publicMutOprater(intnuml,intnum2){
super(numlrnum2);
)
doublecount(){
returnnuml*num2;
classDevOpraterextendsOprater{
publicDevOprater(intnuml,intnum2){
super(numlznum2);
)
doublecount(){
returnnuml/num2;
classOpraterationFactory{
publicstaticintnuml;
publicstaticintnum2;
publicstaticOpratercreateOprateration(charop){
Oprateroprater=null;
switch(op){
case*+1:
oprater=newAddOprater(numl,num2);
break;
case
oprater=newAddOprater(numl,num2);
break;
case1*1.
oprater=newAddOprater(nun?7,nun72);
break;
case*/,:
oprater=newAddOprater(numlAnun?2);
break;
)
returnoprater;
}
)
1.3總結(jié)
1,產(chǎn)品有個(gè)抽象類,所有產(chǎn)品繼承抽象類
2,工場(chǎng)就一個(gè)靜態(tài)方法,得到產(chǎn)品;
說白了還是父類引用指向子類對(duì)象
二,策略模式(strategy)
2.1UML
策略模式(Strategy)結(jié)構(gòu)圖
2.2代碼
packagecom.design.strategy;
publicclassContex{
privateStrategys;
Contex(Strategys){
this.s=s;
)
publicvoidContexlnterface(){
s?algorithm();
publicstaticvoidmain(String[]args){
Contexc;
c=newContex(newAStratege());c.Contexlnterface();
c=newContex(newBStratege());c.Contexlnterface();
c=newContex(newCStratege());c.Contexlnterface();
}
}
abstractclassStrategy{
abstractpublicvoidalgorithm();
}
classAStrategeextendsStrategy{
publicvoidalgorithm(){
System.out.printIn(nAalgorithm");
classBStrategeextendsStrategy{
publicvoidalgorithm(){
System,out.printIn(nBalgorithm**);
classCStrategeextendsStrategy{
publicvoidalgorithm(){
System.out.printIn(nCalgorithm");
2.3總結(jié)
i,策略就是解決問題的一系列方法,但具體用哪個(gè)方法就不知道了,這樣就把策略放在一
個(gè)類中作為該類的成員變量,并有該類調(diào)用具體的策略;
2.策略的核心是:1)父類引用指向子類對(duì)象2)一個(gè)公告類把策略作為成員變量
2.4用處
當(dāng)問題有多種解決方法時(shí)。
三,裝飾著模式(Decorator)
3.1UML
3.2代碼
packagecom.design.decrator;
publicclassDecoratorTest{
publicstaticvoidmain(String[]args){
Personp=newPerson("RichardSine**);
TShirtt=newTShirt();
BigTrouserb=newBigTrouser();
Hath=newHat();
System,out.printin(“第—種裝飾=====
t.decorator(p);
b.decorator(t);
b?show();
System,out.printin("第二種裝飾=====
b.decorator(p);
h.decorator(b);
h.show();
//Person類
classPerson{
privateStringname;
Person(){
Person(Stringname){
=name;
}
publicvoidshow(){
System,out.printin("裝扮的"+nam一);
}
)
//裝飾類
classFineryextendsPerson{
protectedPersonp;
publicvoiddecorator(Personp){
this.p=p;
@(Override
publicvoidshow(){
p?show();
classTShirtextendsFinery{
@|)verride
publicvoidshow(){
System.out.printIn(T恤”);
super.show();
)
}
classBigTrouserextendsFinery{
publicvoidshow(){
System.out.printin("大垮褲");
super.show();
)
)
classHatextendsFinery{
@|)verride
publicvoidshow(){
System,out.printin("大帽子”);
super.show();
)
)
3.3總結(jié)
i,里氏代換原則:只有當(dāng)子類代換掉父類,而軟件的單位功能不受影響時(shí),父類才能真正
被復(fù)用,子類才能在父類上添加功能。
2,裝飾者模式的核心是所有的裝飾者繼承被裝飾對(duì)象,并且含有配裝飾對(duì)象作為成員變量。
3.4用處
當(dāng)系統(tǒng)需要新的功能時(shí),是向舊類中添加的新代碼。這些新加的代碼通常修飾的原有類的
核心職責(zé)。
四,代理模式(Proxy)
4.1UML
代理模式(Pro”)結(jié)構(gòu)圖
Pi3y類,保存一個(gè)用用使用代理可以訪問實(shí)體.
RealSubject類.定義Proxy所代表的山實(shí)實(shí)體并提供個(gè)與Subject的接口相同的接口.這群
代時(shí)就可以用來普代實(shí)體
4.2代碼
packagexy;
publicclassProxyTest{
publicstaticvoidmain(String[]args){
Pursuitpursuit=newPursuit("jiaojiao");
Proxyproxy=newProxy(pursuit);
proxy.giveDolls();
proxy.giveFlowers();
proxy.giveCholote();
)
)
interfaceGiveFift{
voidgiveDolls();
voidgiveFlowers();
voidgiveCholote();
)
classPursuitimplementsGiveFift{
privateStringschoolGirl;
Pursuit(StringschoolGirl){
this.schoolGirl=schoolGirl;
)
@Override
publicvoidgiveDolls(){
System,out.printin("送你洋娃娃");
)
@Override
publicvoidgiveFlowers(){
System.out.printin("送你花”);
)
@Override
publicvoidgiveCholote(){
Syst一m.out.printIn("送你巧克力”);
)
)
classProxyimplementsGiveFift{
privatePursuitp;
Proxy(Pursuitp){
this.p=p;
}
@Override
publicvoidgiveDolls(){
p?giveDolls();
}
@Override
publicvoidgiveFlowers(){
p,giveFlowers();
)
@Override
publicvoidgiveCholote(){
p?giveCholote();
4.3總結(jié)
i,代理模式很簡(jiǎn)單,首先:真實(shí)追求者和代理人必須實(shí)現(xiàn)相同的接口(這樣代理人才能給
真實(shí)追求者完成不多余的動(dòng)作),第二,代理類中含有真是追求者作為成員變量,而動(dòng)作的
真是發(fā)起者是這個(gè)真是變量。
44用處
這個(gè)沒必要說了吧。
五,工場(chǎng)方法模式(FactoryMethod)
5.1UML
運(yùn)年美
工廠類
?NunberA:double
?NuaberB:double
*GetResult0:double?CreateOperation0:運(yùn)算類
加法類顓法工廠集法工廠
-KietResult0:dodile?GetResult0:double
減法類
X法工廠除法工廠
*GetResult0:doubleKJetResult0:dod)le
5.2代碼
packagecom.design.factoryMethod;
publicclassFactoryMethodTest{
publicstaticvoidmain(String[]args){
IFactoryopreationFactory=newAddFactory();
Opreationopre=opreationFactory.createOpreation();
opre.numl=10;
opre.num2=15;
System.out.printin(opre.getResult());
}
abstractclassOpreation{
intnuml,num2;
abstractpublicdoublegetResult();
classAddOpreationextendsOpreation{
publicdoublegetResult(){
returnnuml+num2;
)
}
classDecOpreationextendsOpreation{
publicdoublegetResult(){
returnnuml-num2;
}
classMutOpreationextendsOpreation{
publicdoublegetResult(){
returnnuml*num2;
)
classDevOpreationextendsOpreation{
publicdoublegetResult(){
returnnuml/num2;
interfaceIFactory{
OpreationcreateOpreation();
)
classAddFactoryimplementsIFactory{
@Override
publicOpreationcreateOpreation(){
returnnewAddOpreation();
classDecFactoryimplementsIFactory{
@Override
publicOpreationcreateOpreation(){
returnnewDecOpreation();
classMutFactoryimplementsIFactory{
@Override
publicOpreationcreateOpreation(){
returnnewMutOpreation();
)
)
classDevFactoryimplementsIFactory{
@Override
publicOpreationcreateOpreation(){
returnnewDevOpreation();
)
}
5.3總結(jié)
i,工場(chǎng)方法就是給沒一個(gè)操作都產(chǎn)生一個(gè)工場(chǎng)。用到的思想還是面向接口編程而非實(shí)現(xiàn)。
也體現(xiàn)了開閉原則。
2,工場(chǎng)方法與簡(jiǎn)單工場(chǎng)相比耦合度更低。
3,工場(chǎng)方法實(shí)現(xiàn)時(shí),客戶端需要決定實(shí)例化哪個(gè)工場(chǎng)來實(shí)現(xiàn)運(yùn)算類。而與簡(jiǎn)單工場(chǎng)相比,
它把簡(jiǎn)單工場(chǎng)的內(nèi)部判斷邏輯搬到了客戶端來。若想加功能,在簡(jiǎn)單工場(chǎng)中是改工場(chǎng)類,
而現(xiàn)在改在了客戶端。
5.4用處
注意分類時(shí)候要用到工場(chǎng)模式,是簡(jiǎn)單工場(chǎng)還是工場(chǎng)方法要仔細(xì)判斷。
六,原型模式(Prototype)
6.1UML
原型模式(Prototype)結(jié)構(gòu)圖
6.2代碼
6.2.1基本原型模式
packagetotype;
publicclassPrototypeTest{
publicstaticvoidmain(String[]args){
Resumer=newResume("RichardShine").setPersonalInfomation(
男”,n22n).setWorkExperience("3n,"Tecent").showinfo();
try(
Resumer2=(Resume)r.clone();
r2.showinfo();
}catch(CloneNotSupportedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
classResumeimplementsCloneable{
privateStringname;
privateStringsex;
privateStringage;
privateStringtimeArea;
privateStringcompany;
Resume(Stringname){
=name;
)
publicResumesetPersonallnfomation(Stringsex,Stringage){
this.sex=sex;
this.age=age;
returnthis;
)
publicResumesetWorkExperience(StringtimeArea,Stringcompany){
this.timeArea=timeArea;
pany=company;
returnthis;
)
publicResumeshowinfo(){
Syst一m.out.printin("個(gè)人信息:n);
System,out.printIn("+name+H年齡:H+age);
Syst一m.out.printin("工作經(jīng)驗(yàn):n);
Syst一m.ou七.printin("工作時(shí)間:n+timeArea+n工作地點(diǎn):n+company);
returnthis;
)
@Override
protectedObjectclone()throwsCloneNotSupportedException{
returnthis;
6.2.2淺復(fù)制
,NET中有淺復(fù)制
6.2.3深復(fù)制
packagetotype;
publicclassPrototypeTest{
publicstaticvoidmain(String[]args){
Resumer=newResume("RichardShinen).setPersonallnfomation(
男”,n22n.setWorkExperience(*'3"Tecent11).showlnf;
try{
Resumer2=(Resume)r.clone();
r2.setWorkExperience("4","IBM").showinfo();
}catch(CloneNotSupportedExceptione){
//TOD
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年飯店業(yè)主權(quán)轉(zhuǎn)讓協(xié)議
- 2024年重慶股權(quán)轉(zhuǎn)讓協(xié)議精簡(jiǎn)
- 2024年冬季道路掃雪服務(wù)承包協(xié)議
- 2024屆安徽池州市高三年級(jí)寒假驗(yàn)收考試數(shù)學(xué)試題試卷
- 2023-2024學(xué)年浙江省效實(shí)中學(xué)高三下期末教學(xué)檢測(cè)試題數(shù)學(xué)試題試卷
- 化服務(wù)交易結(jié)算協(xié)議模板2024
- 2024年度裝修項(xiàng)目協(xié)議樣本
- 2024蝦池養(yǎng)殖權(quán)承包協(xié)議示例
- 2024掛靠項(xiàng)目管理協(xié)議樣本集萃
- 2024年天然氣服務(wù)協(xié)議范例
- 廣東省2024-2025學(xué)年高三上學(xué)期10月份聯(lián)考?xì)v史試卷 - 副本
- 2024-2030年中國(guó)軟件測(cè)試行業(yè)現(xiàn)狀分析及投資風(fēng)險(xiǎn)預(yù)測(cè)報(bào)告
- 2024-2030年中國(guó)花青素市場(chǎng)銷售狀況與消費(fèi)趨勢(shì)預(yù)測(cè)報(bào)告
- 旅館業(yè)設(shè)施布局與室內(nèi)設(shè)計(jì)考核試卷
- 2024年消防知識(shí)競(jìng)賽考試題庫(kù)300題(含答案)
- 2024中國(guó)船舶報(bào)社公開招聘采編人員1人高頻難、易錯(cuò)點(diǎn)500題模擬試題附帶答案詳解
- 中圖版2024-2025學(xué)年八年級(jí)地理上冊(cè)期中卷含答案
- 室內(nèi)裝修投標(biāo)方案(技術(shù)方案)
- 噴漆安全管理制度
- 山東科學(xué)技術(shù)出版社小學(xué)一年級(jí)上冊(cè)綜合實(shí)踐活動(dòng)教案
- 2024-2030年中國(guó)市政公用工程行業(yè)市場(chǎng)發(fā)展分析及發(fā)展?jié)摿εc投資研究報(bào)告
評(píng)論
0/150
提交評(píng)論