Lecture 9 Improving Software Design CSC301-Winter 2011 – University of Toronto – Department of Computer Science Hesam C. Esfahani

Slides:



Advertisements
Similar presentations
Quality of a Class Abstraction: Coupling & Cohesion Michael L. Collard, Ph.D. Department of Computer Science Kent State University.
Advertisements

Chapter 11 Component-Level Design
General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
SOLID Object Oriented Design Craig Berntson
What is Software Design?  Introduction  Software design consists of two components, modular design and packaging.  Modular design is the decomposition.
Solutions to Review Questions. 4.1 Define object, class and instance. The UML Glossary gives these definitions: Object: an instance of a class. Class:
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
© Copyright 2011 John Wiley & Sons, Inc.
Jump to first page 1 System Design (Finalizing Design Specifications) Chapter 3d.
Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion.
Developed by Reneta Barneva, SUNY Fredonia Component Level Design.
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
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”.
GENERAL CONCEPTS OF OOPS INTRODUCTION With rapidly changing world and highly competitive and versatile nature of industry, the operations are becoming.
สาขาวิชาเทคโนโลยี สารสนเทศ คณะเทคโนโลยีสารสนเทศ และการสื่อสาร.
Coupling and Cohesion Pfleeger, S., Software Engineering Theory and Practice. Prentice Hall, 2001.
Coupling and Cohesion Source:
Ch:10 Component Level Design Unit 4. What is Component? A component is a modular building block for computer software Because components reside within.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 09. Review Introduction to architectural styles Distributed architectures – Client Server Architecture – Multi-tier.
Company Confidential – Do Not Duplicate 2 Copyright 2008 McLane Advanced Technologies, LLC S.O.L.I.D. Software Development Achieving Object Oriented Principles,
SOFTWARE DESIGN (SWD) Instructor: Dr. Hany H. Ammar
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
Software Design Deriving a solution which satisfies software requirements.
1 Software Design Overview Reference: Software Engineering, by Ian Sommerville, Ch. 12 & 13.
CSE 301 Exam Revision Lecture
Introduction to SOLID Principles. Background Dependency Inversion Principle Single Responsibility Principle Open/Closed Principle Liskov Substitution.
Cohesion and Coupling CS 4311
S.O.L.I.D. Software Development 12 January 2010 (Martin Verboon, Patrick Kalkman, Stan Verdiesen)
Coupling & Cohesion CMSC 201- Fall '11. Vocabulary Routine- A programming unit that performs a task, such as a function, procedure, method, or main class.
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 12-5 Software Engineering Design Goals.
Software Design Principles
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.
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.
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
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.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Microsoft Advertising 16:9 Template Light Use the slides below to start the design of your presentation. Additional slides layouts (title slides, tile.
Beginning Software Craftsmanship Brendan Enrick Steve Smith
Coupling and Cohesion Schach, S, R. Object-Oriented and Classical Software Engineering. McGraw-Hill, 2002.
Coupling and Cohesion Pfleeger, S., Software Engineering Theory and Practice. Prentice Hall, 2001.
14 Jul 2005CSE403, Summer'05, Lecture 09 Lecture 09: Fundamental Principles and Best Practices for Software Design Valentin Razmov.
Mantas Radzevičius ifm-2/2
7. Modular and structured design
Coupling and Cohesion Rajni Bhalla.
Software Architecture & Difference from Design
Coupling and Cohesion 1.
CSE687 - Object Oriented Design class notes Survey of the C++ Programming Language Jim Fawcett Spring 2004.
Copyright © by Curt Hill
Software Engineering: A Practitioner’s Approach, 6/e Chapter 11 Component-Level Design copyright © 1996, 2001, 2005 R.S. Pressman & Associates, Inc.
Cohesion and Coupling Chapter 5, Pfleeger 01/01/10.
Component-Level Design
Object-Oriented Design
Programming Logic and Design Fourth Edition, Comprehensive
Software Design Lecture : 9.
The SOLID Principles.
A (partial) blueprint for dealing with change
Principles of Object-Oriented Design
Cohesion and Coupling.
Dependency Inversion principle
Some principles for object oriented design
Presentation transcript:

Lecture 9 Improving Software Design CSC301-Winter 2011 – University of Toronto – Department of Computer Science Hesam C. Esfahani

Outline  Cohesion & Coupling  SOLID Design Principles 2

Cohesion & Coupling 3

No dependencies Loosely coupled-some dependencies Highly coupled-many dependencies High coupling makes modifying parts of the system difficult, e.g., modifying a component affects all the components to which the component is connected.

5  Content Coupling  One component references contents of another  One component modifies or relies on the internal workings of another module  Common Coupling  Two components share the same global data  Control Coupling  One Component controls the flow of another, by passing it information on what to do  Component passes control parameters to coupled components  Message Coupling (low)  Modules are not dependent on each other, instead they use a public interface to exchange parameter-less messages

 A component is cohesive if all elements of the component are directed toward and essential for performing the same task  Several forms of cohesion:

7 Communicational

8  Coincidental Cohesion  Parts of the component are only related by their location in source code  Logical Cohesion  Elements of component are related logically and not functionally  Temporal Cohesion  Elements of a component are related by timing  Procedural  Elements of a component are related only to ensure a particular order of execution.

9  Communicational Cohesion  Module performs a series of actions related by a sequence of steps to be followed by the product and all actions are performed on the same data  Sequential Cohesion  The output of one component is the input to another. Occurs naturally in functional programming languages  Functional Cohesion  Every essential element to a single computation is contained in the component

10 S.O.L.I.D. Design Principles

Software “Smell” Another perspective  The system is rigid  Hard to change because everything has to change at once  Shotgun Surgery  The system is fragile  Changes cause the system to break in the strangest of places.  The system is immobile  That is, not reusable.  The system is opaque  Hard to understand.  The system is needlessly complex  The system contains needless repetition. Have you ever seen software with these problems?

 Introduced by Robert C. Martin  Agile Principles, Patterns, and Practices in C#  S  O  L  I  D  Single Responsibility Principle  Open/Closed Principle  Liskov Substitution Principle  Interface Segregation Principle  Dependency Inversion Principle SOLID Design Principles

Single Responsibility Principle (SRP)  A class should have only one reason to change Just because you can, doesn’t mean you should.

Single Responsibility Principle (SRP)  A class should have only one reason to change  A responsibility is “a reason for change.”  The responsibilities of a class are axes of change.  If it has two responsibilities, they are coupled in the design, and so have to change together.  If a class has more that one responsibility, it may become fragile when any of the requirements change

SRP – Example GetData() FormatReport() Print() Report

SRP – Example cnt.

GetData() FormatReport() Print() Report GetData() DataAccess FormatReport() ReportFormater Print() ReportPrinter Print() Report SRP – Example cnt.

Benefits of SRP code is smaller ▪ easier to read ▪ easier to understand ▪ easier to maintain Code is potentially easier to test Change is easier to manage ▪ code is easier to replace

Open/Closed Principle (OCP) Software entities (module, classes, functions, etc.) should be open for extension but closed for modification  Classes should be written so that they can be extended without requiring modification.  extend the behaviour of a system (in response to a requested change)  add new code  But not modifying the existing code  Once completed, the implementation of a class should only be modified to correct errors; new or changed features would require that a different class be created  Mechanism: Abstraction and subtype polymorphism are the keys to the OCP  Introducing Abstract Base Classes  Implementing common Interfaces  Deriving from common interfaces or classes

OCP – Example  Changing the print code  Format the report into tabloid (instead of 8-1/2 X 11)  Print the report to a dot-matrix (instead of laser) printer GetData() DataAccess FormatReport() ReportFormater Print() ReportPrinter Print() Report

OCP – Example ct. GetData() DataAccess FormatReport() ReportFormater Print() ReportPrinter Print() Report

OCP – Example cnt.

GetData() DataAccess Format() ReportFormater Print() ReportPrinter Print() Report Print() TabloidReport Print() TabloidReportPrinter Format() TabloidReportFormater

OCP – Example cnt. Note the extension of the system behaviour without making modification to existing code

Benefits of OCP design is more stable ▪ existing (working) code does not change ▪ changes are made by adding, not modifying, code ▪ changes are isolated and do not cascade throughout the code code is potentially easier to test change is easier to manage ▪ code is easier to replace ▪ design is extensible code is potentially reusable Higher abstraction

Liskov Substitution Principle (LSP) Subclasses should be substitutable for their base classes

Liskov Substitution Principle (LSP)  Subclasses should be substitutable for their base classes  LSP states that if a program module is using a Base class, then the reference to the Base class can be replaced with a Derived class without affecting the functionality of the program module.  “A derived class should have some kind of specialized behaviour (it should provide the same services as the superclass, only some, at least, are provided differently.)”  The contract of the base class must be honoured by the derived class.  If this principle is violated, then it will cause the Open-Closed Principle to be violated also. Why?

LSP – Benefits  design is more flexible ▪ a base type can be confidently substituted for a derived type  code is potentially easier to test  required to support the Open/Closed Principle

Interface Segregation Principle (ISP)  Many client-specific interfaces are better than one general purpose interface  Clients should not depend on interfaces that they do not use  Interfaces should be thin and not fat  E.g. if you have an interface with 20 methods, then not all implementers of that interface might not implement all of those methods ▪ Bring large and fat interfaces into smaller ones which classify the related methods  If you have a class with several different uses, create separate (narrow) interfaces for each use.  The purpose is to make clients use as small and as coherent an interface as possible.  Fat interfaces lead to inadvertent couplings and accidental dependencies between classes.

ISP – Example

ISP – Example cnt. Breaking the fat interface of IDataAccess into two smaller ones: Class that implements the interface does no longer need to implement all methods of the class which it does not need

ISP – Example cnt.

ISP – Benefits Cohesion is increased ▪ clients can demand cohesive interfaces Design is more stable ▪ changes are isolated and do not cascade throughout the code Supports the Liskov Substitution Principle

Dependency Inversion Principle (DIP)  High level modules should not depend on low level modules  both should depend upon abstractions  Dependency upon lower-level components limits the reuse opportunities of the higher- level components  Abstractions should not depend on details  Change the relation between concrete classes to relationships among abstractions  Abstraction mechanism: Abstract base classes or Interfaces  The goal of the dependency inversion principle is to  decouple high-level components from low-level components  such that reuse with different low-level component implementations becomes possible  High level classes should contain the “business logic”, low level classes should contain the details of the (current) implementation.  Access instances using interfaces or abstract classes to avoid dependency inversion.

DIP – Example GetData() DataAccess Format() ReportFormater Print() ReportPrinter Print() Report Print() TabloidReport Print() TabloidReportPrinter Format() TabloidReportFormater

DIP Print() ReportPrinter Print() TabloidReport Print() IReportPrinter > Print() TabloidReport Print() ReportPrinter

DIP

 Enables design by contract  change is easier to manage ▪ code is easier to replace ▪ design is extensible  code is potentially easier to test 38

 Example slides are partly adopted from 