




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、 Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 1 of 48Software Design Principle An IntroductionPart TwoDr. ZHOU Xiao
2、-http:/ of Computer ScienceSUN YAT-SEN UNIVERSITY, GZ 510275 Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 2 of 48lI
3、ntroductionlSingle Responsibility Principle (SRP)lOpen Closed Principle (OCP) lLiskov Substitution Principle (LSP)lDependency Inversion Principle (DIP)lInterface Segregation Principle (ISP)lLeast Knowledge Principle (LKP) Department of Computer ScienceDepartment of Computer ScienceDepartment of Comp
4、uter ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 3 of 48lIntroductionlSingle Responsibility Principle (SRP)lOpen Closed Principle (OCP) lLiskov Substitution Principle (LSP)lDependency Inversion Pri
5、nciple (DIP)lInterface Segregation Principle (ISP)lLeast Knowledge Principle (LKP) Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.
6、R.China Page 4 of 48lThe first woman in the United States to be awarded a Ph.D. from a computer science departmentIn 1968, Stanford University lShe received 2008 Turing Award from the ACMfor her work in the design of programming languages and software methodology that led to the development of objec
7、t-oriented programming ACM: Association for Computing MachineryThe Turing Award lThe highest distinction in Computer Science lThe Nobel Prize of computing Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-se
8、n University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 5 of 48lDesign by contractlBehavioral subtypingLet q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T. Department of Computer ScienceDep
9、artment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 6 of 48lA subtype is a datatype that is related to another datatype (the supertype) by some notion of s
10、ubstitutabilityProgram constructs written to operate on elements of the supertype can also operate on elements of the subtypelTypical program construct: subroutines, functions, methodsPolymorphism in object-oriented programming languagelSubtyping relation is a relation on typesOn specification level
11、More abstractly, on concepts Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 7 of 48lA subclass is a class that inheri
12、ts some properties from its superclassA subclass is an extension of its superclassA subclass reuses the properties or services of its superclass lSubclass relation is a relation on implementation levelsuperclass, base class, or parent classchild classes, derived classes, or subclasses Department of
13、Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 8 of 48lstandard requirements on signaturesContravariance of method arguments in the
14、 subtypeCovariance of return types in the subtypeNo new exceptions should be thrown by methods of the subtypelexcept where those exceptions are themselves subtypes of exceptions thrown by the methods of the supertypeThe members of derived classes must expect no more than the corresponding members of
15、 the base class; and should promise no less Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 9 of 48lBehavioral conditi
16、ons that subtype must meetPreconditions cannot be strengthened in a subtypePostconditions cannot be weakened in a subtypeInvariants of the supertype must be preserved in a subtypeHistory constraint: the history properties of the supertype must be preseved in a subtypelHistory properties are properti
17、es true of all sequences of states e.g. the bound of an array does not change Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.Chi
18、na Page 10 of 48lThe square-rectangle problemAlso circle-ellipse problem/wiki/Circle-ellipse_problemA square is a rectangle ?lThe square class inherits the rectangle class will lead to many problems! Department of Computer ScienceDepartment of Computer ScienceDepartment of Comp
19、uter ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 11 of 48lbenefitsdesign is more flexiblela base type can be confidently substituted for a derived typecode is potentially easier to testrequired to
20、support the Open/Closed Principlelcall to actionensure thatlthe base type can be substituted for a derived typelderived types are substitutable for their base typeavoid runtime type checking Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen Univers
21、ity, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 12 of 48lIntroductionlSingle Responsibility Principle (SRP)lOpen Closed Principle (OCP) lLiskov Substitution Principle (LSP)lDependency Inversion Principle (DIP)lInterface Segrega
22、tion Principle (ISP)lLeast Knowledge Principle (LKP) Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 13 of 48lRigidity
23、It is hard to change because every change affects too many other parts of the systemVs. flexiblelFragilityWhen you make a change, unexpected parts of the system breakVs. robustlImmobilityIt is hard to reuse in another application because it cannot be disentangled from the current applicationVs. reus
24、able Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 14 of 48lThe interdependence of the modules makes a design rigid,
25、 fragile, immobile Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 15 of 48 Department of Computer ScienceDepartment o
26、f Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 16 of 48lThe module containing the high level policy, i.e. the Copy() module, is dependent upon the low level de
27、tailed modules that it controls. (i.e. WritePrinter() and ReadKeyboard()lIf we could find a way to make the Copy() module independent of the details that it controls, then we could reuse it freely Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen U
28、niversity, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 17 of 48 Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guang
29、zhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 18 of 48lMore traditional software development methods tend to create software structures in which high level modules depend upon low level modules, and in which abstractions depend upon detailsone of the goals of these methods is to
30、define the subprogram hierarchy that describes how the high level modules make calls to the low level modules Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-se
31、n University, Guangzhou, P.R.China Page 19 of 48 Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 20 of 48 Department o
32、f Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 21 of 48 Department of Computer ScienceDepartment of Computer ScienceDepartment of
33、 Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 22 of 48lFinding the underlying abstractionWhat is the high level policy?lIt is the abstractions that underlie the application, the truths that
34、 do not vary when the details are changedlIn the Button/Lamp example, the underlying abstraction is to detect an on/off gesture from a user and relay that gesture to a target object What mechanism is used to detect the user gesture? Irrelevant! What is the target object? Irrelevant! These are detail
35、s that do not impact the abstraction Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 23 of 48 Department of Computer S
36、cienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 24 of 48 Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer Sc
37、ienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 25 of 48lThe high level policy is entirely captured within the abstract button classThe Button class knows nothing of the physical mechanism for detecting
38、the users gesturesIt knows nothing at all about the lampThose details are isolated within the concrete derivatives: ButtonImplementation and LamplThe high level policy in Listing 6 is reusable with any kind of button, and with any kind of device that needs to be controlledit is not affected by chang
39、es to the low level mechanisms Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 26 of 48change is easier to managecode
40、is easier to replacedesign is extensiblecode is potentially easier to testrequired to support the Open/Closed Principle Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China
41、Sun Yat-sen University, Guangzhou, P.R.China Page 27 of 48create abstractionsdepend on abstractionsdeclare parameters and variables as an interface or abstract classderive only from abstract classesoverride only abstract members Department of Computer ScienceDepartment of Computer ScienceDepartment
42、of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 28 of 48lIntroductionlSingle Responsibility Principle (SRP)lOpen Closed Principle (OCP) lLiskov Substitution Principle (LSP)lDependency Inver
43、sion Principle (DIP)lInterface Segregation Principle (ISP)lLeast Knowledge Principle (LKP) Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guang
44、zhou, P.R.China Page 29 of 48lOnce an interface has gotten too fat it needs to be split into smaller and more specific interfaces so that any clients of the interface will only know about the methods that pertain to themRole InterfacelMartin Fowlers name for smaller interfaces which only include the
45、 needed methods Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 30 of 48 Department of Computer ScienceDepartment of C
46、omputer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 31 of 48lThe interface of Door has been polluted with an interface that it does not require.The Door class now depe
47、nds upon TimerClientlNot all varieties of Door need timing.It has been forced to incorporate this interface solely for the benefit of one of its subclasseslIf this practice is pursued, then every time a derivative needs a new interface, that interface will be added to the base class.This will furthe
48、r pollute the interface of the base class, making it “fat”. Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 32 of 48lS
49、ome users of Timer will register more than one timeout requestWe include a unique timeOutId code in each timeout registration Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.
50、China Sun Yat-sen University, Guangzhou, P.R.China Page 33 of 48lClearly this change will affect all the users of TimerClientHowever, the design in Figure 1 will also cause Door, and all clients of Door to be affected (i.e. at least recompiled) by this fix!Why should a bug in TimerClient have any af
51、fect on clients of Door derivatives that do not require timing?lWhen a change in one part of the program affects other completely unrelated parts of the programthe cost and repercussions of changes become unpredictable; and the risk of fallout from the change increases dramatically. Department of Co
52、mputer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 34 of 48Separation through delegation (1) Department of Computer ScienceDepartment of
53、Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 35 of 48Separation through delegation (2) Department of Computer ScienceDepartment of Computer ScienceDepartment o
54、f Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 36 of 48lbenefitscohesion is increasedlclients can demand cohesive interfacesdesign is more stablelchanges are isolated and do not cascade thr
55、oughout the codesupports the Liskov Substitution Principlelcall to actionconsider the needs of the client when designing the interfacecreate client-specific interfaces Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.Chi
56、na Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 37 of 48lIntroductionlSingle Responsibility Principle (SRP)lOpen Closed Principle (OCP) lLiskov Substitution Principle (LSP)lDependency Inversion Principle (DIP)lInterface Segregation Principle (ISP)lLe
57、ast Knowledge Principle (LKP) Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 38 of 48lLoD is a specific case of loose
58、 couplingEach unit should have only limited knowledge about other units: only units closely related to the current unit. Each unit should only talk to its friends; dont talk to strangers. Only talk to your immediate friends. Department of Computer ScienceDepartment of Computer ScienceDepartment of C
59、omputer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 39 of 48 Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China S
60、un Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 40 of 48 Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen Unive
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 菜攤租賃合同范本
- 股東增資擴(kuò)股合同范本
- MRX343-生命科學(xué)試劑-MCE
- Cloniprazepam-生命科學(xué)試劑-MCE
- BP-Fluor-568-NHS-ester-生命科學(xué)試劑-MCE
- Alfalone-生命科學(xué)試劑-MCE
- 5-Bromo-1-methyl-1H-indazole-d3-生命科學(xué)試劑-MCE
- 4-1-Piperazinyl-1H-Indole-hydrochloride-4-Piperazinoindole-hydrochloride-生命科學(xué)試劑-MCE
- 飼料合同范本
- 2025年煤焦化產(chǎn)品合作協(xié)議書
- 綜合應(yīng)用能力事業(yè)單位考試(綜合管理類A類)試題及解答參考
- 阿爾茲海默病的家庭護(hù)理
- bim技術(shù)課件教學(xué)課件
- 腹水形成的原因及治療
- 單晶爐車間安全培訓(xùn)
- 高中地理必修第一冊(cè)期末試卷及答案-中圖版-2024-2025學(xué)年
- 護(hù)理核心制度測試題+參考答案
- 機(jī)械制造技術(shù)基礎(chǔ)(課程課件完整版)
- 《2023版CSCO卵巢癌診療指南》解讀課件
- 【醫(yī)院藥品管理系統(tǒng)探析與設(shè)計(jì)(論文)10000字】
- 螺旋體病梅毒課件
評(píng)論
0/150
提交評(píng)論