OO Design Principles Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, 2001-2004 Copyright © INTEKS LLC, 2003-2004.

Slides:



Advertisements
Similar presentations
•7/12 /07 F-1 © 2010 T. Horton CS 4240 Principles of SW Design Packages in Java and UML.
Advertisements

Stability and Volatility is Software Design H. Rahnama Tutorial in software engineering.
General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Lecture 9 Improving Software Design CSC301-Winter 2011 – University of Toronto – Department of Computer Science Hesam C. Esfahani
SOLID Object Oriented Design Craig Berntson
Design Patterns Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
Common mistakes Basic Design Principles David Rabinowitz.
Common mistakes Basic Design Principles Amit Shabtay.
Developed by Reneta Barneva, SUNY Fredonia Component Level Design.
Package design and the Iterative process model. What is a package? Classes are not sufficient to group code –Some classes collaborate, implying dependencies.
CSc 335: Three More OO Design Principles
TEST-1 7. Object-Oriented Design Principles. TEST-2 The Pillars of the Paradigm Abstraction Encapsulation Hierarchy –Association, Aggregation –Inheritance.
Pedro Mejia Alvarez CINVESTAV-PN
1 OO Design Principles Project Group eXtreme Programming Md. Abul Bashar 07/09/2004.
Alexander Serebrenik, Serguei Roubtsov, and Mark van den Brand D n -based Design Quality Comparison of Industrial Java Applications.
1 OO Design Novosoft, 2001 by V. Mukhortov. 2 OO Design Goals  Flexibility Changes must be localized  Maintainability Modules requiring changes can.
Ch:10 Component Level Design Unit 4. What is Component? A component is a modular building block for computer software Because components reside within.
Company Confidential – Do Not Duplicate 2 Copyright 2008 McLane Advanced Technologies, LLC S.O.L.I.D. Software Development Achieving Object Oriented Principles,
Advanced Principles II Principles of Object-Oriented Component Design Copyright  by Object Mentor, Inc All Rights Reserved Portions of this.
More Design. Next Tuesday First draft of architectural design –use cases –class diagrams for major classes with responsibilities –sequence diagrams for.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
Introduction to SOLID Principles. Background Dependency Inversion Principle Single Responsibility Principle Open/Closed Principle Liskov Substitution.
S.O.L.I.D. Software Development 12 January 2010 (Martin Verboon, Patrick Kalkman, Stan Verdiesen)
Law of Demeter. What is it: Style Rule for building systems. Proposed by my research group: The Demeter Research Group in 1987, published in Covered.
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 12-5 Software Engineering Design Goals.
4/1/05F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Packages and Components in Java and UML.
Design Principles iwongu at gmail dot com.
The benefits of SOLID in software development Ruben Agudo Santos (GS-AIS-HR)
1 OO Package Design PrinciplesStefan Kluth 4OO Package Design Principles 4.1Packages Introduction 4.2Packages in UML 4.3Three Package Design Principles.
Software Design Principles
1 Single Responsibility Principle Open Closed Principle Liskov Substitution Principle Law of Demeter CSC 335: Object-Oriented Programming and Design.
Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
1 Software Engineering: A Practitioner’s Approach, 6/e Chapter 11a: Component-Level Design Software Engineering: A Practitioner’s Approach, 6/e Chapter.
High Cohesion Low Coupling Old Standards for Object Oriented Programming.
Five design principles
Evaluating an Object-Oriented Design ©SoftMoore ConsultingSlide 1.
CHAPTER 3 MODELING COMPONENT-LEVEL DESIGN.
Principles of Object Oriented Design
SOLID Design Principles
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
Session 33 More on SOLID Steve Chenoweth Office: Moench Room F220 Phone: (812) Chandan Rupakheti Office: Moench.
S.Ducasse Stéphane Ducasse 1 Some Principles Stéphane Ducasse ---
1 Good Object-Oriented Design Radu Marinescu Lecture 3 Principles of Object-Oriented Design Part II 19 th November 2002.
The Law of Demeter For Operating System Course. Motivation Several programs were written in bad style Are difficult to maintain There is a simple remedy.
1 The Law of Demeter By Rick Mercer with help from Object-Oriented Design Heuristics, Arthur Riel Addison-Wesley, 1996, ISBN X and Applying.
Principles of Package Design COMPOSE AT A HIGHER LEVEL OF ABSTRACTION.
1 Advanced Object- oriented Design – Principles CS320 Fall 2005.
1 Advanced Object-oriented Design – Principles and Patterns OO Design Principles.
Mantas Radzevičius ifm-2/2
Course information Old exam Resit Report Result and walkthrough
More Design Heuristics
Software Design Principles
Chapter 12 Object Oriented Design Principles
Chapter 12 Object Oriented Design Principles
Copyright © by Curt Hill
Principles of Package Architecture
Software Design Principles
lecture 08, OO Design Principle
Object-Oriented Design
The SOLID Principles.
Principles of Object-Oriented Design Part II
A (partial) blueprint for dealing with change
Principles of Object-Oriented Design
Object Oriented Design & Analysis
Principles of High-Level Design
Dependency Inversion principle
Law of Demeter (LoD) 7/23/2019 LoD
Liskov Substitution Principle (LSP)
Chapter 10 – Component-Level Design
Presentation transcript:

OO Design Principles Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,

OO Designer Activities  Class design  Package design  Describing the design patterns

Class design principles  ORR One Responsibility Rule  LSP Liskov Substitution Principle  LoDLaw of Demeter  OCPOpen-Closed Principle  ISPInterface Segregation Principle

ORR - One Responsibility Rule A class has a single responsibility: it does it all, it does it well, it does it only - R. Martin

LSP – Liskov Substitution Principle Functions that use pointers or references to base classes must be able to use objects of the derived classes without knowing it. - R.Martin, 1996 Original formula: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T the behavior of P is unchanged when o1 is substituted by o2 then S is a subtype of T. - Barbara Liskov, 1988

LSP violation: Rectangle & Square Problem: Square s = new Square(5); s.setHeight(6); // s is not consistent class Rectangle { private int h; private int w; public Rectangle( int w, int h ) { this.h = h; this.w = w; } public void setHeight( int h ) { this.h = h; } public int getHeight() { return h; } } class Square extends Rectangle { public Square( int s ) { super( s, s ); } }

class Square extends Rectangle { public Square( int s ) { super( s, s ); } public void setSize( int s ) { super.setHeight(s); super.setWidth(s); } public void setHeight( int h ) { setSize(h); } public void setWidth( int w ) { setSize(w); } } Problem: void f( Rectangle r ) throws Exception { r.setHeight(4); r.setWidth(5); if( r.getHight() * r.getWidth() != 20 ) throw new Exception( “Bug!” ); }

LSP: The real problem ? Square object is not a Rectangle object! Why? Because of behavior of a Square is not consistent with the behavior of a Rectangle! and … it is behavior that software is really all about! IsA is a behavioral relationship.

LoD – Law of Demeter Original formula: Only talk to your immediate friends. - Ian Holland, 1987 immediate friends of method f : methods of class of f and other argument classes of f methods of immediate part classes of class of f methods of classes of objects that are created in f. A method should have limited knowledge of an object model. - D. Rumbaugh

Grady Booch about The LoD “The basic effect of applying this Law is the creation of loosely coupled classes, whose implementation secrets are encapsulated. Such classes are fairly unencumbered, meaning that to understand the meaning of one class, you need not understand the details of many other classes.” You can play with yourself. You can play with your own toys (but you can't take them apart), You can play with toys that were given to you. And you can play with toys you've made yourself. More on LoD Peter Van Rooijen simple formula

LoD violation Problem: public void getTimeOfBirth() { long time = p.getDateOfBirth().getTime(); } Do not reveal a class secret!

LoD-compliant design Solution: void m() { this.b.call_foo(); } // hide the class secret!

OCP – Open-Closed Principle Software entities (classes, modules, functions, etc) should be open for extension, but closed for modification. - B. Meyer, 1988

OCP violation: Shapes void drawShapes( Shape[] shapes ) { for( int i = 0; i < shapes.length; ++i ) { if( shape[i].getType == Shape.SQUARE ) { drawSquare( (Square)shape[i] ); } else drawCircle( (Circle)shape[i] ); } The problem: You can't add a new shape without changing drawShapes() code.

OCP compliant solution void drawShapes( Shape[] shapes ) { for( int i=0; i < shapes.length; ++i ) { shape[i].draw( device ); }

ISP – Interface Segregation Principle Clients should not be forced to depend upon services they do not use. - R.Martin, 1996 Hints:  Avoid fat interfaces  Separate clients mean separate interfaces Violation cost:  lack of flexibility

ISP violation: Security Door Door has to sound an alarm if it is open for too long. Problem: Timeout method has to be public. But…there are Door clients that do NOT use timeout method and don’t have to. This approach leads to mistakes.

ISP-compliant Security Door Door clients still can use TimedDoor via Door interface Door clients will not be affected by changes made in Timer, TimerClient and TimedDoor

ISP violation: ATM Transactions  Adding new transactions causes all other transactions to recompile  If any Transaction requires a change to UI, all of the other will be forced to recompile

ISP-compliant solution

Dependencies  MDPMinimal Dependencies Principle  DIPDependency Inversion Principle  ADPAcyclic Dependencies Principle

DIP – Dependency Inversion Principle High level modules should not depend upon low-level ones. Both should depend upon abstractions. Abstractions should not depend upon implementation details. Details should depend upon abstractions. - R.Martin, 1996

DIP violation: Copier What if we need to support another kind of printer ?

DIP-compliant solution Now we can easily add new writers and readers

ADP – Acyclic Dependencies Principle The dependency structure between entities (classes, packages, functions) must be a Directed Acyclic Graph (DAG). - R.Martin, 1996  Two entities having to know about each other can not be used separately. They work like a monolith and there is no benefit in separating them.  Increases maintainability

Example: cyclic dependencies Due to the dependency from MyDialogs to Application, MyTasks package depends upon the entire system.

Package design principles  REP - Reuse-Release Equivalence Principle  CRP - Common Reuse Principle  CCP - Common Closure Principle  SDP - Stable Dependencies Principle  SAP - Stable Abstractions Principle

REP - Reuse/Release Equivalence Principle The unit of reuse is the unit of release. The unit is what UML refers to as a package. - R.Martin, 1996  Classes should be grouped into packages according to how they will be reused  To be effectively reused, packages must be given a release number  Makes updates convenient for reusers

CRP – Common Reuse Principle The classes in a package are to be reused together. Reusers should depend upon the entire package, not just a part of it. - R.Martin, 1996  ISP, scaled to packages  Reduces maintenance cost, increases reusability

CRP violation: remote service Problem: Every time the new version of Service is released, clients of ServiceAgent must expect that their code won’t work, even if changes do not affect ServiceAgent.

CRP compliant design Clients of ServiceAgent depend only upon things they really use. Benefit: Application can easily switch from local to remote service implementation.

CRP compliant design Clients of ServiceAgent depend only upon things they really use. Benefit: Changes in local and server packages do not affect application

CCP – Common Closure Principle Classes within the package should be affected by the same kind of changes. Either all open to the kind of change or all closed to the kind of change. - R.Martin, 1996  This produces packages where changes are very localized, and, therefore, number of releases is minimized.

ADP – Acyclic Dependencies Principle The dependency structure between packages must be a Directed Acyclic Graph (DAG). - R.Martin, 1996  Two packages having to know about each other can’t be reused separately. Therefore, they work like a single package.  Increases maintainability

SDP – Stable Dependencies Principle A package should only depend upon packages that are more stable than itself. Stability is a measure of difficulty in changing a package. - R.Martin, 1996  What makes a package hard to change?  Increases maintainability

Measuring Instability Instability of a package I = Ce / ( Ca + Ce ) where Ce = efferent couplings (number of classes outside the package that classes inside the package depend upon). How dependent am I? Ca = afferent couplings (number of classes outside the package that depend upon classes within the package). How responsible am I?

SAP – Stable Abstractions Principle The abstractness of the package should be proportional to its stability. - R.Martin, 1996  If all packages are maximally stable, the system would be unchangeable.  Therefore, some packages must be instable.  Increases maintainability

Measuring Abstractness Abstractness of a package A = Na / N where Na = number of abstract classes N = total number of classes

Main sequence Main Sequence – an instability versus abstractness graph Packages along the line from (0,1) to (1,0) have a good balance Distance from the main sequence D = | A + I – 1 | Given this metric, a design can be analyzed for its overall conformance to the main sequence. - R.Martin, 1994

Main sequence I A USELESS AREA CONCRETE CLASSES

Example: server  What happens if ResultSet is not an interface? I=0 A=1 D=0 I = 1/1 = 1 A= 0 D= 0 I=2/3 A=0 D=1/3

Example: server I=1 A=0 D=0 I=0 A=1/2 D=1/2 I=1/2 A=0 D=1/2 This change affects both MyServer and AbstractServer balance