




已阅读5页,还剩29页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第八章装饰模式 2020 1 5 1 装饰模式 别名 包装器 动态地给对象添加一些额外的职责 就功能来说装饰模式相比生成子类更为灵活 DecoratorPattern AnotherName Wrapper Attachadditionalresponsibilitiestoanobjectdynamically Decoratorsprovideaflexiblealternativetosubclassingforextendingfunctionality 一 概述 2020 1 5 3 装饰模式是动态地扩展一个对象的功能 而不需要改变原始类代码的一种成熟模式 在装饰模式中 具体组件 类和 具体装饰 类是该模式中的最重要的两个角色 二 装饰模式模式的结构与使用 2020 1 5 4 装饰模式的结构中包括四种角色 抽象组件 Component 具体组件 ConcreteComponent 装饰 Decorator 具体装饰 ConcreteDecotator 2020 1 5 5 装饰模式的UML类图 装饰模式的结构的描述与使用 假设系统中有一个Bird抽象类以及Bird类的一个子类Sparrow Sparrow类实现了Bird类的fly方法 使得Sparrow类创建的对象 麻雀 调用fly方法能连续飞行100米 现在 用户需要两只鸟 无论哪种鸟都可以 但必须分别能连续飞行150米和200米 2020 1 5 7 1 抽象组件 Bird javapublicabstractclassBird publicabstractintfly 2020 1 5 8 2 具体组件 Sparrow javapublicclassSparrowextendsBird publicfinalintDISTANCE 100 publicintfly returnDISTANCE 2020 1 5 9 3 装饰 Decorator Decorator javapublicabstractclassDecoratorextendsBird protectedBirdbird publicDecorator publicDecorator Birdbird this bird bird 2020 1 5 10 4 具体装饰 ConcreteDecotator SparrowDecorator javapublicclassSparrowDecoratorextendsDecorator publicfinalintDISTANCE 50 eleFly方法能飞50米SparrowDecorator Birdbird super bird publicintfly intdistance 0 distance bird fly eleFly returndistance privateinteleFly 装饰者新添加的方法returnDISTANCE 2020 1 5 11 5 应用Application javapublicclassApplication publicvoidneedBird Birdbird intflyDistance bird fly System out println 这只鸟能飞行 flyDistance 米 publicstaticvoidmain Stringargs Applicationclient newApplication Birdsparrow newSparrow BirdsparrowDecorator1 newSparrowDecorator sparrow BirdsparrowDecorator2 newSparrowDecorator sparrowDecorator1 client needBird sparrowDecorator1 client needBird sparrowDecorator2 三 装饰模式的优点 2020 1 5 12 被装饰者和装饰者是松耦合关系 由于装饰 Decorator 仅仅依赖于抽象组件 Component 因此具体装饰只知道它要装饰的对象是抽象组件的某一个子类的实例 但不需要知道是哪一个具体子类 装饰模式满足 开 闭原则 不必修改具体组件 就可以增加新的针对该具体组件的具体装饰 可以使用多个具体装饰来装饰具体组件的实例 四 JavaIO与装饰模式 Java io包的设计使用了装饰模式 Reader FileReader BufferedReader类的关系符合装饰模式的结构 publicfinalclassAccessTextFile 1 演示将流中的文本读入一个StringBuffer中 throwsIOException publicvoidreadToBuffer StringBufferbuffer InputStreamis throwsIOException Stringline 用来保存每行读取的内容BufferedReaderreader newBufferedReader newInputStreamReader is line reader readLine 读取第一行while line null 如果line为空说明读完了buffer append line 将读到的内容添加到buffer中buffer append n 添加换行符line reader readLine 读取下一行 2 演示将StringBuffer中的内容读出到流中 publicvoidwriteFromBuffer StringBufferbuffer OutputStreamos 用PrintStream可以方便的把内容输出到输出流中 其对象的用法和System out一样 System out本身就是PrintStream对象 PrintStreamps newPrintStream os ps print buffer toString 3 调用copyStream InputStream OutputStream 方法拷贝文本文件 publicvoidcopyTextFile StringinFilename StringoutFilename throwsIOException 先根据输入 输出文件生成相应的输入 输出流InputStreamis newFileInputStream inFilename OutputStreamos newFileOutputStream outFilename copyStream is os 用copyStream拷贝内容is close is是在这里打开的 所以需要关闭os close os是在这里打开的 所以需要关闭 publicstaticvoidmain String args throwsIOException intsw 1 三种测试的选择开关AccessTextFiletest newAccessTextFile switch sw case1 测试读 InputStreamis newFileInputStream E test txt StringBufferbuffer newStringBuffer test readToBuffer buffer is System out println buffer 将读到buffer中的内容写出来is close break case2 测试写 StringBufferbuffer newStringBuffer Onlyatest n test writeFromBuffer buffer System out break case3 测试拷贝 test copyTextFile E test txt E r txt break 五 应用举例 读取单词表 Word txtarrangeexampleintelligence chinese txt整理例子智力 englishSentence txtarrangetoolsinorderciteanexampleinpointanintelligencetest ReadWord javaimportjava io importjava util ArrayList publicabstractclassReadWord publicabstractArrayListreadWord Filefile ReadEnglishiWord javaimportjava io importjava util ArrayList publicclassReadEnglishiWordextendsReadWord publicArrayListreadWord Filefile ArrayListwordList newArrayList try FileReaderinOne newFileReader file BufferedReaderinTwo newBufferedReader inOne Strings null while s inTwo readLine null wordList add s inTwo close inOne close catch IOExceptionexp System out println exp returnwordList Decorator javapublicabstractclassDecoratorextendsReadWord protectedReadWordreader publicDecorator publicDecorator ReadWordreader this reader reader WordDecorator javapublicclassWordDecoratorextendsDecorator FiledecoratorFile WordDecorator ReadWordreader FiledecoratorFile super reader this decoratorFile decoratorFile publicArrayListreadWord Filefile ArrayListwordList reader readWord file try FileReaderinOne newFileReader decoratorFile BufferedReaderinTwo newBufferedReader inOne Strings null intm 0 while s inTwo readLine null Stringword wordList get m word word concat s wordList set m word m if m wordList size break inTwo close inOne close catch IOExceptionexp System out println exp returnwordList importjava util ArrayList importjava io File publicclassApplication publicstaticvoidmain Stringargs ArrayListwordList newArrayList ReadEnglishiWordREW newReadEnglishiWord WordDecoratorWD1 newWordDecorator REW newFile chinese txt ReadWordreader WD1 wordList reader readWord newFile word txt for inti 0 i wordList size i System out println wordList get i WordDecoratorWD2 newWordDecorator WD1 newFile englishSentence txt reader WD2 wordList reader readWord newFile word txt for inti 0 i wordList size i System out println wordList get i 六 应用举例 二 publicabstractclassDecorator Themethodplaceseachdecorativeitem onthetree publicabstractvoidplace Branchbranch publicclassChristmasTree privateBranchbranch publicBranchgetBranch returnbranch publicclassBallDecoratorextendsDecorator DefaultConstructorpublicBallDecorator ChristmasTreetree Branchbranch tree getBranch place branch Themethodplaceseachdecorativeitem onthetree publicvoidplace Branchbranch branch put ball BallDecoratordecorator newBallDecorator newChristmasTree 七 应用举例三 对输出的名
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 塑造健康生活模式
- 手工艺术探秘
- 透析过程中血压管理
- 保持呼吸道通畅的护理措施
- 肺炎拔管后护理要点
- 世界卫生日健康教育
- 关节镜ACL术后护理
- 2025年造纸黑液碱回收成套装置项目立项申请报告模板
- 2025关于租赁合同的诉讼时效
- 中医外科学教案
- 2025人工智能工程师笔试题及答案
- 语文中考文学类文本托物言志专题复习教学设计
- 安徽省合肥市2025届高三下学期5月教学质量检测(三模)英语试卷(含音频)
- 贵州国企招聘2025贵州乌江煤层气勘探开发有限公司招聘16人笔试参考题库附带答案详解
- 放射科出科试题 及答案
- 炊事员培训试题及答案
- 浙能镇海联合发电公司燃机异地迁建改造项目环评报告
- 办公大楼保安试题及答案
- 新一代大型机场行李处理系统关键技术与应用
- 铁路电务设备培训课件
- 全国100所名校2025届高考冲刺模拟英语试题含答案
评论
0/150
提交评论