版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年工程税收与结算合同
- 2024年度电竞游戏开发与发行合同
- 2024年丙方法律咨询与代理合同
- 2024年应急出口指示牌制作安装合同
- 2024年城市道路泥水施工合同
- 2024年建筑工程所需材料采购协议
- 2024年度无人机制造与销售合同
- 2024园林绿化工程绿化带规划与设计合同
- 2024腾讯朋友圈广告合同
- 2024年度医院医疗设备采购与安装合同
- 口腔常见疾病的诊治
- MOOC 人像摄影-中国传媒大学 中国大学慕课答案
- MOOC 计算机组成原理-电子科技大学 中国大学慕课答案
- 2024年江苏无锡市江阴市江南水务股份有限公司招聘笔试参考题库含答案解析
- 中学教材、教辅征订管理制度
- (高清版)DZT 0213-2002 冶金、化工石灰岩及白云岩、水泥原料矿产地质勘查规范
- 消防安全评估消防安全评估方案
- 工程造价专业《工程经济》课程标准
- ZARA服装市场营销策略研究分析 市场营销专业
- 设备维保的市场化运作与服务模式创新
- 幼儿园科普知识宣传
评论
0/150
提交评论