Systems Analysis & Design Methods VII OOD-principles.

Slides:



Advertisements
Similar presentations
General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Advertisements

C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Error Management with Design Contracts Karlstad University Computer Science Error Management with Design Contracts Eivind J. Nordby, Martin Blom, Anna.
The child gets it all..  Factor out common behavior  parent class implements behavior needed by children  guarantee that all subclasses have the characteristics.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
© The McGraw-Hill Companies, 2006 Chapter 8 Extending classes with inheritance.
Chapter 25 GRASP: More Objects with Responsibilities 1CS6359 Fall 2011 John Cole.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
(c) 2007 Mauro Pezzè & Michal Young Ch 16, slide 1 Fault-Based Testing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Portability CPSC 315 – Programming Studio Spring 2008 Material from The Practice of Programming, by Pike and Kernighan.
3/15/05H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Object-oriented metrics Design decisions: Class Cohesion Open-Closed Single Responsibility Interface Segregation Dependency Inversion Liskov Substitution.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
CMSC 202 Inheritance II. Version 10/102 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Programming using C# Joins SQL Injection Stored Procedures
Refactoring Improving the structure of existing code Refactoring1.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
Башкирцев (Старовер) Станислав JavaTalks OOD Principles.
Systems Analysis & Design Methods UML diagrams. 2 System Analysis & Design Methods: UML diagrams Books UML for JAVA Programmers UML for JAVA Programmers.
Refactoring1 Improving the structure of existing code.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Slide 20.1 Copyright © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. An Introduction to Object-Oriented Systems Analysis and Design with.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 What is SOLID  The S in SOLID  The O in SOLID  The L in SOLID  The I in SOLID  The D in SOLID  Questions.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Software Design Principles
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Software Development
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency.
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
Type Abstraction Liskov, Chapter 7. 2 Liskov Substitution Principle In any client code, if the supertype object is substituted by a subtype object, the.
Type Abstraction SWE Spring October 05Kaushik, Ammann Substitution Principle “In any client code, if supertype object is substituted.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Five design principles
Online Simulation Creation Wizard Introduction to Project
CSE 143 Lecture 12 Inheritance slides created by Ethan Apter
29-July-2002cse Inheritance © 2002 University of Washington1 Inheritance CSE 142, Summer 2002 Computer Programming 1
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Refactoring1 Improving the structure of existing code.
OOP Basics Classes & Methods (c) IDMS/SQL News
CSHenrik Bærbak Christensen1 Flexibility and Maintainability And their metrics: coupling and cohesion.
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CSCE 240 – Intro to Software Engineering Lecture 3.
CSE 143 Lecture 13 Inheritance slides created by Ethan Apter
Sections Inheritance and Abstract Classes
How to be a Good Developer
Inheritance and Polymorphism
Copyright © by Curt Hill
Adaptive Code Via C#
Type Abstraction SWE Spring 2009.
Portability CPSC 315 – Programming Studio
Type Abstraction Liskov, Chapter 7.
Interfaces.
Subtype Substitution Principle
Type Abstraction SWE Spring 2013.
Chapter 6 Inheritance.
Chapter 5 Classes.
Presentation transcript:

Systems Analysis & Design Methods VII OOD-principles

2 Systems Analysis & Design Methods, VII OOD-principles Books UML for JAVA Programmers UML for JAVA Programmers (Robert C. Martin © Copyright 2003.) A book you should read, even if it is not necessary for this course. UML distilled UML distilled Martin Fowler

3 Systems Analysis & Design Methods, VII OOD-principles OOD principles Single Responsibility Single Responsibility Liskov Substitution Liskov Substitution Dependency Inversion Dependency Inversion

4 Systems Analysis & Design Methods, VII OOD-principles OOD principle: Single Responsibility A class should only have one reason to change significantly. Why ?: Simplicity. Suppose a class combines non related functionality and different goals: Clients who are interested in only one aspect, are overwhelmed with methods they don’t need. Clients who are interested in only one aspect, are overwhelmed with methods they don’t need. If one method changes, the client has to recompile, and –more importantly- worry whether his old code will still run as before. If one method changes, the client has to recompile, and –more importantly- worry whether his old code will still run as before.

5 Systems Analysis & Design Methods, VII OOD-principles Single Responsibility: counter example Don’t do this: Problem: A client of Employee who doesn’t need persistence is overloaded with persistence methods it doesn’t care about > Persistent Employee Company

6 Systems Analysis & Design Methods, VII OOD-principles Single Responsibility: Solution Do this: Solution: Code involving persistence is now moved to its own class. ( A client who thinks he is working with an Employee, can still be given a PersistentEmployee without him knowing nor caring about it. ) > Persistent Employee PersistentEmployee

7 Systems Analysis & Design Methods, VII OOD-principles OOD principle: Liskov’s Substitution If in a client X of a class Y, the class Y is replaced by a subclass Z of Y, the client code in X should still run as expected. Why? Avoid unforseen bugs. Suppose we violate this principle, because of a special subclass Z: The client code has to be adapted to cope with the special case. The client code has to be adapted to cope with the special case. The client will have to test the dynamic type of the used class. The client will have to test the dynamic type of the used class.

8 Systems Analysis & Design Methods, VII OOD-principles Liskov’s Substitution: counter example Don’t do this: Don’t do this: > Employee PayedEmployee Volunteer +calculateSalary:double {A}

9 Systems Analysis & Design Methods, VII OOD-principles Liskov’s Substitution: counter example (continued) Inside PayedEmployee, we have: Inside PayedEmployee, we have: public double calculateSalary(){ … return … ;// real salary > 0 } Inside Volunteer, we have: Inside Volunteer, we have: public double calculateSalary(){ public double calculateSalary(){ return 0; // fake salary == 0 }

10 Systems Analysis & Design Methods, VII OOD-principles Liskov’s Substitution: counter example (continued) A client: A client: public float getSumSalaries( List salaries ){ public float getSumSalaries( List salaries ){ Employee emp; Employee emp; float sum = 0; float sum = 0; for(int i = 0; i < salaries.size(); i++){ for(int i = 0; i < salaries.size(); i++){ emp = (Employee) salaries.get(i); emp = (Employee) salaries.get(i); sum += emp.calculateSalary(); sum += emp.calculateSalary(); } return sum; return sum; }

11 Systems Analysis & Design Methods, VII OOD-principles Liskov’s Substitution: counter example (continued) On first sight, the previous slide looks like a nice example of polymorphisme. BUT ‘Paying a salary of zero’ is not the same as ‘not paying a salary’. ‘Paying a salary of zero’ is not the same as ‘not paying a salary’. example: Imagine an embarassing situation of a volunteer receiving a letter ‘Your month’s pay’ with a big zero on it. example: Imagine an embarassing situation of a volunteer receiving a letter ‘Your month’s pay’ with a big zero on it. The client of Employee has to tests the dynamic type to see whether calling the method ‘give paycheck’ or ‘calculateSalary’ are applicable. These kinds of tests are exactly what polymorphism tries to avoid. The client of Employee has to tests the dynamic type to see whether calling the method ‘give paycheck’ or ‘calculateSalary’ are applicable. These kinds of tests are exactly what polymorphism tries to avoid. Calling calculateSalary should not be allowed for objects of type Volunteer. That’s why we opt for the following solution: Calling calculateSalary should not be allowed for objects of type Volunteer. That’s why we opt for the following solution:

12 Systems Analysis & Design Methods, VII OOD-principles Liskov’s Substitution: Solution > Employee PayedEmployee Volunteer +calculateSalary: double Do this: +calculateSalary:double {A}

13 Systems Analysis & Design Methods, VII OOD-principles Dependency Inversion The principle should have been called instead: The principle should have been called instead: ‘Depend on Abstract’ The name originate from the following rule: The name originate from the following rule: Concrete classes should depend on abstract classes (or interfaces) and not the other way around. Concrete classes should depend on abstract classes (or interfaces) and not the other way around. Why ?: Why ?: Concrete classen change a lot. These classes influence your work if you depend on them (meaning: if you are their client). Concrete classen change a lot. These classes influence your work if you depend on them (meaning: if you are their client). Note: It may be okay to depend on concrete classees that aren’t likely to change anywhere soon, e.g. Date, HashMap, etc…. But you never know:.VB6 -> VB.net Note: It may be okay to depend on concrete classees that aren’t likely to change anywhere soon, e.g. Date, HashMap, etc…. But you never know:.VB6 -> VB.net

14 Systems Analysis & Design Methods, VII OOD-principles Dependency Inversion: counter example Don’t do this: Problem: If the MicroToolsCoolForm changes its default properties, then you have to accept that, or change them 58 times in my application. MicroToolsCoolForm MyAccountingApplication 58

15 Systems Analysis & Design Methods, VII OOD-principles Dependency Inversion: First improvement MyForm Improvement: If the MicroToolsCoolForm changes, say, color, you can still overwrite that color ONES in MyForm. 58 MicroToolsCoolForm MyAccountingApplication

16 Systems Analysis & Design Methods, VII OOD-principles Dependency Inversion: Second Improvement: ‘depend on abstract’ MyConcreteForm MyForm > Improvement: IfMicroToolsCoolForm changes, then the MyAccoutingApplication code doesn’t even have to know. It is a client of the MyForm which is abstract (in the case of Java, it is an interface ) MyAccountingApplication MicroToolsCoolForm 58