TEST-1 7. Object-Oriented Design Principles. TEST-2 The Pillars of the Paradigm Abstraction Encapsulation Hierarchy –Association, Aggregation –Inheritance.

Slides:



Advertisements
Similar presentations
Ch:8 Design Concepts S.W Design should have following quality attribute: Functionality Usability Reliability Performance Supportability (extensibility,
Advertisements

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
Inheritance Inheritance Reserved word protected Reserved word super
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Software Testing and Quality Assurance Lecture 12 - The Testing Perspective (Chapter 2, A Practical Guide to Testing Object-Oriented Software)
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Programming Language Paradigms: summary. Object-oriented programming Objects are the fundamental building blocks of a program. Interaction is structured.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion.
Chapter 10 Class and Method Design
Developed by Reneta Barneva, SUNY Fredonia Component Level Design.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
3/15/05H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
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.
CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.
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.
CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
CSE 301 Exam Revision Lecture
Introduction to SOLID Principles. Background Dependency Inversion Principle Single Responsibility Principle Open/Closed Principle Liskov Substitution.
CSSE 374: More GRASP’ing for Object Responsibilities
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Башкирцев (Старовер) Станислав JavaTalks OOD Principles.
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 12-5 Software Engineering Design Goals.
Design Principles iwongu at gmail dot com.
1 OO Package Design PrinciplesStefan Kluth 4OO Package Design Principles 4.1Packages Introduction 4.2Packages in UML 4.3Three Package Design Principles.
Object Oriented Software Development
Object-Oriented Design
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.
OO Design Principles Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
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.
Testing OO software. State Based Testing State machine: implementation-independent specification (model) of the dynamic behaviour of the system State:
High Cohesion Low Coupling Old Standards for Object Oriented Programming.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CHAPTER 3 MODELING COMPONENT-LEVEL DESIGN.
Principles of Object Oriented Design
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
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.
PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 5th Edition Copyright © 2015 John Wiley & Sons, Inc. All rights.
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Principles of Package Design COMPOSE AT A HIGHER LEVEL OF ABSTRACTION.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
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
Copyright © Jim Fawcett Spring 2017
Software Architecture & Difference from Design
Copyright © by Curt Hill
TIM 58 Chapter 8: Class and Method Design
Principles of Package Architecture
Object-Oriented Design
Design Tips.
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
A (partial) blueprint for dealing with change
Principles of Object-Oriented Design
Presentation transcript:

TEST-1 7. Object-Oriented Design Principles

TEST-2 The Pillars of the Paradigm Abstraction Encapsulation Hierarchy –Association, Aggregation –Inheritance Polymorphism

TEST-3 What’s OO? Is it using Objects? Is it using C++, Java, C#, Smalltalk? No, its got to be using UML?! What makes a program OO? How do you measure good design?

TEST-4 Measuring Quality of an Abstraction Designing Classes & Objects An incremental, iterative process Difficult to design right the first time

TEST-5 Metrics for class design Coupling –inheritance Vs. coupling –Strong coupling complicates a system –design for weakest possible coupling Cohesion –degree of connectivity among the elements of a single module/class –coincidental cohesion: all elements related undesirable –Functional cohesion: work together to provide well-bounded behavior

TEST-6 Law of Demeter “Methods of a class should not depend in any way on the structure of any class, except the immediate structure of their own class. Further, each method should send messages to objects belonging to a very limited set of classes only.”

TEST-7 Bad design Perils of a bad design –Rigidity Hard to change, results in cascade of changes –Fragility Breaks easily and often –Immobility Hard to reuse (due to coupling) –Viscosity Easy to do wrong things, hard to do right things –Needless Complexity Complicated class design, overly generalized –Needless Repetition Copy and Paste away –Opacity Hard to understand

TEST-8 Principles Guiding Principles that help develop better systems Use principles only where they apply You must see the symptoms to apply them If you apply arbitrarily, the code ends up with Needless Complexity

TEST-9 DRY Don’t Repeat Yourself “Every Piece of Knowledge must have a single, unambiguous, authoritative representation within a system” One of the most difficult, but most seen How many times have you see this happen Execution Engine (chokes on certain names of objects) Frontend 1. Validates input 2. Fixes the restriction 3. Took weeks to get this issue resolved

TEST-10 DRY Some times hard to realize this It is much easier to copy, paste and modify code to get it working the way you want it, isn’t it Duplicating code results in –Poor maintainability –Expensive to fix bugs/errors –Hard to keep up with change

TEST-11 SRP Single-Responsibility Principle What metric comes to mind? “A class should have only one reason to change” Some C++ books promoted bad design –Overloading input/output operators! What if you do not want to display on a terminal any more? –GUI based, or web based?

TEST-12 SRP… Alarm +alert() UI Control System AlarmUI +display(Panel) Alarm +alert() +display(Panel) UI Control System Faces more frequent change Has greater dependency (to UI related stuff) Related topics: MVC Analysis model stereotypes : Control Entity Boundary

TEST-13 SRP at Module Level Can be extended to module level as well Gui Framework V 1.0 Gui Framework V 1.1 Component Development Utilities Throw it in there Gui Framework V 1.2 User Of Module Forced to accept Irrelevant change

TEST-14 SRP affects Reuse Lower cohesion results in poor reuse –My brother just bought a new DVD and a big screen TV! –He offers to give me his VCR! –I have a great TV and all I need is a VCR –Here is what I found when I went to pickup! Tight coupling Poor Cohesion Bad for resuse Disclaimer: This slide not intended to say anything about the brand of product shown here as an example!

TEST-15 Nature of code “Software Systems change during their life time” Both better designs and poor designs have to face the changes; good designs are stable

TEST-16 OCP… Bertrand Meyer: “Software Entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification”

TEST-17 Characteristics of a poor design: –Single change results in cascade of changes –Program is fragile, rigid and unpredictable Characteristics of good design: –Modules never change –Extend Module’s behavior by adding new code, not changing existing code OCP…

TEST-18 Software Modules must –be open for extension module’s behavior can be extended –closed for modification source code for the module must not be changed OCP…

TEST-19 How to make the Car run efficiently with Turbo Engine ? Only by changing Car in the above design OCP… Car Piston Engine

TEST-20 A class must not depend on a Concrete class; it must depend on an abstract class Abstraction & Polymorphism are the Key OCP… Car Abstract Engine Piston Engine

TEST-21 Strategic Closure: No program can be 100% closed There will always be changes against which the module is not closed Closure is not complete - it is strategic Designer must decide what kinds of changes to close the design for. This is where the experience and problem domain knowledge of the designer comes in OCP…

TEST-22 Heuristics and Conventions that arise from OCP Make all member variables private –encapsulation: All classes/code that depend on my class are closed from change to the variable names or their implementation within my class. Member functions of my class are never closed from these changes –Further, if this were public, no class will be closed against improper changes made by any other class No global variables OCP…

TEST-23 Heuristics and Conventions that arise from OCP... RTTI is ugly and dangerous –If a module tries to dynamically cast a base class pointer to several derived classes, any time you extend the inheritance hierarchy, you need to change the module Not all these situations violate OCP all the time OCP…

TEST-24 Inheritance is used to realize Abstraction and Polymorphism which are key to OCP How do we measure the quality of inheritance? LSP: “Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it” Liskov Substitution Principle

TEST-25 B publicly inherits from (“is-a”) A means: every object of type B is also object of type A whats true of object of A is also of object of B A represents a more general concept than B B represents more specialized concept than A anywhere an object of A can be used, an object of B can be used A B public/is-a Inheritance

TEST-26 Advertised Behavior of an object Advertised Requirements (Pre-Condition) Advertised Promise (Post Condition) Stack and eStack example Behavior

TEST-27 Design by Contract Advertised Behavior of the Derived class is Substitutable for that of the Base class Substitutability: Derived class Services Require no more and promise no less than the specifications of the corresponding services in the base class Design by Contract

TEST-28 “Any Derived class object must be substitutable where ever a Base class object is used, without the need for the user to know the difference” LSP

TEST-29 LSP in Java? LSP is being used in Java at least in two places Overriding methods can not throw new unrelated exceptions Overriding method’s access can’t be more restrictive than the overridden method –for instance you can’t override a public method as protected or private in derived class

TEST-30 Bad Design is one that is –Rigid - hard to change since changes affect too many parts –Fragile - unexpected parts break upon change –Immobile - hard to separate from current application for reuse in another Nature of Bad Design

TEST-31 Ramifications Controller Clock Depends for Alarm Controller needs an alarm Clock has it, so why not use it? Concrete Controller depends on concrete Clock Changes to Clock affect Controller Hard to make Controller use different alarm (fails OCP) Clock has multiple responsibilities (fails SRP)

TEST-32 Alternate Design Dependency has been inverted Both Controller and Clock depend on Abstraction (IAlarm) Changes to Clock does not affect Controller Better reuse results as well Controller Clock IAlarm Timer

TEST-33 Dependency Inversion Principle “High level modules should not depend upon low level modules. Both should depend upon abstractions.” “Abstractions should not depend upon details. Details should depend upon abstractions.” DIP

TEST-34 The Founding Principles The three principles are closely related Violating either LSP or DIP invariably results in violating OCP It is important to keep in mind these principles to get the most out of OO development

TEST-35 Fat Interfaces Classes tend to grow into fat interfaces Examples of this can been seen in several APIs Less cohesive (fails SRP) A Class Interface of the class may be split C1C2 I1 I2 A Class C1C2 Clients should not know this as a single class They should know about abstract base classes with cohesive interfaces

TEST-36 Growth of an interface IMicrowave +cook(time) +stop() C1 C2 MicrowaveImpl A few days later, Client C2 wants it to chime A few days later, Client C1 wants it to notify (workaholic client?!) IMicrowave +cook(time) +stop() +chime() +notify(…) C1 C2 MicrowaveImpl Clients are forced to use interfaces they do not care about. May result in greater coupling, dependency to more libraries All implementations must carry the weights

TEST-37 ISP Interface Segregation Principle “Clients should not be forced to depend on methods that they do not use” IMicrowave +cook(time) +stop() C1 C2 MicrowaveImpl2 IChimer +chime() INotify +notify(…) MicrowaveImpl1

TEST-38 “The granularity of reuse is the same as the granularity of release. Only components that are released through a tracking system can be effectively reused.” Reuse/Release Equivalency Principle

TEST-39 Release –A class generally collaborates with other classes –For a class to be reused, you need also the classes that this class depends on –All related classes must be released together Reuse/Release Equivalency Principle

TEST-40 Tracking –A class being reused must not change in an uncontrolled manner –Code copying is a poor form of reuse Software must be released in small chunks - components Each chunk must have a version number Reusers may decide on an appropriate time to use a newer version of a component release Reuse/Release Equivalency Principle

TEST-41 “Classes within a released component should share common closure. If one need to be changed, they all are likely to need to be changed. What affects one, affect all.” Common Closure Principle

TEST-42 A change must not cause modification to all released components Change must affect smallest possible number of released components Classes within a component must be cohesive Given a particular kind of change, either all classes in a component must be modified or no class needs to be modified Reduces frequency of re-release of component Common Closure Principle...

TEST-43 “Classes within a released component should be reused together. That is, it must be impossible to separate the component in order to reuse less than the total.” Common Reuse Principle

TEST-44 Components must be focused Component must not contain classes that an user is not likely to reuse –user may be forced to accept a new release due to changes to unused classes Component must be narrow Common Reuse Principle...

TEST-45 “The dependency structure for released component must be a Directed Acyclic Graph. There can be no cycles.” Acyclic Dependency Principle

TEST-46 Cannot release application in pieces ElectronicCircuit Components Packaged Units Combinatorial Components Sequential Components Acyclic Dependency Principle...

TEST-47 ElectronicCircuit Components Packaged Units Combinatorial Components Sequential Components Connectors Break cyclic dependency into subcomponents Acyclic Dependency Principle

TEST-48 “Dependencies between released components must run in the direction of stability. The dependee must be more stable than the depender.” Stable Dependency Principle

TEST-49 A component can never be more stable than the one it depends upon Instability I = C e / (C a + C e ), where C a - # of classes outside that depend upon this class C e - # of classes outside that this class depends upon 0  I  ultimately stable; 1 - ultimately unstable Stable Dependency Principle

TEST-50 Components should be arranged such that components with a high I metrics should depend upon component with low I metrics Stable Dependency Principle...

TEST-51 “The more stable a component is, the more it must consist of abstract classes. A completely stable category should consist of nothing but abstract classes.” Stable Abstraction Principle

TEST-52 Implementation of methods change more often than the interface Interfaces have more intrinsic stability than executable code Abstraction of a Component A = (# of abstract classes) / (# of classes) 0  A  no abstract classes; 1 - all abstract classes Stable Abstraction Principle

TEST-53 Stability Vs. Abstractness The Main Sequence A I Instability Abstractness (0, 1) (1, 0) 0 Maximally stable & abstract Maximally instable & concrete Highly stable & Concrete (Rigid) (1,1) Highly instable & abstract (Rigid)

TEST-54 Distance from the main sequence D = |(A + I - 1) / 2 | 0  D  0.707; Desirable value of D is closed to 0 Normalized form D' = |(A + I -1)| Calculate D value for each component Component whose D value is not near Zero can be reexamined and restructured

TEST-55 Applying the Principles Developing with OO is more than –Using a certain language –Creating objects –Drawing UML It tends to elude even experienced developers Following the principles while developing code helps attain agility Use of each principle should be justified at each occurrence, however