© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.

Slides:



Advertisements
Similar presentations
Design Principles & Patterns
Advertisements

CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
Since 1995, we’ve been building innovative custom solutions specifically designed to meet the unique needs of America’s most recognized companies. If you.
CS 2430 Day 28. Announcements Program 5 was posted (on Tuesday, 4/2/2013) Can work with a partner (sign up by today at 3:52pm) Exam 2 handed back on Monday.
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
5/17/2015 OO Design: Liskov Substitution Principle 1.
More Inheritance and LSP CS340100, NTHU Yoshi. More about Inheritance Reuse? – Q1: 你有沒有程式 ” 砍掉重練 ” 的經驗 ? – Q2: 你有沒有 ” 再造輪子 ” 的經驗 ? class Rectangle – Firstly,
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
CSc 335: Three More OO Design Principles
13 Jul 2006CSE403, Summer'06, Lecture10 Lifecycle Architecture Review: Preliminary Feedback Valentin Razmov.
1 OO Design Principles Project Group eXtreme Programming Md. Abul Bashar 07/09/2004.
Building SOLID Software with Dependency Injection Jeremy Rosenberg.
CLASS DESIGN PRINCIPLES Lecture 2. The quality of the architecture What is a good design? It is the design that at least does not have signs of “bad”.
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,
OODP Prudent OO Design. OODP-2 The Pillars of the Paradigm Abstraction Encapsulation Hierarchy –Association, Aggregation –Inheritance Polymorphism.
CSE 301 Exam Revision Lecture
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)
Башкирцев (Старовер) Станислав JavaTalks OOD Principles.
Design Principles iwongu at gmail dot com.
The benefits of SOLID in software development Ruben Agudo Santos (GS-AIS-HR)
 What is SOLID  The S in SOLID  The O in SOLID  The L in SOLID  The I in SOLID  The D in SOLID  Questions.
Software Design Principles
1 Single Responsibility Principle Open Closed Principle Liskov Substitution Principle Law of Demeter CSC 335: Object-Oriented Programming and Design.
OO Design Principles Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design for testability as a way to good coding Simone Chiaretta Architect, Council of the EU December 9 th,
High Cohesion Low Coupling Old Standards for Object Oriented Programming.
Five design principles
Subtype Polymorphism, Subtyping vs
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Evaluating an Object-Oriented Design ©SoftMoore ConsultingSlide 1.
Principles of Object Oriented Design
PRINCIPLES OF OBJECT ORIENTED DESIGN S.O.L.I.D. S.O.L.I.D Principles What is SOLID?  Acrostic of 5 Principles:  The Single Responsibility Principle.
SOLID Design Principles
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 ---
SOLID PHP & Code Smell Wrap-Up
Liskov Substitution Principle Jon McBee CLA, CLED, CTD, CPI, LabVIEW Champion.
Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle.
Beginning Software Craftsmanship Brendan Enrick Steve Smith
14 Jul 2005CSE403, Summer'05, Lecture 09 Lecture 09: Fundamental Principles and Best Practices for Software Design Valentin Razmov.
1 Advanced Object- oriented Design – Principles CS320 Fall 2005.
Mantas Radzevičius ifm-2/2
OCP and Liskov Principles
Course information Old exam Resit Report Result and walkthrough
Software Design Principles
Inheritance ITI1121 Nour El Kadri.
Software Architecture & Difference from Design
How to be a Good Developer
Copyright © by Curt Hill
CSE 331 Subtyping slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Software Design Principles
lecture 08, OO Design Principle
11/29/2018 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
Subtype Polymorphism, Subtyping vs
Software Design Lecture : 12.
15 letters that will change your code
Questions First On class? On project? On material we’ve discussed?
The SOLID Principles.
A (partial) blueprint for dealing with change
Principles of Object-Oriented Design
Software Development An overview of careers in Business Application Development Sources: Dice.com, money.CNN.com, InfoWorld.com, money.USNews.com,
Object Oriented Design & Analysis
Dependency Inversion principle
Some principles for object oriented design
Presentation transcript:

© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 2 Agenda  What is SOLID Design Principles?  Code Examples  Q&A

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 3 SOLID  Introduced by Robert C. Martins (”Uncle Bob”)  Agile Manifesto  Author of several books, e.g. ”Clean Code”

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 4 SOLID  SOLID - Single Responsibility Principle - Open Closed Principle - Liskov Substitution Principle - Interface Segregation Principle - Dependency Inverison Principle  Code becomes more Testably (remember TDD is not only about testing, more important its about Design)  Apply ’smart’ - don’t do stuff ’just because of’ - very importad to see the context of the program/code when applying SOLID - Joel On Software advise – use with common sense!

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 5 Single Responsibility Principle  "There should never be more than one reason for a class to change." — Robert Martin, SRP paper linked from The Principles of OODThe Principles of OOD  My translation: A class should concentrate on doing one thing and one thing only

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 6 Single Responsibility Principle  Two resposibilities  Connection Management + Data Communication interface Modem { public void dial(String pno); public void hangup(); public void send(char c); public char recv(); }

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 7 Single Responsibility Principle  Separate into two interfaces interface DataChannel { public void send(char c); public char recv(); } interface Connection { public void dial(String phn); public char hangup(); }

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 8 Open Closed Principle  "Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification." — Robert Martin paraphrasing Bertrand Meyer, OCP paper linked from The Principles of OODThe Principles of OOD  My translation: Change a class' behavior using inheritance and composition

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 9 Open Closed Principle // Open-Close Principle - Bad example class GraphicEditor { public void drawShape(Shape s) { if (s.m_type==1) drawRectangle(s); else if (s.m_type==2) drawCircle(s); } public void drawCircle(Circle r) {....} public void drawRectangle(Rectangle r) {....} } class Shape { int m_type; } class Rectangle extends Shape { Rectangle() { super.m_type=1; } } class Circle extends Shape { Circle() { super.m_type=2; } }

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 10 Open Closed Principle – a Few Problems….  Impossible to add a new Shape without modifying GraphEditor  Important to understand GraphEditor to add a new Shape  Tight coupling between GraphEditor and Shape  Difficult to test a specific Shape without involving GraphEditor  If-Else-/Case should be avoided

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 11 Open Closed Principle - Improved  // Open-Close Principle - Good example class GraphicEditor { public void drawShape(Shape s) { s.draw(); } } class Shape { abstract void draw(); } class Rectangle extends Shape { public void draw() { // draw the rectangle } }

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 12 Liskov Substitution Principle  "Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it." — Robert Martin, LSP paper linked from The Principles of OODThe Principles of OOD  My translation: Subclasses should behave nicely when used in place of their base class

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 13 Liskov Substitution Principle // Violation of Liskov's Substitution Principle class Rectangle { int m_width; int m_height; public void setWidth(int width){ m_width = width; } public void setHeight(int h){ m_height = ht; } public int getWidth(){ return m_width; } public int getHeight(){ return m_height; } public int getArea(){ return m_width * m_height; } } class Square extends Rectangle { public void setWidth(int width){ m_width = width; m_height = width; } public void setHeight(int height){ m_width = height; m_height = height; } }

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 14 Liskov Substitution Principle class LspTest { private static Rectangle getNewRectangle() { // it can be an object returned by some factory... return new Square(); } public static void main (String args[]) { Rectangle r = LspTest.getNewRectangle(); r.setWidth(5); r.setHeight(10); // user knows that r it's a rectangle. It assumes that he's able to set the width and height as for the base class System.out.println(r.getArea()); // now he's surprised to see that the area is 100 instead of 50. } }

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 15 Interface Segregation Principle  "Clients should not be forced to depend upon interfaces that they do not use." — Robert Martin, ISP paper linked from The Principles of OODThe Principles of OOD  My translation: Keep interfaces small

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 16 Interface Segregation Principle  Don’t force classes so implement methods they can’t (Swing/Java)  Don’t pollute interfaces with a lot of methods  Avoid ’fat’ interfaces

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 17 Interface Segregation Principle //bad example (polluted interface) interface Worker { void work(); void eat(); } ManWorker implements Worker { void work() {…}; void eat() {30 min break;}; } RobotWorker implements Worker { void work() {…}; void eat() {//Not Appliciable for a RobotWorker}; }

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 18 Interface Segregation Principle  Solution - split into two interfaces interface Workable { public void work(); } interface Feedable{ public void eat(); }

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 19 Dependency Inversion Principle  "A. High level modules should not depend upon low level modules. Both should depend upon abstractions. B. Abstractions should not depend upon details. Details should depend upon abstractions." — Robert Martin, DIP paper linked from The Principles of OODThe Principles of OOD  My translation: Use lots of interfaces and abstractions

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 20 Dependency Inversion Principle //DIP - bad example public class EmployeeService { private EmployeeFinder emFinder //concrete class, not abstract. Can access a SQL DB for instance public Employee findEmployee(…) { emFinder.findEmployee(…) }

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 21 Dependency Inversion Principle  Now its possible to change the finder to be a XmEmployeeFinder, DBEmployeeFinder, FlatFileEmployeeFinder, MockEmployeeFinder…. //DIP - fixed public class EmployeeService { private IEmployeeFinder emFinder //depends on an abstraction, no an implementation public Employee findEmployee(…) { emFinder.findEmployee(…) }

© 2009 Capgemini - All rights reserved 19 oktober 2015Sida 22 Q&A