版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、 Ch9 Design Patterns(1)Programming to InterfacesOCPSoftware System Design and ArchitectureMain ContentsDesign patternsProgramming to InterfacesOCPWhat is a Design Pattern?A design patternabstracts a recurring design structurecomprises class and/or objectdependencies, structures, interactions, orconv
2、entions distills design experienceElements of Design PatternsDesign patterns have 4 essential elements:Pattern name: increases vocabulary of designersProblem: intent, context, when to apply Solution: UML-like structure, abstract codeConsequences: results and tradeoffsMain ContentsDesign patternsProg
3、ramming to InterfacesOCPProgramming to Interfaces, not Implementations“Connections that address or refer to a module as a whole by its name yield lower coupling than connections referring to the internal elements of another module”Single values VS a collection of valuesTake Care Friend methods and R
4、eferencesShare Data !Iterator PatternIntent Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.Supports multiple traversals of aggregate objects.Provides a uniform interface for traversing different aggregate structures (it support
5、s polymorphic iteration).Abstraction Mechanisms in CLU, Liskov, 1977createIterator( )Iterator PatternStructure/client codeAggregate myList = new ConcreteAggregate( );Iterator itr = myList.createIterator( );Iterator PatternConsequencesProgramming to interfaces and Information HidingIt supports variat
6、ions in the traversal of an aggregate. For example, code generation may traverse the parse tree inorder or preorder. Iterators make it easy to change the traversal. Just replace the iterator instance with a different one.Iterators simplify the Aggregate interface. Iterators traversal interface obvia
7、tes the need for a similar interface in Aggregate.More than one traversal can be pending on an aggregate.11ClientSubjectabstractRequest()RealSubjectRequest()ProxyRequest()refers to-Request()The Proxy Pattern: SolutionThe Proxy PatternThe ProblemProvide a surrogate or placeholder for another object t
8、o control access to itRemote Proxy: provide local representative for object in different address space, may re-code request.Virtual Proxy: creates expensive objects on demand, like in example, may also cache.Protection Proxy: control access to original object, useful when access rights differ.The Pr
9、oxy Pattern ConsequencesThe Proxy pattern introduces a level of indirection when accessing an object:A remote proxy can hide the fact that the object resides in a different address spaceA virtual proxy can perform optimisationsAllow additional housekeepingPrototype PatternClassification Class , Prot
10、otype ClassSpecify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototypePrototype Pattern UMLParticipants: Prototypeo declares an interface for cloning itself. ConcretePrototype o implements an operation for cloning itself. Cliento creates a n
11、ew object by asking a prototype to clone itself.Lets Try!思考題有一個(gè)數(shù)據(jù)列表DataList,其基本類(lèi)型是3維向量ThreeD1)用Java語(yǔ)言實(shí)現(xiàn)該數(shù)據(jù)列表的數(shù)據(jù)結(jié)構(gòu)假設(shè)有三個(gè)外部對(duì)象A,B,C,分別對(duì)其x、y、z維度感興趣,希望訪(fǎng)問(wèn)DataList在相應(yīng)維度的數(shù)據(jù)并進(jìn)行處理2)請(qǐng)定義其對(duì)外的數(shù)據(jù)接口分別使用迭代器與ProxyMain ContentsDesign patternsProgramming to InterfacesOCPOCPFunction of InheritanceIncremental developmentD
12、ata Abstraction and Hierarchy, LiskovModify closed, increment easierOn the Notion of Inheritance,ANTERO TAIVALSAARIAfter 1990s, requirements changed become criticalOCP (depend DIP)Open/Closed Principle (OCP)Software entities should be open for extension, but closed for modificationB. Meyer, 1988 / q
13、uoted by R. Martin, 1996Be open for extensionmodules behavior can be extendedBe closed for modificationsource code for the module must not be changesThere is no 100% OCP!A Simple Example of OCPCopyReadKeyboardWritePrintervoid Copy(ReadKeyboard& r, WritePrinter& w) int c; while (c = r.read () != EOF)
14、 w.write (c);WriteDiskenum OutputDevice printer, disk; void Copy(ReadKeyboard& r, WritePrinter& wp, WriteDisk& wd, OutputDevice dev) int c; while(c = r.read()!= EOF) if(dev = printer) wp.write(c); else wd.write (c);A Simple Example of OCPDiskWriter:Write(c) WriteDisk(c);CopyKeyboardReaderPrinterWrit
15、erDiskWritervoid Copy(ReadKeyboard& r, WritePrinter& w) int c; while (c = r.read () != EOF) w.write (c);思考題Extension:Border( Plain, 3D, Fancy)ScrollBar( Horiz, Vert).VisualComponentdraw()resize()TextViewdraw()resize()SteamedVideoViewdraw()resize()24The Decorator Pattern: ProblemsDecorators expand th
16、e functionality of an instance of a class without changing the class code Add borders or scrollbars to a GUI componentAdding items such as scroll bars as needed provides more flexibility than requiring all windows to have scroll barsAdd headers and footers to an advertisementAdd stream functionality
17、 such as reading a line of input or compressing a file before sending it over the wireMore flexibility than static inheritanceDecorators work behind the scenes, they are transparent to the interfaceThe Decorator Pattern: The SolutionVisualComponentdraw()resize()TextViewdraw()resize()Borderdraw()resi
18、ze()Decoratordraw()resize()ScrollBardraw()resize()SteamedVideoViewdraw()resize()11Plaindraw()resize()3Ddraw()resize()Fancydraw()resize()Decorator contains a visual componentHorizdraw()resize()Vertdraw()resize()27The Decorator Pattern: ConsequencesMore flexible than static inheritanceAvoids feature laden classes high up in hierarchyLo
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024-2030年雙氧水市場(chǎng)發(fā)展現(xiàn)狀調(diào)查及供需格局分析預(yù)測(cè)報(bào)告
- 2024-2030年衛(wèi)衣市場(chǎng)發(fā)展現(xiàn)狀調(diào)查及供需格局分析預(yù)測(cè)報(bào)告
- 2024-2030年醫(yī)藥輔料行業(yè)并購(gòu)重組機(jī)會(huì)及投融資戰(zhàn)略研究咨詢(xún)報(bào)告
- 2024-2030年醫(yī)療耗材行業(yè)發(fā)展分析及投資戰(zhàn)略研究報(bào)告
- 2024-2030年醫(yī)用堿石灰行業(yè)市場(chǎng)現(xiàn)狀供需分析及投資評(píng)估規(guī)劃分析研究報(bào)告
- 2024-2030年包裝塑料行業(yè)市場(chǎng)發(fā)展分析及前景趨勢(shì)與投融資發(fā)展機(jī)會(huì)研究報(bào)告
- 2024-2030年冷鏈行業(yè)市場(chǎng)深度分析及競(jìng)爭(zhēng)格局與投資潛力研究報(bào)告
- 2024-2030年農(nóng)林工程產(chǎn)業(yè)市場(chǎng)發(fā)展分析及發(fā)展趨勢(shì)與投資研究報(bào)告
- 2024-2030年農(nóng)業(yè)機(jī)械服務(wù)行業(yè)市場(chǎng)發(fā)展分析及前景趨勢(shì)與投融資戰(zhàn)略研究報(bào)告
- 2024-2030年再生PET行業(yè)市場(chǎng)現(xiàn)狀供需分析及投資評(píng)估規(guī)劃分析研究報(bào)告
- 精神分裂癥診療
- 術(shù)前禁食禁飲指南專(zhuān)家講座
- 小兒骶管阻滯
- 授三皈五戒儀規(guī)
- 醫(yī)療質(zhì)量管理辦法及核心制度考試試題
- 壓瘡試題答案
- 指紋密碼鎖使用說(shuō)明書(shū)
- GB/T 36876-2018中小學(xué)校普通教室照明設(shè)計(jì)安裝衛(wèi)生要求
- GB/T 28827.2-2012信息技術(shù)服務(wù)運(yùn)行維護(hù)第2部分:交付規(guī)范
- “兩山”轉(zhuǎn)化中心(“兩山銀行”)建設(shè)工作方案
- 公文格式(全區(qū))課件
評(píng)論
0/150
提交評(píng)論