设计模式精髓-《大话设计模式》读书录_第1页
设计模式精髓-《大话设计模式》读书录_第2页
设计模式精髓-《大话设计模式》读书录_第3页
设计模式精髓-《大话设计模式》读书录_第4页
设计模式精髓-《大话设计模式》读书录_第5页
已阅读5页,还剩72页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

设计模式精华

《大话设计模式》读书总结

薛栋

2015年4月1日

目录

设计模式基本概念................................................................2

设计模式空间:..............................................................2

UML标记....................................................................3

设计原则.....................................................................3

一,简单工场(simpleFactory)..............................................................................................4

l.lUML..........................................................................................................................................4

1.2代码....................................................................4

1.3总结....................................................................6

二,策略模式(strategy)....................................................................................................................6

2.1UML..........................................................................................................................................6

2.2代码....................................................................6

2.3总结....................................................................7

2.4用处....................................................................8

三,装饰着模式(Decorator)............................................................................................................8

3.1UML..........................................................................................................................................8

3.2代码....................................................................8

3.3总结...................................................................10

3.4用处...................................................................10

四,代理模式(Proxy)......................................................................................................................11

4.1UML........................................................................................................................................11

4.2代码...................................................................11

4.3总结...................................................................12

4.4用处...................................................................13

五,工场方法模式(FactoryMethod)...........................................................................................13

5.1UML........................................................................................................................................13

52代码....................................................................13

5.3总结....................................................................15

5.4用处...................................................................15

六,原型模式(Prototype)...............................................................................................................16

6.1UML........................................................................................................................................16

6.2代码...................................................................16

6.2.1基本原型模式.....................................................16

6.2.2浅复制...........................................................17

6.2.3深复制............................................................17

6.3总结...................................................................19

6.4用处...................................................................19

七,模版方法(templateMethod).................................................................................................20

7.1UML........................................................................................................................................20

7.2代码...................................................................20

7.3总结...................................................................22

7.4用处...................................................................22

八,外观模式(Facade)...................................................................................................................23

8.1UML........................................................................................................................................23

8.2代码...................................................................23

8.3总结...................................................................25

8.4用处...................................................................25

九,建造者模式(builder)..............................................................................................................26

9.1UML........................................................................................................................................26

9.2代码...................................................................26

9.3总结...................................................................28

9.4用处...................................................................29

十,观察者模式..................................................................29

10.1UML.....................................................................................................................................29

10.2代码..................................................................29

10.3总结..................................................................32

10.4用处..................................................................32

H—•,抽象工厂模式(AbstractFactory)......................................................................................33

11.1UML.....................................................................................................................................33

11.2代码..................................................................33

11.3总结..................................................................36

11.4用处..................................................................36

11.5注意..................................................................36

11.6反射机制.............................................................37

十二,状态模式(State).................................................................................................................37

12.1UML.....................................................................................................................................37

12.2代码..................................................................37

12.3总结..................................................................40

12.4用处..................................................................40

十三,适配器模式(Adapter).........................................................................................................41

13.1UML.....................................................................................................................................41

13.2代码..................................................................41

13.3总结..................................................................43

13.4用处..................................................................44

十四,备忘录模式(Memento)......................................................................................................44

14.1UML.....................................................................................................................................44

14.2代码..................................................................44

14.3总结..................................................................46

14.4用处..................................................................46

十五,组合模式(Composite)........................................................................................................46

15.1UML.....................................................................................................................................46

15.2代码..................................................................46

15.3总结..................................................................48

15.4用处..................................................................48

十六,迭代器模式(Iterator).........................................................................................................49

16.1UML.....................................................................................................................................49

16.2代码..................................................................49

16.3总结..................................................................51

16.4用处..................................................................51

十七,单例模式(singleton)...........................................................................................................51

17.1UML.....................................................................................................................................51

17.2代码..................................................................52

17.3总结..................................................................52

17.4用处..................................................................52

十八,桥接模式(bridge)...............................................................................................................53

18.1UML.....................................................................................................................................53

18.2代码..................................................................53

18.3总结..................................................................55

18.4用处..................................................................55

十九,命令模式..................................................................55

19.1UML.....................................................................................................................................55

19.2代码..................................................................55

19.3总结..................................................................57

19.4用处以及优点..........................................................58

二十,职责链模式(ChainofResponsibility)................................................................................58

20.1UML.....................................................................................................................................58

20.2代码..................................................................58

20.3总结..................................................................61

20.4用处..................................................................61

二十一,中介者模式(Mediator)...................................................................................................62

21.1UML.....................................................................................................................................62

21.2代码..................................................................62

21.3总结..................................................................64

21.4用处..................................................................65

二十二,享元模式(Flyweight)......................................................................................................65

22.1UML.....................................................................................................................................65

22.2代码..................................................................65

22.3总结..................................................................66

22.4用处..................................................................67

二十三,解释器模式(interpreter)................................................................................................67

23.1UML.....................................................................................................................................67

23.2代码..................................................................67

23.3总结..................................................................70

23.4用处..................................................................70

二十四,访问者模式(Visitor).......................................................................................................70

24.1UML.....................................................................................................................................70

24.2代码..................................................................71

24.3总结..................................................................73

24.4用处..................................................................73

总结............................................................................73

设计模式基本概念

设计模式空间:

目的

创建型结构型行为型

范围类FactionMethodAdapter(类)Interpreter

TemplateMethod

对象AbstractAdapter(对ChainofResponsibility

Factory象)Command

BuilderBridgeIterator

PrototypeCompositeMediator

SingletonDecoratorMemento

FacadeObserver

FlyweightState

ProxyStrategy

Visitor

UML标记

企・关联关第

二丽50

…也EtfO

—■承L■

«interface»实境接口

MU

矩形我示法怎■看<<h>t-e»

,行;排n名称圈・旁为接口名称

一:行;接口方法推口方法在实境美中出现

设计原则

•单一职责

・开闭原则(多扩展,少修改)(对于变化的需求是通过增加新代码解决的,而不是修改

原代码,这就是开闭原则的精髓。

•依赖倒置原则

•迪米特法则(最少知识原则):如果两个类不彼此直接通信,那么这两个类就不应当发

生直接的相互作用。如果其中一个类需要调用某一个类的另一个方法的话,则可以通

过第三者转发这个调用。

・里氏代换原则:只有当子类代换掉父类,而软件的单位功能不受影响时,父类才能真

正被复用,子类才能在父类上添加功能。

・合成/聚合复用原则

简单工场(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总结

1,产品有个抽象类,所有产品继承抽象类

2,工场就一个静态方法,得到产品;

说白了还是父类引用指向子类对象

二,策略模式(strategy)

2.1UML

策略模式(Strategy)结构图

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总结

i,策略就是解决问题的一系列方法,但具体用哪个方法就不知道了,这样就把策略放在一

个类中作为该类的成员变量,并有该类调用具体的策略;

2.策略的核心是:1)父类引用指向子类对象2)一个公告类把策略作为成员变量

2.4用处

当问题有多种解决方法时。

三,装饰着模式(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总结

i,里氏代换原则:只有当子类代换掉父类,而软件的单位功能不受影响时,父类才能真正

被复用,子类才能在父类上添加功能。

2,装饰者模式的核心是所有的装饰者继承被装饰对象,并且含有配装饰对象作为成员变量。

3.4用处

当系统需要新的功能时,是向旧类中添加的新代码。这些新加的代码通常修饰的原有类的

核心职责。

四,代理模式(Proxy)

4.1UML

代理模式(Pro”)结构图

Pi3y类,保存一个用用使用代理可以访问实体.

RealSubject类.定义Proxy所代表的山实实体并提供个与Subject的接口相同的接口.这群

代时就可以用来普代实体

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总结

i,代理模式很简单,首先:真实追求者和代理人必须实现相同的接口(这样代理人才能给

真实追求者完成不多余的动作),第二,代理类中含有真是追求者作为成员变量,而动作的

真是发起者是这个真是变量。

44用处

这个没必要说了吧。

五,工场方法模式(FactoryMethod)

5.1UML

运年美

工厂类

♦NunberA:double

♦NuaberB:double

*GetResult0:double♦CreateOperation0:运算类

加法类颛法工厂集法工厂

-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总结

i,工场方法就是给没一个操作都产生一个工场。用到的思想还是面向接口编程而非实现。

也体现了开闭原则。

2,工场方法与简单工场相比耦合度更低。

3,工场方法实现时,客户端需要决定实例化哪个工场来实现运算类。而与简单工场相比,

它把简单工场的内部判断逻辑搬到了客户端来。若想加功能,在简单工场中是改工场类,

而现在改在了客户端。

5.4用处

注意分类时候要用到工场模式,是简单工场还是工场方法要仔细判断。

六,原型模式(Prototype)

6.1UML

原型模式(Prototype)结构图

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("个人信息:n);

System,out.printIn("+name+H年龄:H+age);

Syst㊀m.out.printin("工作经验:n);

Syst㊀m.ou七.printin("工作时间:n+timeArea+n工作地点:n+company);

returnthis;

)

@Override

protectedObjectclone()throwsCloneNotSupportedException{

returnthis;

6.2.2浅复制

,NET中有浅复制

6.2.3深复制

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等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论