The Challenge. Principles of Software Design

Similar documents
Principles of Software Design. Software Engineering Alessio Gambi Saarland University

PRINCIPLES OF SOFTWARE DESIGN

Maintainable Software. Software Engineering Andreas Zeller, Saarland University

Patterns of Software Design. Andreas Zeller Saarland University

Lecture: Modular Design

1. Software Systems Complexity, OO Paradigm, UML

Classes and Objects. Object Orientated Analysis and Design. Benjamin Kenwright

The major elements of the object-oriented model

Object-Oriented Modeling Using UML. CS151 Chris Pollett Aug. 29, 2005.

OO Class Design Principles

Course 3 7 March

Making Programs Fail. Andreas Zeller

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

OBJECT ORİENTATİON ENCAPSULATİON

Object Orientated Analysis and Design. Benjamin Kenwright

Chapter 8: Class and Method Design

CS 520 Theory and Practice of Software Engineering Fall 2017

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Software Engineering CSC40232: SOFTWARE ENGINEERING. Guest Lecturer: Jin Guo SOLID Principles sarec.nd.edu/courses/se2017

Today's Agenda. References. Open Closed Principle. CS 247: Software Engineering Principles. Object-Oriented Design Principles

CS 520 Theory and Practice of Software Engineering Fall 2018

Object- Oriented Design with UML and Java Part I: Fundamentals

Introduction to Software Engineering. 5. Modeling Objects and Classes

UML Primer. -Elango Sundaram

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

Object-Oriented Programming Concepts

Intro to: Design Principles

S T R U C T U R A L M O D E L I N G ( M O D E L I N G A S Y S T E M ' S L O G I C A L S T R U C T U R E U S I N G C L A S S E S A N D C L A S S D I A

administrivia today UML start design patterns Tuesday, September 28, 2010

Abstract Data Types (ADT) and C++ Classes

Component-Level Design

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

CISC 322 Software Architecture

GRASP Design Patterns A.A. 2018/2019

Unified Modeling Language

OO Frameworks. Introduction. Using Frameworks

Chapter 5 Object-Oriented Programming

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

CS 320 Introduction to Software Engineering Spring March 06, 2017

LABORATORY 1 REVISION

CS 370 Design Heuristics D R. M I C H A E L J. R E A L E F A L L

OO Techniques & UML Class Diagrams

Chapter 10 Classes Continued. Fundamentals of Java

Chapter 1: Principles of Programming and Software Engineering

Information System Design (IT60105)

Introduction to Unified Modelling Language (UML)

CSE 70 Final Exam Fall 2009

UML & OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 3 09/04/2012

History of object-oriented approaches

Principles of Object-Oriented Design

Principles of OO Design

OOAD - OBJECT MODEL. The concepts of objects and classes are intrinsically linked with each other and form the foundation of object oriented paradigm.

Programmazione. Prof. Marco Bertini

CSC207 Week 3. Larry Zhang

LECTURE 3: SOFTWARE DESIGN. Software Engineering Mike Wooldridge

2 UML for OOAD. 2.1 What is UML? 2.2 Classes in UML 2.3 Relations in UML 2.4 Static and Dynamic Design with UML. UML for OOAD Stefan Kluth 1

Software Engineering

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

OOAD and UML. Prabhudeva S. Assistant professor Department of Computer Science and engineering. J N N C E, Shimoga. Downloaded from

2D1358 Object Oriented Program Construction in C++ Exercises & Labs. Course Registration / Accounts. Course Literature

CMPS 115 Winter 04. Class #10 (2004/02/05) Changes/Review Programming Paradigms Principles of OOD <break> Design Patterns

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 00. WELCOME TO OBJECTVILLE. Speaking the Language of OO

UML Is Not a Methodology

OBJECT-ORIENTED MODELING AND DESIGN. Introduction

1. Write two major differences between Object-oriented programming and procedural programming?

Object Oriented Analysis & Design (OOAD)

Lecture Chapter 2 Software Development

CHAPTER 5 GENERAL OOP CONCEPTS

Expanding Our Horizons. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 9 09/25/2011

Introduction to Software Engineering. 5. Modeling Objects and Classes

Object-Oriented Introduction

Object Model. Object Orientated Analysis and Design. Benjamin Kenwright

What are the characteristics of Object Oriented programming language?

Abstraction. Design fundamentals in OO Systems. Fundamental Software Development Principles

Experiment no 4 Study of Class Diagram in Rational Rose

Lecture #2 on Object-Oriented Modeling

OBJECT ORIENTED SYSTEM DEVELOPMENT Software Development Dynamic System Development Information system solution Steps in System Development Analysis

Design Engineering. Overview

Chapter 1: Programming Principles

The Unified Modeling Language. Asst.Prof.Dr. Supakit Nootyaskool IT-KMITL

BCS THE CHARTERED INSTITUTE FOR IT. BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 5 Diploma in IT. Object Oriented Programming

Object-Oriented Design II - GRASP

Object Oriented Software Development CIS Today: Object Oriented Analysis

Summary of the course lectures

Object-Oriented Design

CS 520 Theory and Practice of Software Engineering Fall 2018

Last lecture. Lecture 9. in a nutshell. in a nutshell 2. Example of encapsulation. Example of encapsulation. Class test. Procedural Programming

INHERITANCE - Part 1. CSC 330 OO Software Design 1

Software Development Cycle. Unified Modeling Language. Unified Modeling Language. Unified Modeling Language. Unified Modeling Language.

OO System Models Static Views

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

Object-Oriented Design

Object Oriented Programming

CS 320 Introduction to Software Engineering Spring March

COURSE 2 DESIGN PATTERNS

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

From Module To Objects

From Analysis to Design. LTOOD/OOAD Verified Software Systems

MechEng SE3 Lecture 7 Domain Modelling

Transcription:

Squircle {2 * k.radius = r.a = r.b +set_a() +resize(factor:double) Shape -position: Point = (10, 10) +area(): double +draw() +set_position(position: Point) +get_position(): Point Circle -radius: double = 1 {radius > 0 +area(): double +draw() +set_radius(radius:double) +get_radius(): double Rectangle -a: double = 10 {a > 0 -b: double = 10 {b > 0 +area(): double +draw() +set_a(length:double) +set_b(length:double) +get_a(): double +get_b(): double 1 These slides are based on Grady Booch: Object-Oriented Analysis and Design (1998), updated from various sources Principles of Software Design Software Engineering Andreas Zeller Saarland University The Challenge 2 Software may live much longer than expected Software must be continuously adapted to a changing environment Maintenance takes 50 80% of the cost Goal: Make software maintainable and reusable at little or no cost Abstract Class Abstract Method Composition Object Model Associations between Objects Underlined names indicate concrete objects (instances), which have concrete values for their attributes. UML in a Nutshell s: Squircle r: Rectangle c: Circle resize(factor) p1: Professor Initial Value name = "Phillip" Constraint p2: Professor name = "Andreas" State Diagram s1: Student name = "Georg" s2: Student name = "Gerda" s3: Student name = "Gustav" s4: Student name = "Grete" 3 User get_a() a cancel() [bookedseats > 1] new a: a' = a * factor set_a(a') set_radius(a' / 2) Not reserved entry / reset() reserve() cancel() [bookedseats == 1] reserve() [availableseats > 1] partially booked Sequence Diagram set_a(a') set_b(a') create_flight() cancel_flight() close() closed close() reserve() cancel() [availableseats == 1] fully booked

UML Recap 4 Want a notation to express OO designs UML = Unified Modeling Language a standardized (ISO/IEC 19501:2005), general-purpose modeling language includes a set of graphic notation techniques to create visual models of object-oriented software-intensive systems UML Creators 5 Grady Booch Jim Rumbaugh Ivar Jacobson Object-Oriented Modeling in UML 6 includes the following design aspects: Object model: Which objects do we need? Which are the features of these objects? (attributes, methods) How can these objects be classified? (Class hierarchy) What associations are there between the classes? Sequence diagram: How do the objects act together? State chart: What states are the objects in?

Object Model Account -balance: double -minimum_balance: double -owner: string +open() +deposit() +withdraw() +may_withdraw() 7 Cheking_Account -overdraft_limit: double +set_overdraft_limit() +may_withdraw() +print_account_statement() Loan_Account -interest_rate: double -amortization_amount: double +set_interest_rate() +may_withdraw() Associations 8 Professor -name: string 0..* 0..* Student -name: string Professors have multiple students, and students have multiple professors. Associations between Objects 9 p1: Professor name = "Phillip" s1: Student name = "Georg" p2: Professor name = "Andreas" s2: Student name = "Gerda" Underlined names indicate concrete objects (instances), which have concrete values for their attributes. s3: Student name = "Gustav" s4: Student name = "Grete"

Composition 10 A "squircle" consists of a circle on top of a square: Composition 11 Abstract Class Abstract Method Shape -position: Point = (10, 10) +area(): double +draw() +set_position(position: Point) +get_position(): Point Initial Value Constraint Squircle {2 * k.radius = r.a = r.b +set_a() +resize(factor:double) Composition Circle -radius: double = 1 {radius > 0 +area(): double +draw() +set_radius(radius:double) +get_radius(): double Rectangle -a: double = 10 {a > 0 -b: double = 10 {b > 0 +area(): double +draw() +set_a(length:double) +set_b(length:double) +get_a(): double +get_b(): double 12 s: Squircle r: Rectangle c: Circle resize(factor) get_a() User set_a(a') a new a: a' = a * factor Sequence Diagram set_a(a') set_b(a') set_radius(a' / 2)

Squircle {2 * k.radius = r.a = r.b +set_a() +resize(factor:double) Shape -position: Point = (10, 10) +area(): double +draw() +set_position(position: Point) +get_position(): Point Circle -radius: double = 1 {radius > 0 +area(): double +draw() +set_radius(radius:double) +get_radius(): double Rectangle -a: double = 10 {a > 0 -b: double = 10 {b > 0 +area(): double +draw() +set_a(length:double) +set_b(length:double) +get_a(): double +get_b(): double State Diagram 13 cancel() [bookedseats > 1] Not reserved entry / reset() reserve() cancel() [bookedseats == 1] partially booked reserve() [availableseats > 1] create_flight() cancel_flight() close() cancel() reserve() [availableseats == 1] closed close() fully booked Abstract Class Abstract Method Composition Object Model Associations between Objects Underlined names indicate concrete objects (instances), which have concrete values for their attributes. UML in a Nutshell s: Squircle r: Rectangle c: Circle resize(factor) p1: Professor Initial Value name = "Phillip" Constraint p2: Professor name = "Andreas" State Diagram s1: Student name = "Georg" s2: Student name = "Gerda" s3: Student name = "Gustav" s4: Student name = "Grete" 14 User get_a() a cancel() [bookedseats > 1] new a: a' = a * factor set_a(a') set_radius(a' / 2) Not reserved entry / reset() reserve() cancel() [bookedseats == 1] reserve() [availableseats > 1] partially booked Sequence Diagram set_a(a') set_b(a') create_flight() cancel_flight() close() closed close() reserve() cancel() [availableseats == 1] fully booked 15 These slides are based on Grady Booch: Object-Oriented Analysis and Design (1998), updated from various sources Principles of Software Design Software Engineering Andreas Zeller Saarland University

Principles of object-oriented design 16 Abstraction Encapsulation Modularity Hierarchy Goal: Maintainability and Reusability Principles of object-oriented design 17 Abstraction Encapsulation Modularity Hierarchy Abstraction 18 Concrete Object General Principle

Abstraction 19 Highlights common properties of objects Distinguishes important and unimportant properties Must be understood even without a concrete object Abstraction 20 An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer Perspectives 21

Example: Sensors 22 An Engineer s Solution 23 void check_temperature() { // see specs AEG sensor type 700, pp. 53 short *sensor = 0x80004000; short *low = sensor[0x20]; short *high = sensor[0x21]; int temp_celsius = low + high * 256; if (temp_celsius > 50) { turn_heating_off() Abstract Solution 24 typedef float Temperature; typedef int Location; All implementation! class TemperatureSensor { details are hidden public: TemperatureSensor(Location); ~TemperatureSensor();! void calibrate(temperature actual); Temperature currenttemperature() const; Location location() const;! private:

More Abstraction 25 Principles of object-oriented design 26 Abstraction hide details Encapsulation Modularity Hierarchy Principles of object-oriented design 27 Abstraction Hide details Encapsulation Modularity Hierarchy

Encapsulation 28 No part of a complex system should depend on internal details of another Goal: keep software changes local Information hiding: Internal details (state, structure, behavior) become the object s secret Encapsulation 29 Encapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and its behavior; encapsulation serves to separate the contractual interface of an abstraction and its implementation. Encapsulation 30

An active Sensor 31 class ActiveSensor { public: ActiveSensor(Location) ~ActiveSensor();! void calibrate(temperature actual); Temperature currenttemperature() const; Location location() const;! void register(void (*callback)(activesensor *));! private: called when temperature changes Callback management is the sensor s secret Anticipating Change 32 Features that are anticipated to change should be isolated in specific components Number literals String literals Presentation and interaction Number literals 33 If one searches for 100, one will miss the 99 :-( int a[100]; for (int i = 0; i <= 99; i++) a[i] = 0; const int SIZE = 100; int a[size]; for (int i = 0; i < SIZE; i++) a[i] = 0; const int ONE_HUNDRED = 100; int a[one_hundred];

Number literals 34 double sales_price = net_price * 1.19; final double VAT = 1.19; double sales_price = net_price * VAT; String literals 35 if (sensor.temperature() > 100) printf( Water is boiling! ); if (sensor.temperature() > BOILING_POINT) printf(message(boiling_warning, Water is boiling! ); if (sensor.temperature() > BOILING_POINT) alarm.handle_boiling(); Principles of object-oriented design 36 Abstraction Hide details Encapsulation Keep changes local Modularity Hierarchy

Principles of object-oriented design 37 Abstraction Hide details Encapsulation Keep changes local Modularity Hierarchy Modularity 38 Basic idea: Partition a system such that parts can be designed and revised independently ( divide and conquer ) System is partitioned into modules that each fulfil a specific task Modules should be changeable and reuseable independent of other modules Modularity 39

Modularity 40 Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules. Module Balance 41 Goal 1: Modules should hide information and expose as little as possible Goal 2: Modules should cooperate and therefore must exchange information These goals are in conflict with each other Principles of Modularity 42 High cohesion Modules should contain functions that logically belong together Weak coupling Changes to modules should not affect other modules Law of Demeter talk only to friends

High cohesion 43 Modules should contain functions that logically belong together Achieved by grouping functions that work on the same data natural grouping in object oriented design Weak coupling 44 Changes in modules should not impact other modules Achieved via Information hiding Depending on as few modules as possible Law of Demeter or Principle of Least Knowledge 45 Demeter = Greek Goddess of Agriculture; grow software in small steps; signify a bottom-up philosophy of programming Basic idea: Assume as little as possible about other modules Approach: Restrict method calls to friends

Call your Friends 46 http://en.wikipedia.org/wiki/law_of_demeter A method M of an object O should only call methods of 1. O itself 2. M s parameters 3. any objects created in M 4. O s direct component objects single dot rule Demeter: Example 47 class Uni { Prof boring = new Prof(); public Prof getprof() { return boring; public Prof getnewprof() { return new Prof();! class Test { Uni uds = new Uni(); public void one() { uds.getprof().fired(); public void two() { uds.getnewprof().hired(); Demeter: Example 48 class Uni { Prof boring = new Prof(); public Prof getprof() { return boring; public Prof getnewprof() { return new Prof(); public void fireprof(...) {... class BetterTest { Uni uds = new Uni(); public void betterone() { uds.fireprof(...);!

Demeter effects 49 Reduces coupling between modules Disallow direct access to parts Limit the number of accessible classes Reduce dependencies Results in several new wrapper methods ( Demeter transmogrifiers ) Principles of object-oriented design 50 Abstraction Hide details Encapsulation Keep changes local Modularity Control information flow High cohesion weak coupling talk only to friends Hierarchy Principles of object-oriented design 51 Abstraction Hide details Encapsulation Keep changes local Modularity Control information flow High cohesion weak coupling talk only to friends Hierarchy

Hierarchy 52 Hierarchy is a ranking or ordering of abstractions. Central Hierarchies 53 has-a hierarchy Aggregation of abstractions A car has three to four wheels is-a hierarchy Generalization across abstractions An ActiveSensor is a TemperatureSensor Central Hierarchies 54 has-a hierarchy Aggregation of abstractions A car has three to four wheels is-a hierarchy Generalization across abstractions An ActiveSensor is a TemperatureSensor

Hierarchy principles 55 Open/Close principle Classes should be open for extensions Liskov principle Subclasses should not require more, and not deliver less Dependency principle Classes should only depend on abstractions Hierarchy principles 56 Open/Close principle Classes should be open for extensions Liskov principle Subclasses should not require more, and not deliver less Dependency principle Classes should only depend on abstractions Open/Close principle 57 A class should be open for extension, but closed for changes Achieved via inheritance and dynamic binding

An Internet Connection 58 void connect() { if (connection_type == MODEM_56K) { Modem modem = new Modem(); modem.connect(); else if (connection_type == ETHERNET) else if (connection_type == WLAN) else if (connection_type == UMTS) Solution with Hierarchies 59 MyApp connect() Connection connect() hangup() ModemConnection connect() hangup() WLANConnection connect() hangup() EthernetConnection connect() hangup() Hierarchy principles 60 Open/Close principle Classes should be open for extensions Liskov principle Subclasses should not require more, and not deliver less Dependency principle Classes should only depend on abstractions

Liskov Substitution Principle 61 http://en.wikipedia.org/wiki/liskov_substitution_principle An object of a superclass should always be substitutable by an object of a subclass: Same or weaker preconditions Same or stronger postconditions Derived methods should not assume more or deliver less Circle vs Ellipse 62 Every circle is an ellipse Does this hierarchy make sense? No, as a circle requires more and delivers less Ellipse draw() Circle draw() Hierarchy principles 63 Open/Close principle Classes should be open for extensions Liskov principle Subclasses should not require more, and not deliver less Dependency principle Classes should only depend on abstractions

Dependency principle 64 A class should only depend on abstractions never on concrete subclasses (dependency inversion principle) This principle can be used to break dependencies Dependency // Print current Web page to FILENAME. void print_to_file(string filename) { if (path_exists(filename)) { // FILENAME exists; // ask user to confirm overwrite bool confirmed = confirm_loss(filename); if (!confirmed) return;! // Proceed printing to FILENAME... Cyclic Dependency 65 66 invokes Core +print_to_file() UserPresentation +confirm_loss() invokes Constructing, testing, reusing individual modules becomes impossible!

Dependency // Print current Web page to FILENAME. void print_to_file(string filename, Presentation *p) { if (path_exists(filename)) { // FILENAME exists; // ask user to confirm overwrite bool confirmed = p->confirm_loss(filename); if (!confirmed) return;! // Proceed printing to FILENAME... Depending on Abstraction 67 68 Core +print_to_file() Presentation +confirm_loss() UserPresentation +confirm_loss() ask user AutomatedPresentation +confirm_loss() return true; Choosing Abstraction 69 More on this topic: aspect-oriented programming Which is the dominant abstraction? How does this choice impact the remaining system?

Hierarchy principles 70 Open/Close principle Classes should be open for extensions Liskov principle Subclasses should not require more, and not deliver less Dependency principle Classes should only depend on abstractions Principles of object-oriented design 71 Abstraction Hide details Encapsulation Keep changes local Modularity Control information flow High cohesion weak coupling talk only to friends Hierarchy Order abstractions Classes open for extensions, closed for changes Subclasses that do not require more or deliver less depend only on abstractions Principles of object-oriented design 72 Abstraction Hide details Encapsulation Keep changes local Modularity Control information flow High cohesion weak coupling talk only to friends Hierarchy Order abstractions Classes open for extensions, closed for changes Subclasses that do not require more or deliver less depend only on abstractions Goal: Maintainability and Reusability