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

下载本文档

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

文档简介

设计模式精华大话设计模式读书总结薛栎2015年4月1日 目录设计模式基本概念2设计模式空间:2UML标记3设计原则3一,简单工场(simple Factory)41.1UML41.2代码41.3总结6二,策略模式(strategy)62.1UML62.2代码62.3总结72.4用处8三,装饰着模式(Decorator)83.1UML83.2代码83.3总结103.4 用处10四,代理模式(Proxy)114.1UML114.2代码114.3总结124.4用处13五,工场方法模式(Factory Method)135.1UML135.2代码135.3 总结155.4 用处15六,原型模式(Prototype)166.1UML166.2代码166.2.1基本原型模式166.2.2浅复制176.2.3深复制176.3总结196.4用处19七,模版方法(template Method)207.1UML207.2代码207.3总结227.4用处22八,外观模式(Facade)238.1UML238.2代码238.3总结258.4用处25九,建造者模式(builder)269.1UML269.2代码269.3总结289.4用处29十,观察者模式2910.1UML2910.2代码2910.3总结3210.4用处32十一,抽象工厂模式(Abstract Factory)3311.1UML3311.2代码3311.3总结3611.4用处3611.5注意3611.6反射机制37十二,状态模式(State)3712.1UML3712.2代码3712.3总结4012.4用处40十三,适配器模式(Adapter)4113.1UML4113.2代码4113.3总结4313.4用处44十四,备忘录模式(Memento)4414.1UML4414.2代码4414.3总结4614.4用处46十五,组合模式(Composite)4615.1UML4615.2代码4615.3总结4815.4用处48十六,迭代器模式(Iterator)4916.1UML4916.2代码4916.3总结5116.4用处51十七,单例模式(singleton)5117.1UML5117.2代码5217.3总结5217.4用处52十八,桥接模式(bridge)5318.1UML5318.2代码5318.3总结5518.4用处55十九,命令模式5519.1UML5519.2代码5519.3总结5719.4用处以及优点58二十,职责链模式(Chain of Responsibility)5820.1UML5820.2代码5820.3总结6120.4用处61二十一,中介者模式(Mediator)6221.1UML6221.2代码6221.3总结6421.4用处65二十二,享元模式(Flyweight)6522.1UML6522.2代码6522.3总结6622.4用处67二十三,解释器模式(interpreter)6723.1UML6723.2代码6723.3总结7023.4用处70二十四,访问者模式(Visitor)7024.1UML7024.2代码7124.3总结7324.4用处73总结73设计模式基本概念设计模式空间: 目的创建型结构型行为型范围 类Faction MethodAdapter(类)InterpreterTemplate Method对象Abstract FactoryBuilderPrototypeSingletonAdapter(对象)BridgeCompositeDecoratorFaadeFlyweightProxyChain of ResponsibilityCommandIteratorMediatorMementoObserverStateStrategyVisitorUML标记设计原则 单一职责 开闭原则(多扩展,少修改)(对于变化的需求是通过增加新代码解决的,而不是修改原代码,这就是开闭原则的精髓。 依赖倒置原则 迪米特法则(最少知识原则):如果两个类不彼此直接通信,那么这两个类就不应当发生直接的相互作用。如果其中一个类需要调用某一个类的另一个方法的话,则可以通过第三者转发这个调用。 里氏代换原则:只有当子类代换掉父类,而软件的单位功能不受影响时,父类才能真正被复用,子类才能在父类上添加功能。 合成/聚合 复用原则 简单工场(simple Factory)1.1UML1.2代码package com.design.counter;public class Counter public static void main(String args) Oprater o = OpraterationFactory.createOprateration(+);System.out.println(o.count();abstract class Opraterpublic Oprater(int num1,int num2) this.num1 = num1;this.num2 = num2;double num1,num2;abstract double count();class AddOprater extends Opraterpublic AddOprater(int num1,int num2) super(num1, num2);double count() return num1 + num2;class DecOprater extends Opraterpublic DecOprater(int num1,int num2) super(num1, num2);double count() return num1 - num2;class MutOprater extends Opraterpublic MutOprater(int num1,int num2) super(num1, num2);double count() return num1 * num2;class DevOprater extends Opraterpublic DevOprater(int num1,int num2) super(num1, num2);double count() return num1 / num2;class OpraterationFactorypublic static int num1;public static int num2;public static Oprater createOprateration(char op)Oprater oprater = null;switch(op)case +:oprater = new AddOprater(num1,num2);break;case -:oprater = new AddOprater(num1,num2);break;case *:oprater = new AddOprater(num1,num2);break;case /:oprater = new AddOprater(num1,num2);break;return oprater;1.3总结1,产品有个抽象类,所有产品继承抽象类2,工场就一个静态方法,得到产品;说白了还是父类引用指向子类对象二,策略模式(strategy)2.1UML2.2代码package com.design.strategy;public class Contex private Strategy s;Contex(Strategy s)this.s = s;public void ContexInterface()s.algorithm();public static void main(String args) Contex c;c = new Contex(new AStratege();c.ContexInterface();c = new Contex(new BStratege();c.ContexInterface();c = new Contex(new CStratege();c.ContexInterface();abstract class Strategyabstract public void algorithm();class AStratege extends Strategypublic void algorithm() System.out.println(A algorithm);class BStratege extends Strategypublic void algorithm() System.out.println(B algorithm);class CStratege extends Strategypublic void algorithm() System.out.println(C algorithm);2.3总结1,策略就是解决问题的一系列方法,但具体用哪个方法就不知道了,这样就把策略放在一个类中作为该类的成员变量,并有该类调用具体的策略;2.策略的核心是:1)父类引用指向子类对象2)一个公告类把策略作为成员变量2.4用处当问题有多种解决方法时。三,装饰着模式(Decorator)3.1UML3.2代码package com.design.decrator;public class DecoratorTest public static void main(String args) Person p = new Person(RichardSine);TShirt t = new TShirt();BigTrouser b = new BigTrouser();Hat h = new Hat();System.out.println(第一种装饰=);t.decorator(p);b.decorator(t);b.show();System.out.println(第二种装饰=);b.decorator(p);h.decorator(b);h.show();/Person 类class Personprivate String name;Person() Person(String name) = name;public void show()System.out.println(装扮的+name);/装饰类class Finery extends Personprotected Person p;public void decorator(Person p)this.p = p;Overridepublic void show() p.show();class TShirt extends FineryOverridepublic void show() System.out.println(大 T恤);super.show();class BigTrouser extends Finerypublic void show() System.out.println(大垮裤);super.show();class Hat extends FineryOverridepublic void show() System.out.println(大帽子);super.show();3.3总结1,里氏代换原则:只有当子类代换掉父类,而软件的单位功能不受影响时,父类才能真正被复用,子类才能在父类上添加功能。2,装饰者模式的核心是所有的装饰者继承被装饰对象,并且含有配装饰对象作为成员变量。3.4 用处当系统需要新的功能时,是向旧类中添加的新代码。这些新加的代码通常修饰的原有类的核心职责。四,代理模式(Proxy)4.1UML4.2代码package xy;public class ProxyTest public static void main(String args) Pursuit pursuit = new Pursuit(jiaojiao);Proxy proxy = new Proxy(pursuit);proxy.giveDolls();proxy.giveFlowers();proxy.giveCholote();interface GiveFiftvoid giveDolls();void giveFlowers();void giveCholote();class Pursuit implements GiveFiftprivate String schoolGirl;Pursuit(String schoolGirl) this.schoolGirl = schoolGirl;Overridepublic void giveDolls() System.out.println(送你洋娃娃);Overridepublic void giveFlowers() System.out.println(送你花);Overridepublic void giveCholote() System.out.println(送你巧克力);class Proxy implements GiveFiftprivate Pursuit p;Proxy(Pursuit p) this.p = p;Overridepublic void giveDolls() p.giveDolls();Overridepublic void giveFlowers() p.giveFlowers();Overridepublic void giveCholote() p.giveCholote();4.3总结1,代理模式很简单,首先:真实追求者和代理人必须实现相同的接口(这样代理人才能给真实追求者完成不多余的动作),第二,代理类中含有真是追求者作为成员变量,而动作的真是发起者是这个真是变量。4.4用处这个没必要说了吧。五,工场方法模式(Factory Method)5.1UML5.2代码package com.design.factoryMethod;public class FactoryMethodTest public static void main(String args) IFactory opreationFactory = new AddFactory();Opreation opre = opreationFactory.createOpreation();opre.num1 = 10;opre.num2 = 15;System.out.println(opre.getResult();abstract class Opreationint num1,num2;abstract public double getResult();class AddOpreation extends Opreationpublic double getResult() return num1+num2;class DecOpreation extends Opreationpublic double getResult() return num1-num2;class MutOpreation extends Opreationpublic double getResult() return num1*num2;class DevOpreation extends Opreationpublic double getResult() return num1/num2;interface IFactoryOpreation createOpreation();class AddFactory implements IFactoryOverridepublic Opreation createOpreation() return new AddOpreation();class DecFactory implements IFactoryOverridepublic Opreation createOpreation() return new DecOpreation();class MutFactory implements IFactoryOverridepublic Opreation createOpreation() return new MutOpreation();class DevFactory implements IFactoryOverridepublic Opreation createOpreation() return new DevOpreation();5.3 总结1,工场方法就是给没一个操作都产生一个工场。用到的思想还是面向接口编程而非实现。也体现了开闭原则。2,工场方法与简单工场相比耦合度更低。3,工场方法实现时,客户端需要决定实例化哪个工场来实现运算类。而与简单工场相比,它把简单工场的内部判断逻辑搬到了客户端来。若想加功能,在简单工场中是改工场类,而现在改在了客户端。5.4 用处注意分类时候要用到工场模式,是简单工场还是工场方法要仔细判断。六,原型模式(Prototype)6.1UML6.2代码6.2.1基本原型模式package totype;public class PrototypeTest public static void main(String args) Resume r = new Resume(RichardShine).setPersonalInfomation(男, 22).setWorkExperience(3, Tecent).showInfo();try Resume r2 = (Resume)r.clone();r2.showInfo(); catch (CloneNotSupportedException e) / TODO Auto-generated catch blocke.printStackTrace();class Resume implements Cloneableprivate String name;private String sex;private String age;private String timeArea;private String company; Resume(String name) = name; public Resume setPersonalInfomation(String sex,String age) this.sex = sex; this.age = age; return this; public Resume setWorkExperience(String timeArea,String company) this.timeArea = timeArea; pany = company; return this; public Resume showInfo() System.out.println(个人信息:); System.out.println(名字: +name+ 年龄:+age); System.out.println(工作经验:); System.out.println(工作时间:+timeArea+ 工作地点:+company); return this; Overrideprotected Object clone() throws CloneNotSupportedException return this;6.2.2浅复制.NET中有浅复制6.2.3深复制package totype;public class PrototypeTest public static void main(String args) Resume r = new Resume(RichardShine).setPersonalInfomation(男, 22).setWorkExperience(3, Tecent).showInfo();try Resume r2 = (Resume)r.clone();r2.setWorkExperience(4, IBM).showInfo(); catch (CloneNotSupportedException e) / TODO Auto-generated catch blocke.printStackTrace();class Resume implements Cloneableprivate String name;private String sex;private String age;private WorkExperience work; Resume(String name) = name; work = new WorkExperience(); public Resume setWorkExperience(String workDate,String workCompany) work.setWorkDate(workDate); work.setWorkCompany(workCompany); return this; public Resume setPersonalInfomation(String sex,String age) this.sex = sex; this.age = age; return this; public Resume showInfo() System.out.println(个人信息:); System.out.println(名字: +name+ 性别:+sex+ 年龄:+age); System.out.println(工作经验:); System.out.println(工作地点:+work.getWorkCompany()+工作时间:+work.getWorkDate(); return this; Overrideprotected Object clone() throws CloneNotSupportedException return this;class WorkExperienceprivate String workDate;private String workCompany;public String getWorkDate() return workDate;public void setWorkDate(String workDate) this.workDate = workDate;public String getWorkCompany() return workCompany;public void setWorkCompany(String workCompany) this.workCompany = workCompany;6.3总结1,没别的就是用了clone()方法。这是最基本的。但是主要clone是浅复制。浅复制和深复制:浅复制:如果字段(成员变量是基本数据类型,则对盖字段逐位复制,而如果是引用类型则只复制引用,不复制对象);深复制:就是所有的都复制了,没什么好解释的了。Java中clone()是深复制;6.4用处就是复制七,模版方法(template Method)7.1UML7.2代码package com.design.TemplateMethod;public class TemplateMethodTest public static void main(String args) TestPaper t1 = new TestPaperA();System.out.println(学生t1的答案:);System.out.println(t1.answerOne()+t1.answerTwo()+t1.answerThree();TestPaper t2 = new TestPaperB();System.out.println(学生t2的答案:);System.out.println(t2.answerOne()+t2.answerTwo()+t2.answerThree();abstract class TestPaperpublic void questionOne()System.out.println(int in java hava byte.+n A.1 B.4 C.8 D.16 n+Answer is:+answerOne();public void questionTwo()System.out.println(long in java hava byte.+n A.1 B.4 C.8 D.16 n+Answer is:+answerTwo();public void questionThree()System.out.println(char in java hava byte.+n A.1 B.4 C.8 D.16 n+Answer is:+answerThree();abstract public String answerOne();abstract public String answerTwo();abstract public String answerThree();class TestPaperA extends TestPaperOverridepublic String answerOne() return B;Overridepublic String answerTwo() return C;Overridepublic String answerThree() return A;class TestPaperB extends TestPaperOverridepublic String answerOne() return D;Overridepublic String answerTwo() return C;Overridepublic String answerThree() return A;7.3总结1,模版其实就是抽象类。定义并实现了一个模版方法,这个模版方法是一个具体的方法,它给出了一个顶级逻辑框架,而逻辑的组成步骤在抽象方法中。7.4用处一系列步骤构成的过程需要执行,这些步骤从高层上来看是相同的,但具体细节不同,这时需要用到模版方法。八,外观模式(Facade)8.1UML8.2代码package com.design.facade;public class FacadeTest public static void main(String args) Found jijin = new Found();jijin.foundBuy();jijin.foundSell();class Foundprivate Stock1 s1;private Stock2 s2;private NationalDebet nd;private Realty r ;Found() s1 = new Stock1();s2 = new Stock2();nd = new NationalDebet();r = new Realty();public void foundBuy()s1.buy();s2.buy();nd.buy();r.buy();public void foundSell()s1.sell();s2.sell();nd.sell();r.sell();class Stock1public void buy()System.out.println(买股票1);public void sell()System.out.println(卖股票1);class Stock2public void buy()System.out.println(买股票2);public void sell()System.out.println(卖股票2);class NationalDebetpublic void buy()System.out.println(买国债);public void sell()System.out.println(卖国债);class Realtypublic void buy()System.out.println(买房产);public void sell()System.out.println(卖房产);8.3总结1,没什么总结的了,就是统一一下,记住基金这个例子就行。8.4用处九,建造者模式(builder)9.1UML9.2代码package com.design.builder;import java.awt.Graphics;public class BuilderTest public static void main(String args) System.out.println(瘦娃娃:);PersonBuiider tpb = new ThinPersonBuiider(huapu, pen);PersonDriector pd = new PersonDriector(tpb);pd.createPerson();System.out.println(胖娃娃:);PersonBuiider fpb= new FatPersonBuiider(huapu, pen);pd = new PersonDriector(fpb);pd.createPerson();abstract class PersonBuiiderprivate String huaBu;private String pen;PersonBuiider(String huaBu,String pen)this.huaBu = huaBu;this.pen = pen;public abstract void drawHead();public abstract void drawBody();public abstract void drawTwoArm();public abstract void drawTwoLeg();class ThinPersonBuiider extends PersonBuiiderThinPersonBuiider(String huaBu,String pen)super(huaBu, pen);Overridepublic void drawHead() System.out.println(画头);Overridepublic void drawBody() System.out.println(画瘦身);Overridepublic void drawTwoArm() System.out.println(画俩胳膊);Overridepublic void drawTwoLeg() System.out.println(画俩腿);class FatPersonBuiider extends PersonBuiiderFatPersonBuiider(String huaBu,String pen)super(huaBu, pen);Overridepublic void drawHead() System.out.println(画头);Overridepublic void drawBody() System.out.println(画胖身);Overridepublic void drawTwoArm() System.out.println(画俩胳膊);Overridepublic void drawTwoLeg() System.out.println(画俩腿);class PersonDriectorPersonBuiider personBuiider;PersonDriector(PersonBuiider personBuiider)this.personBuiider = personBuiider;public void createPerson()personBuiider.drawHead();personBuiider.drawBody();personBuiider.drawTwoArm();personBuiider.drawTwoLeg();9.3总结1,没别的还是父类引用指向子类对象,跟代理模式很像。规范了行为。9.4用处用于创建一些复杂的对象,这些对象构建间的构建顺序通常是稳定的。但对象内部的构建通常是复杂多变的。十,观察者模式10.1UML10.2代码package com.design.observer;import java.awt.event.ItemListener;import java.util.ArrayList;import java.util.List;public class ObserverTest public static void main(String args) NBAObserver o1 = new NBAObserver(zhangsan);StockObserver o2 = new StockObserver(Lisi);NewsObserver o3 = new NewsObserver(zhaoliu);Subject boss = new Boss();Subject countet = new counter();boss.attachObserver(o1);boss.attachObserver(o2);countet.attachObserver(o3);boss.notifyAllObservers();countet.notifyAllObservers();abstract class Subjectprotected List obserVers;Subject()obserVers = new ArrayList();public abstract void attachObserve

温馨提示

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

评论

0/150

提交评论