1、Design Patterns - OO design Principle,Foglight Zhuhai Dashboard Team,Agenda,What is Object-oriented design principle Object-oriented design principles Open-Closed Principle (OCP) Single Responsibility Principle (SRP) Liskov Substitution Principle (LSP) Interface Segregation Principle (ISP) Dependenc
2、y Inversion Principle (DIP) References,What is Object-oriented design principle,Object-oriented design is the process of planning a system of interacting objects for the purpose of solving a software problem.The experience and regularity when we use object-oriented design which can help programmer t
3、o create a system that is easy to maintain and extend over time.,Object-oriented design principles(1),Five principles of class design: Open-Closed Principle (OCP: 开合原则) Single Responsibility Principle (SRP: 单职原则) Liskov Substitution Principle (LSP: 里氏置换原则) Interface Segregation Principle (ISP: 接口隔离原
4、则) Dependency Inversion Principle (DIP: 依赖倒置原则),Object-oriented design principles(2),Three principles of package cohesion: Reuse Release Equivalence Principle (REP: 重用发布等价原则) Common Closure Principle (CCP: 公共封闭原则) Common Reuse Principle (CRP: 公共复用原则)Three principles of package coupling : Acyclic Dep
5、endencies Principle (ADP: 无环依赖原则) Stable Dependencies Principle (SDP: 稳定依赖原则) Stable Abstractions Principle (SAP: 稳定抽象原则),Object-oriented design principles,Open-Closed Principle (OCP) Single Responsibility Principle (SRP) Liskov Substitution Principle (LSP) Interface Segregation Principle (ISP) Depe
6、ndency Inversion Principle (DIP),Open-Closed Principle (OCP),Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification Abstraction is the Key Encapsulate the Variation Inheritance and aggregationIs-A VS Has-A,Example violated OCP,class Shapestrin
7、g type (“circle”, “square”, ); Void drawAllShapes(List Shapes)for(Shape shape : Shapes)if(shape.type = “circle”)drawCircle(shape); else if(shape.type = “square”) drawSquare(shape); ,Refactor it to follow OCP,Interface Shapepbulic void draw(); class Circle implements Shape class Square implements Sha
8、pe Void drawAllShapes(List Shapes)for(Shape shape : Shapes)shape.draw(); ,Bad Example in DME project,Get Virtual Machines from Topology Objects,Object-oriented design principles,Open-Closed Principle (OCP) Single Responsibility Principle (SRP) Liskov Substitution Principle (LSP) Interface Segregatio
9、n Principle (ISP) Dependency Inversion Principle (DIP),Single Responsibility Principle (SRP),Every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class A responsibility as a reason to change, and concludes that a class or module should have
10、one, and only one, reason to change,Example violated SRP,Refactor it to follow SRP,Another example about SRP,interface Modem public void dial(String phoneNO);public void hangup();public void send(String c);public String receive(); ,Bad example in DME project,com.quest.dme.capacity.data.DataRetriver,
11、Object-oriented design principles,Open-Closed Principle (OCP) Single Responsibility Principle (SRP) Liskov Substitution Principle (LSP) Interface Segregation Principle (ISP) Dependency Inversion Principle (DIP),Liskov Substitution Principle (LSP),If S is a subtype of T, then objects of type T may be
12、 replaced with objects of type S without altering any of the desirable properties of that. Subtype must be substituted for its base type,Example violated LSP(1),class Rectangle private double width;private double height;void setWidth(double w) width=w;void setHeight(double h) height=h;double getHeig
13、ht() return height;double getWidth() return width; ;class Square extends Rectanglevoid setWidth(double w) super.setWidth(w); super.setHeight(w)void setHeight(double h) super.setWidth(h); super.setHeight(h) ,Example violated LSP(2),double arear.setWidth(5);r.setHeight(4);assert(r.getWidth() * r.getHe
14、ight() = 20); ,How to follow LSP,Contravariance of method arguments in the subtype. Covariance of return types in the subtype. No new exceptions should be thrown by methods of the subtype Preconditions cannot be strengthened in a subtype. Postconditions cannot be weakened in a subtype. Invariants of
15、 the supertype must be preserved in a subtype.,Object-oriented design principles,Open-Closed Principle (OCP) Single Responsibility Principle (SRP) Liskov Substitution Principle (LSP) Interface Segregation Principle (ISP) Dependency Inversion Principle (DIP),Interface Segregation Principle (ISP),Spli
16、ts too large interfaces into smaller and more specific ones so that any client it will only know about the methods that are of interest. No client should be forced to depend on methods it does not use. Separate Clients mean Separate Interfaces,ATM Example violated ISP,Refactor it to follow ISP,Bad e
17、xample in DME project,com.quest.dme.hyperv.capacity.calculator.retriever.DataRetriver,Object-oriented design principles,Open-Closed Principle (OCP) Single Responsibility Principle (SRP) Liskov Substitution Principle (LSP) Interface Segregation Principle (ISP) Dependency Inversion Principle (DIP),Dep
18、endency Inversion Principle (DIP),A. High-level modules should not depend on low-level modules. Both should depend on abstractions. B. Abstractions should not depend upon details. Details should depend upon abstractions.,Example violated DIP,Refactor it to follow DIP,How to follow DIP,Any variable s
19、hould not hold a reference point to the specific class. Any class should not be derived from the concrete class. Any method should not override the method which has already been implemented in base class.,Bad example in DME project,com.quest.dme.scan.model.action.Action,References,Agile Software Development, Principles, Patterns, and Practices (敏捷软件开发:原则、模式与实践) Design Patterns: Elements of Reusable Object-Oriented Software(设计模式-可复用面向对象软件的基础) Head First Design Patterns(深入浅出设计模式) Java与模式 Google zhu13916technology documentdesign patterns,