CS 520 Theory and Practice of Software Engineering Fall 2018

Similar documents
CS 520 Theory and Practice of Software Engineering Fall 2017

CS 320 Introduction to Software Engineering Spring March 06, 2017

CS 320 Introduction to Software Engineering Spring March

CS 320 Introduction to Software Engineering Spring 2017

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

Software Engineering

Liskov Substitution Principle

Inheritance and object compatibility

CS 520 Theory and Practice of Software Engineering Fall 2017

Inheritance and Polymorphism

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 520 Theory and Practice of Software Engineering Fall 2018

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

Java Object Oriented Design. CSC207 Fall 2014

Introduction to Object-Oriented Programming

Object Oriented Software Design - I

CS-202 Introduction to Object Oriented Programming

Inheritance. Transitivity

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

ITI Introduction to Computing II

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

CS 251 Intermediate Programming Inheritance

Example: Count of Points

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Object Oriented Programming. Java-Lecture 11 Polymorphism

COMP1008 An overview of Polymorphism, Types, Interfaces and Generics

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

ITI Introduction to Computing II

Example: Count of Points

Intro to: Design Principles

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

C12a: The Object Superclass and Selected Methods

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Lecture: Modular Design

OO Design Principles

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

COURSE 2 DESIGN PATTERNS

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

CS111: PROGRAMMING LANGUAGE II

First IS-A Relationship: Inheritance

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

CMSC 132: Object-Oriented Programming II

Programming II (CS300)

Programming in C# Inheritance and Polymorphism

11/2/09. Code Critique. What goal are we designing to? What is the typical fix for code smells? Refactoring Liskov Substitution Principle

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Inheritance and Substitution (Budd chapter 8, 10)

Polymorphism and Interfaces. CGS 3416 Spring 2018

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Exercise: Singleton 1

Goals of the Lecture OO Programming Principles

Inheritance and Interfaces

More Relationships Between Classes

Chapter 10 Classes Continued. Fundamentals of Java

The class Object. Lecture CS1122 Summer 2008

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

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Practice for Chapter 11

Need to store a list of shapes, each of which could be a circle, rectangle, or triangle

CSC207 Week 3. Larry Zhang

Principles of Object-Oriented Design

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Chapter 11 Classes Continued

CS Programming I: Inheritance

S.O.L.I.D: Software Engineering Principles

CH. 2 OBJECT-ORIENTED PROGRAMMING

GRASP Design Patterns A.A. 2018/2019

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

L4: Inheritance. Inheritance. Chapter 8 and 10 of Budd.

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

Last class. -More on polymorphism -super -Introduction to interfaces

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

Inheritance, Polymorphism, and Interfaces

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Computer Science II (20073) Week 1: Review and Inheritance

Introduction to Inheritance

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Introduction to Object Oriented Programming. (Hebrew University, CS / Spring 2012 )

CIS 110: Introduction to computer programming

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Principles of Software Construction: Objects, Design, and Concurrency. Part 1: Designing Classes. Design for Reuse School of Computer Science

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

OO Class Design Principles

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

!" #$ +-,.-% $/00&&& $ 0$0

Concepts of Programming Languages

Object-oriented Programming. Object-oriented Programming

Motivations. Chapter 13 Abstract Classes and Interfaces

Transcription:

Today CS 520 Theory and Practice of Software Engineering Fall 2018 Object Oriented (OO) Design Principles September 13, 2018 Code review and (re)design of an MVC application (and encapsulation) Polymorphism Open/closed principle Inheritance in Java The diamond of death Liskov substitution principle Composition/aggregation over inheritance Let s review the code of the following application Source code available on the course web site MyClass public class MyClass { public int nelem; public int capacity; public int top; public int[] elems; public boolean canresize; public void resize(int s){ public void push(int e){ public int capacityleft(){ public int getnumelem(){ public int pop(){ public int[] getelems(){ MyClass public class MyClass { public int nelem; public int capacity; public int top; public int[] elems; public boolean canresize; public void resize(int s){ public void push(int e){ public int capacityleft(){ public int getnumelem(){ public int pop(){ public int[] getelems(){ What does MyClass do? 1

public class { public int nelem; public int capacity; public int top; public int[] elems; public boolean canresize; public void resize(int s){ public void push(int e){ public int capacityleft(){ public int getnumelem(){ public int pop(){ public int[] getelems(){ Anything that could be improved in this implementation? - elems : int[] : Reveal as little information about internals as possible. Separate public interface from implementation details. Reduce complexity. vs. visibility vs. visibility Public Public??? Private??? Private Protected, package-private, or friend-accessible (C++). Not part of the public API. Implementation detail that a subclass/friend may rely on. A little refresher: what is Polymorphism? 2

A little refresher: what is Polymorphism? An object s ability to provide different behaviors. Types of polymorphism Ad-hoc polymorphism (e.g., operator overloading) a + b String vs. int, double, etc. Subtype polymorphism (e.g., method overriding) Object obj = ; tostring() can be overridden in subclasses obj.tostring(); and therefore provide a different behavior. Parametric polymorphism (e.g., Java generics) class <E> { A can store elements void add(e) { regardless of their type but still E get(int index) { provide full type safety. A little refresher: what is Polymorphism? An object s ability to provide different behaviors. Types of polymorphism Subtype polymorphism (e.g., method overriding) Object obj = ; tostring() can be overridden in subclasses obj.tostring(); and therefore provide a different behavior. Subtype polymorphism is essential to many. Open/closed principle Software entities (classes, components, etc.) should be: open for extensions closed for modifications public static void draw(object o) { if (o instanceof ) { draw(() o) else if (o instanceof Circle) { drawcircle((circle) o); else { Good or bad design? + draw() Circle + drawcircle() Open/closed principle Software entities (classes, components, etc.) should be: open for extensions closed for modifications public static void draw(object o) { if (o instanceof ) { draw(() o) else if (o instanceof Circle) { drawcircle((circle) o); else { Violates the open/closed principle! + draw() Circle + drawcircle() Open/closed principle Software entities (classes, components, etc.) should be: open for extensions closed for modifications public static void draw(object s) { if (s instanceof Shape) { s.draw(); else { public static void draw(shape s) { s.draw(); Shape + draw() Circle 3

{abstract {abstract {abstract Deque, Deque Iterable Collection {abstract Deque 4

Iterable Collection Iterable, Collection {abstract Iterable Collection Deque The diamond of death : the problem A a = new D(); int num = a.getnum(); A D C The diamond of death : the problem The diamond of death : concrete example A Animal + canfly():bool A a = new D(); int num = a.getnum(); B C Bird + canfly():bool Horse + canfly():bool Which getnum() method should be called? D Pegasus Can this happen in Java? Yes, with default methods in Java 8. 5

Motivating example We know that a square is a special kind of a rectangle. So, which of the following OO designs makes sense? Is the subtype requirement fulfilled? r = new (2,2); int A = r.getarea(); int w = r.getwidth(); r.setwidth(w * 2); assertequals(a * 2, r.getarea()); r = new (2,2); new (2); int A = r.getarea(); int w = r.getwidth(); r.setwidth(w * 2); assertequals(a * 2, r.getarea()); r = new (2,2); new (2); int A = r.getarea(); int w = r.getwidth(); r.setwidth(w * 2); assertequals(a * 2, r.getarea()); Violates the Liskov substitution principle! 6

Shape Inheritance vs. (Aggregation vs. Composition) Person Customer Room Design choice: inheritance or composition? Student Bank Building public class Student Person{ public Student(){ is-a relationship public class Bank { Customer c; public Bank(Customer c){ this.c = c; has-a relationship public class Building { Room r; public Building(){ this.r = new Room(); public class <E> <E> { public class <E> <E> { private <E> l = new <>(); Hmm, both designs seem valid -- what are pros and cons? Design choice: inheritance or composition? : summary Pros No delegation methods required. Reuse of common state and behavior. Cons Exposure of all inherited methods (a client might rely on this particular superclass -> can t change it later). Changes in superclass are likely to break subclasses. Pros Highly flexible and configurable: no additional subclasses required for different compositions. Cons All interface methods need to be implemented -> delegation methods required, even for code reuse. Composition/aggregation over inheritance allows more flexibility. 7