软件设计原则简介(第一部分)_第1页
软件设计原则简介(第一部分)_第2页
软件设计原则简介(第一部分)_第3页
软件设计原则简介(第一部分)_第4页
软件设计原则简介(第一部分)_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

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

文档简介

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 34Software Design Principle An IntroductionPart OneDr. 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 34lI

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 34lIntroductionlSingle 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 34lSOLIDSingle responsibility principle /wiki/Single_responsibility_principleOpen/closed principle /wiki/Open/closed_principleLiscov substitution principle /wiki/Liskov_substitution_principleInterface segregation principle en.wikipedi

7、/wiki/Interface_segregation_principle Dependency inversion principle /wiki/Dependency_inversion_principlelIntroduced by Robert C. Martin in the early 2000s Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou

8、, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 5 of 34lManifesto for Agile Software DIndividuals and interactions over processes and tools.Working software over comprehensive documentation.Customer collaboration

9、over contract negotiation.Responding to change over following a plan.lPrinciples behind the Agile M/principles.htmlwelcome changing requirements, even late in developmentcontinuous attention to technical excellence andgood design enhances agility Department of Computer Scie

10、nceDepartment 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 34 Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer Scienc

11、eSun 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 34lLaw of Demeter (LoD) /home/lieber/LoD.html /wiki/Law_of_Demeter It is so named for its origin in the Demeter

12、 Projectlan adaptive programming and aspect-oriented programming effort The guideline was invented at Northeastern University towards the end of 1987 lDemeterGoddess of the Earth, Agriculture, Harvest, and Forests Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer Sci

13、enceSun 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 34lcohesionlcouplinglabstractionlencapsulationlinheritancelpolymorphism Department of Computer ScienceDepartment of Computer ScienceDepartment of Compu

14、ter 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 34lCohesion /wiki/Cohesion_(computer_science)A measure of how strongly-related and focused the various responsibilities of a sof

15、tware module are.Modules with high cohesion tend to be preferable lhigh cohesion is associated with several desirable traits of software including robustness, reliability, reusability, and understandability llow cohesion is associated with undesirable traits such as being difficult to maintain, diff

16、icult to test, difficult to reuse, and even difficult to understand. 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

17、0 of 34lCoupling /wiki/Coupling_(computer_science)The degree to which each program module relies on each one of the other modules.Low coupling refers to a relationship in which one module interacts with another module through a stable interface and does not need to be concerned with

18、the other modules internal implementationlWith low coupling, a change in one module will not require a change in the implementation of another module. lLow coupling is often a sign of a well-structured computer system, and when combined with high cohesion, supports the general goals of high readabil

19、ity and maintainability. 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 11 of 34lAbstraction /wiki/Ab

20、straction_(computer_science) A mechanism and practice to reduce and factor out details so that one can focus on a few concepts at a time. Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Gua

21、ngzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 12 of 34lEncapsulation /wiki/Encapsulation_(computer_science)The hiding of the internal mechanisms and data structures of a software component behind a defined interfacelUsers of the component (other pieces of softwa

22、re) only need to know what the component does, and cannot make themselves dependent on the details of how it does it. 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 Su

23、n Yat-sen University, Guangzhou, P.R.China Page 13 of 34lInheritance /wiki/Inheritance_(computer_science)A way to form new classes (instances of which are called objects) using classes that have already been defined. Department of Computer ScienceDepartment of Computer ScienceDepartm

24、ent 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 34lPolymorphism /wiki/Polymorphism_in_object-oriented_programming The ability of one type, A, to appear as and be u

25、sed like another type, B lIn strongly typed languages, this usually means that type A somehow derives from type B, or type A implements an interface that represents type B. Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.

26、R.China Sun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 15 of 34lIntroductionlSingle Responsibility Principle (SRP)lOpen Closed Principle (OCP) lLiskov Substitution Principle (LSP)lDependency Inversion Principle (DIP)lInterface Segregation Principle (IS

27、P)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 16 of 34ldefine a responsibility to

28、 be “a reason for change.”more than one motive for changing a class, then that class has more than one responsibility 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 Su

29、n Yat-sen University, Guangzhou, P.R.China Page 17 of 34 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 18 of 34 Depa

30、rtment 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 19 of 34lWe are accustomed to thinking of responsibility in groups Departm

31、ent 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 34 Department of Computer ScienceDepartment of Computer ScienceDepartme

32、nt 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 34lInterface has only one responsibilitylBut, class does not need to be Department of Computer ScienceDepartment of Computer Science

33、Department 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 22 of 34code is smallereasier to readeasier to understandeasier to maintaincode is potentially easier to testchange is easier to m

34、anagecode is easier to replacecontention may be reduced for source code files among multiple developersdeployment may be reduced Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P

35、.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 23 of 34recognize the class as the software building blockmake classes whichdo one thing or change for only one reasonare smallare easy to read and understandare easy to maintainare potentially easy to testare easy to replaceensure high cohe

36、sioneach member should directly relate to the class nameremember that its a principle, not a rule;use your professional skills and experience Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.China Sun Yat-sen University,

37、 Guangzhou, P.R.China Sun Yat-sen University, Guangzhou, P.R.China Page 24 of 34lIntroductionlSingle Responsibility Principle (SRP)lOpen Closed Principle (OCP) lLiskov Substitution Principle (LSP)lDependency Inversion Principle (DIP)lInterface Segregation Principle (ISP)lLeast Knowledge Principle (L

38、KP) 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 25 of 34lAll systems change during their life cycles This must be

39、borne in mind when developing systems expected to last longer than the first version 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,

40、P.R.China Page 26 of 34lModules that conform to the open-closed principle have two primary attributesThey are “Open For Extension”lwe can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.They are “Closed for Modi

41、fication”lNo one is allowed to make source code changes to it.lAbstraction is the Key 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,

42、 P.R.China Page 27 of 34 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 28 of 34 Department of Computer ScienceDepart

43、ment 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 29 of 34lNo significant program can be 100% closedthere will always be some kind of change against which i

44、t is not closedlSince closure cannot be complete, it must be strategicThe designer must choose the kinds of changes against which to close his designThe experienced designer judge the probability of different kinds of changes. lHe then makes sure that the open-closed principle is invoked for the mos

45、t probable changes 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 34Using Abstraction to Gain Explicit Closure

46、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 31 of 34lMake all Member Variables PrivatelNo Global Variables EverlRun time type identification (RTTI) is Dangerousif a use of RTTI does not violate the open-closed principle, it is safe Department of Computer ScienceDepartment of Computer ScienceDepartment of Computer ScienceSun Yat-sen University, Guangzhou, P.R.

温馨提示

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

评论

0/150

提交评论