PRINCIPLES OF SOFTWARE DESIGN

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

Maintainable Software. Software Engineering Andreas Zeller, Saarland University

The Challenge. Principles of Software Design

The major elements of the object-oriented model

Lecture: Modular Design

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

Chapter 8: Class and Method Design

OO Class Design Principles

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

Chapter 5 Object-Oriented Programming

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

Intro to: Design Principles

Making Programs Fail. Andreas Zeller

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

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

CS 520 Theory and Practice of Software Engineering Fall 2017

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

1. Software Systems Complexity, OO Paradigm, UML

CS 520 Theory and Practice of Software Engineering Fall 2018

Software Engineering

Relationships amongst Objects

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

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

SELECTED TOPICS in APPLIED COMPUTER SCIENCE

Object-Oriented Design

GRASP Design Patterns A.A. 2018/2019

Object Model. Object Orientated Analysis and Design. Benjamin Kenwright

OO Frameworks. Introduction. Using Frameworks

Principles of Object-Oriented Design

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

Chapter 10 Classes Continued. Fundamentals of Java

CSE 70 Final Exam Fall 2009

CPSC 310: Sample Final Exam Study Questions 2014S1 (These are in addition to the Study Questions listed at the end of some lectures)

CHAPTER 5 GENERAL OOP CONCEPTS

Object-Oriented Design II - GRASP

Principles of Software Construction: Objects, Design, and Concurrency

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

CS 320 Introduction to Software Engineering Spring March 06, 2017

Component-Level Design

From Module To Objects

Top Down Design vs. Modularization

Modularity!! Guidelines for design! in any programming language!

CHAPTER 9 DESIGN ENGINEERING. Overview

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

1 Introduction 2 Complex Systems 3 Object Model 4 Dependency Management 5 Class Design. Object Oriented Programming in Physics

Principles of Software Construction: Objects, Design, and Concurrency

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

The Software Design Process. CSCE 315 Programming Studio, Fall 2017 Tanzir Ahmed

DEBUGGING: TESTING WS 2017/2018. Martina Seidl Institute for Formal Models and Verification

Modularity Guidelines for design in any programming language

Practice Problems. Review, with SOME solutions

OBJECT ORİENTATİON ENCAPSULATİON

In this Lecture you will Learn: Design Patterns. Patterns vs. Frameworks. Patterns vs. Frameworks

LECTURE 3: SOFTWARE DESIGN. Software Engineering Mike Wooldridge

INTERNAL ASSESSMENT TEST III Answer Schema

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

Chapter 2 Basic Principles of the Object-Oriented Paradigm 2.1 Abstraction

Components vs. Objects

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

Principles of Object-Oriented Design

CAS 703 Software Design

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

G Programming Languages - Fall 2012

Object-Oriented Design

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

Open Closed Principle (OCP)

LABORATORY 1 REVISION

Design Process Overview. At Each Level of Abstraction. Design Phases. Design Phases James M. Bieman

Lecture 13: Design Patterns

Advanced JML Erik Poll Radboud University Nijmegen

Abstraction. Abstraction

Object-Oriented Design

Exam Questions. Object-Oriented Design, IV1350. Maximum exam score is 100, grade limits are as follows. Score Grade 90 A 80 B 70 C 60 D 50 E

Object-Oriented Design II

Object Orientated Analysis and Design. Benjamin Kenwright

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

Use the scantron sheet to enter the answer to questions (pages 1-6)

Software Design Heuristics

Introduction to Design Patterns

Java Object Oriented Design. CSC207 Fall 2014

What are the characteristics of Object Oriented programming language?

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

Examples. Object Orientated Analysis and Design. Benjamin Kenwright

Design Patterns (Composite, Iterator)

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

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

Object Relationships

10.1 Big Objects, Business Objects, and UML Components

Lecture Chapter 2 Software Development

Elementary Concepts of Object Class

Programmazione. Prof. Marco Bertini

Object-Oriented Design

CSE 403 Lecture 8. UML Class Diagrams. Thanks to Marty Stepp, Michael Ernst, and other past instructors of CSE 403

Design Engineering. Overview

Object Oriented Technology

Information System Design (IT60105)

Part II Black-Box Composition Systems 10. Business Components in a Component-Based Development Process

Object interconnections

Assertions. Assertions - Example

SE 1: Software Requirements Specification and Analysis

Transcription:

F. Tip and M. Weintraub PRINCIPLES OF SOFTWARE DESIGN Thanks go to Andreas Zeller for allowing incorporation of his materials

THE CHALLENGE 1. Software may live much longer than expected 2. Software must be continuously adapted to a changing environment 3. Maintenance takes 50 80% of the cost Goal: Make software maintainable and reusable at little or no cost

USE THE PRINCIPLES OF OBJECT-ORIENTED DESIGN TO ACHIEVE THE GOAL 1. Abstraction 2. Encapsulation 3. Modularity 4. Hierarchy Goal: Maintainability and Reusability

PRINCIPLES OF OBJECT-ORIENTED DESIGN 1. Abstraction 2. Encapsulation 3. Modularity 4. Hierarchy

ABSTRACTION Concrete Object General Principle

ABSTRACTION 1. Highlights common properties of objects 2. Distinguishes important and unimportant properties 3. Must be understood even without a concrete object

ABSTRACTION 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 From Object Oriented Design with Applications by Grady Booch

PERSPECTIVES

EXAMPLE: SENSORS

AN ENGINEER S SOLUTION 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() } C code where values read by a sensor are directly mapped to memory locations

ABSTRACT SOLUTION interface Temperature { } interface Location { } All implementation details are hidden class TemperatureSensor { public TemperatureSensor(Location){ } public void calibrate(temperature actual){ } public Temperature currenttemperature(){ } public Location location(){ } } // private methods below

MORE ABSTRACTION

IT S A PROJECTION OF A SLIDE OF A PHOTO OF A PAINTING OF A PIPE

PRINCIPLES OF OBJECT-ORIENTED DESIGN 1. Abstraction hide details 2. Encapsulation 3. Modularity 4. Hierarchy

PRINCIPLES OF OBJECT-ORIENTED DESIGN 1. Abstraction Hide details 2. Encapsulation 3. Modularity 4. Hierarchy

ENCAPSULATION 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

GRADY BOOCH ON ENCAPSULATION 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. Grady Booch, Object-Oriented Analysis and Design with Applications, Addison-Wesley, 2007, p. 51-52

AN ACTIVE SENSOR notified when temperature class ActiveSensor { public ActiveSensor(Location) changes } public void calibrate(temperature actual){ } public Temperature currenttemperature(){ } public Location location(){ } public void register(activesensorobserver o){ } // private methods below Callback management is the sensor s secret and this illustrates how the Observer design pattern is used to avoid giving external parties access to internal state of the ActiveSensor

ANTICIPATING CHANGE Features you expect will change should be isolated in specific components Number literals String literals Presentation and interaction

NUMBER LITERALS int a[100]; for (int i = 0; i <= 99; i++) a[i] = 0;

NUMBER LITERALS int a[100]; for (int i = 0; i <= 99; i++) a[i] = 0; int SIZE = 100; int a[size]; for (int i = 0; i < SIZE; i++) a[i] = 0; int ONE_HUNDRED = 100; int a[one_hundred];

NUMBER LITERALS double sales_price = net_price * 1.06;

NUMBER LITERALS double sales_price = net_price * 1.06; final double SALES_TAX = 1.06; double sales_price = net_price * SALES_TAX;

STRING LITERALS if (sensor.temperature() > 100) System.out.println( Water is boiling! );

STRING LITERALS if (sensor.temperature() > 100) System.out.println( Water is boiling! ); if (sensor.temperature() > BOILING_POINT) System.out.println(message(BOILING_WARNING, Water is boiling! ); if (sensor.temperature() > BOILING_POINT) alarm.handle_boiling();

PRINCIPLES OF OBJECT-ORIENTED DESIGN 1. Abstraction Hide details 2. Encapsulation Keep changes local 3. Modularity 4. Hierarchy

PRINCIPLES OF OBJECT-ORIENTED DESIGN 1. Abstraction Hide details 2. Encapsulation Keep changes local 3. Modularity 4. Hierarchy

MODULARITY Basic idea: Partition a system such that parts can be designed and revised independently ( divide and conquer ) System is partitioned into modules, with each one fulfilling a specific task Modules should be changeable and reuseable independent of other modules

GRADY BOOCH ON MODULARITY Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules.

MODULE BALANCE Goal 1: Modules should hide information and expose as little as possible Goal 2: Modules should cooperate and therefore must exchange information These goals conflict with each other

PRINCIPLES OF MODULARITY High cohesion Weak coupling Law of Demeter Modules should contain functions that logically belong together Changes to modules should not affect other modules Talk only to friends

HIGH COHESION 1. Modules should contain functions that logically belong together 2. Achieved by grouping functions that work on the same data 3. Natural grouping in object oriented design

WEAK COUPLING Changes in modules should not impact other modules Achieved via 1. Information hiding 2. Depending on as few modules as possible

LAW OF DEMETER (OR: PRINCIPLE OF LEAST KNOWLEDGE) Basic idea: Assume as little as possible about other modules Approach: Restrict method calls to friends Demeter (aka Ceres) is the Greek mythical goddess of the harvest, and she presided also over the sacred law and the cycle of life and death. Proposed by Holland, Lieberherr, and Riel at Northeastern University in 1988

LoD: CALL YOUR FRIENDS 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 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 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 1. Reduces coupling between modules 2. Disallow direct access to parts 3. Limit the number of accessible classes 4. Reduce dependencies 5. Results in several new wrapper methods Demeter transmogrifiers

PRINCIPLES OF OBJECT-ORIENTED DESIGN 1. Abstraction Hide details 2. Encapsulation Keep changes local 3. Modularity Control information flow high cohesion weak coupling talk only to friends 4. Hierarchy

PRINCIPLES OF OBJECT-ORIENTED DESIGN 1. Abstraction Hide details 2. Encapsulation Keep changes local 3. Modularity Control information flow High cohesion weak coupling talk only to friends 4. Hierarchy

HIERARCHY Hierarchy is a ranking or ordering of abstractions.

CENTRAL HIERARCHIES 1. has-a hierarchy Aggregation of abstractions A car has three to four wheels 1. is-a hierarchy Generalization across abstractions An ActiveSensor is a TemperatureSensor

HIERARCHY PRINCIPLES Open/Close Principle Classes should be open for extensions Liskov Substitution Principle Subclasses should not require more, and not deliver less Dependency Principle Classes should only depend on abstractions

OPEN/CLOSE PRINCIPLE A class should be open for extension, but closed for changes Achieved via inheritance and dynamic binding

AN INTERNET CONNECTION 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

AN INTERNET CONNECTION abstract class Connection { abstract int connect(); abstract int hangup(); } class EthernetConnection extends Connection { int connect() {// does Ethernet connection; } } class ModemConnection extends Connection { int connect() {// does dial-up connection; } }

CONSIDER BILLING PLANS enum { FUN50, FUN120, FUN240,... } plan; enum { STUDENT, ADAC, ADAC_AND_STUDENT... } special; enum { PRIVATE, BUSINESS,... } customer_type; enum { T60_1, T60_60, T30_1,... } billing_increment; int compute_bill(int seconds) { if (customer_type == BUSINESS) billing_increment = T1_1; else if (plan == FUN50 plan == FUN120) billing_increment = T60_1; else if (plan == FUN240 && contract_year < 2011) billing_increment = T30_1; else billing_increment = T60_60; if (contract_year >= 2011 && special!= ADAC) billing_increment = T60_60; // etc.etc.

HIERARCHY SOLUTION You can add a new plan at any time!

HIERARCHY PRINCIPLES Open/Close principle Classes should be open for extensions Liskov substitution principle Subclasses should not require more, and not deliver less Dependency principle Classes should only depend on abstractions

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 Every circle is an ellipse Does this hierarchy make sense? No, as a circle requires more and delivers less draw() Ellipse stretchx() draw() Circle

HIERARCHY PRINCIPLES Open/Close principle Classes should be open for extensions Liskov substitution principle Subclasses should not require more, and not deliver less Dependency principle Classes should only depend on abstractions

DEPENDENCY PRINCIPLE 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 after user clicks print." void print_to_file(string filename) { if (path_exists(filename)) { // FILENAME exists; // ask user to confirm overwrite in UserPresentation bool confirmed = confirm_loss(filename); if (!confirmed) return; } } // Proceed printing to FILENAME...

CYCLIC DEPENDENCY constructing, testing, reusing individual modules becomes impossible!

DEPENDENCY // Print current Web page to FILENAME after user clicks print." 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

CHOOSING ABSTRACTION 1. Which is the dominant abstraction? 2. How does this choice impact the remaining system?

PRINCIPLES OF OBJECT-ORIENTED DESIGN 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 Abstraction Hide details Encapsulation Keep changes local Modularity Control information flow high cohesion weak coupling talk only to friends Goal: Maintainability and Reusability Hierarchy Order abstractions classes open for extensions, closed for changes subclasses that do not require more or deliver less depend only on abstractions