OO Design Principles
|
|
- Allen Hunter
- 5 years ago
- Views:
Transcription
1 OO Design Principles Software Architecture VO ( ) Roman Kern Institute for Interactive Systems and Data Science, TU Graz Roman Kern (ISDS, TU Graz) OO Design Principles / 69
2 Outline 1 Introduction 2 Open-Closed Principle 3 Liskov Substitution Princple 4 Single Responsibility Principle 5 Law of Demeter 6 Enterprise Architecture Principles Roman Kern (ISDS, TU Graz) OO Design Principles / 69
3 Introduction Overview of OO principles Roman Kern (ISDS, TU Graz) OO Design Principles / 69
4 Software Design Design is the process of inventing new software entities to satisfy requirements Software entities might be different things depending on the level of granularity and programming paradigm Procedural programming: procedures, variables, OO programming: classes, objects, relations between them, Roman Kern (ISDS, TU Graz) OO Design Principles / 69
5 Software Design Design is not an exact entity For example, you can calculate how fast is an algorithm But you cannot measure how good a design is However, there are certain design principles and design rules that you should follow Roman Kern (ISDS, TU Graz) OO Design Principles / 69
6 Software Design These design principles come from the experience of numerous programmers Some of the experiences can be built directly into a programming language E.g. absence of direct multiple inheritance in Java It was a design decision to leave it out of the language because of problems it might cause Roman Kern (ISDS, TU Graz) OO Design Principles / 69
7 OO design principles The design principles that we will discuss lay the foundation of solid OO practices: 1 Open-Closed principle 2 Design by contract/liskov substitution principle 3 Single responsibility principle 4 Law of Demeter Roman Kern (ISDS, TU Graz) OO Design Principles / 69
8 Open-Closed Principle Open for extension, closed for modification Roman Kern (ISDS, TU Graz) OO Design Principles / 69
9 Open-Closed Principle: Example Online publication system An online publication system allows users to search in publications by the year of publication. Search functionality is supported by a search filter that filters publications by comparing their publication year with a user query. Roman Kern (ISDS, TU Graz) OO Design Principles / 69
10 Open-Closed Principle: Example public class Filter { public List filteronyearofpublication(collection pubs, int year) { List<Publication> filtered = new ArrayList<Publication>(); for (Publication pub : pubs) { if (pub.getyearofpublication() == year) { filtered.add(pub); return filtered; Roman Kern (ISDS, TU Graz) OO Design Principles / 69
11 Open-Closed Principle: Example Online publication system Extend the search filter to also filter by the title. Roman Kern (ISDS, TU Graz) OO Design Principles / 69
12 Open-Closed Principle: Example public class Filter {... public List filterontitleofpublication(collection pubs, String title) { List<Publication> filtered = new ArrayList<Publication>(); for (Publication pub : pubs) { if (pub.gettitleofpublication().equals(title)) { filtered.add(pub); return filtered; Roman Kern (ISDS, TU Graz) OO Design Principles / 69
13 Open-Closed Principle This is the cornerstone principle of OOD All software changes during its life cycle Programmers face ever changing functional requirements But need to design software that is stable (i.e. does not change) Software entities should be open for extension, but closed for modification by B. Meyer Roman Kern (ISDS, TU Graz) OO Design Principles / 69
14 Open-Closed Principle In OO terms Classes should be open for extension, but closed for modification In other words you should be able to extend a class without modifying it Open for extension: behaviour of classes can be extended to satisfy changing requirements Closed for modification: classes themselves are not allowed to change Roman Kern (ISDS, TU Graz) OO Design Principles / 69
15 Open-Closed Principle At first this seems contradictory If you need a modified behaviour of a class just change that class This is however wrong The answer here is abstraction In OO it is possible to create abstractions with fixed design but limitless behaviours Roman Kern (ISDS, TU Graz) OO Design Principles / 69
16 Open-Closed Principle Abstractions through inheritance - changed behaviour through polymorphism Inheritance: abstract base classes or interfaces You must decide early on in the design which parts of the system will be expanded later and which will stay fixed Roman Kern (ISDS, TU Graz) OO Design Principles / 69
17 Open-Closed Principle The design is extended by adding new code and classes by inheriting existing base classes Modifying the existing classes is not needed Completed and tested code is declared closed, and it is never modified Roman Kern (ISDS, TU Graz) OO Design Principles / 69
18 Open-Closed Principle: Example We violated the OCP before To change the behaviour we changed the base class Both methods differ only in one statement: selection statement This is where we need to design for change We need to refactor Roman Kern (ISDS, TU Graz) OO Design Principles / 69
19 Open-Closed Principle: Example public class Filter { public Vector filterpublication(collection pubs, Selector selector) { List<Publication> filtered = new ArrayList<Publication>(); for (Publication pub : pubs) { if (selector.ispubselected(pub)) { filtered.add(pub); return filtered; Roman Kern (ISDS, TU Graz) OO Design Principles / 69
20 Open-Closed Principle: Example public interface Selector { public boolean ispubselected(publication pub); Roman Kern (ISDS, TU Graz) OO Design Principles / 69
21 Open-Closed Principle: Example public class YearSelector implements Selector { private final int year; public YearSelector(int year) { this.year = year; public boolean ispubselected(publication pub) { if (pub.getyearofpublication() == year) { return true; return false; Roman Kern (ISDS, TU Graz) OO Design Principles / 69
22 Open-Closed Principle: Example public class TitleSelector implements Selector { private final String title; public TitleSelector(String title) { this.title = title; public boolean ispubselected(publication pub) { if (pub.gettitleofpublication().equals(title)) { return true; return false; Roman Kern (ISDS, TU Graz) OO Design Principles / 69
23 Open-Closed Principle Now filtering is open for extension, i.e. adding filters for author, pub type Still filtering is closed for modification - no need to modify the Filter class With the class being open for extension, it is highly reusable and can be used to satisfy a ever changing list of requirements The class is extremely maintainable, as it never needs to change Roman Kern (ISDS, TU Graz) OO Design Principles / 69
24 Disadvantages of instanceof instanceof operator in Java checks whether an object is of a certain type Typical use of this operator is in if-elseif-else or switch type statement Depending on the type of an object you initiate actions Roman Kern (ISDS, TU Graz) OO Design Principles / 69
25 Disadvantages of instanceof... public void cookpizza(pizza pizza) { if (pizza instanceof ThinCrustPizza) { ((ThinCrustPizza)pizza).cookInWoodFireOven(); else if (pizza instanceof PanPizza) { ((PanPizza)pizza).cookInGreasyPan();... Note: At least have an else throw new IllegalArgumentException(); Roman Kern (ISDS, TU Graz) OO Design Principles / 69
26 Disadvantages of instanceof This is a direct violation of OCP This class can not be extended without modifying it Suppose there is a new kind of pizza You need to extend the above class with a new elseif that handles the new type Roman Kern (ISDS, TU Graz) OO Design Principles / 69
27 Disadvantages of instanceof You can always design your classes so that they do not need instanceof operator The key again is inheritance + polymorphism In certain cases you will need an extra design construct But it pays off because you get the benefits of the OCP Roman Kern (ISDS, TU Graz) OO Design Principles / 69
28 Disadvantages of instanceof abstract public Pizza { public void cook() { placeoncookingsurface(); placeincookingdevice(); int cooktime = getcooktime(); letitcook(cooktime); removefromcookingdevice(); abstract public void placeincookingdevice();... Roman Kern (ISDS, TU Graz) OO Design Principles / 69
29 Disadvantages of instanceof... public void cookpizza(pizza pizza) { pizza.cook();... Roman Kern (ISDS, TU Graz) OO Design Principles / 69
30 Disadvantages of instanceof We applied an extra design construct here: Template Method cook() method defines a general cook algorithm It calls a number of template methods that are abstract Subclasses need to implement that behaviour by implementing template methods Roman Kern (ISDS, TU Graz) OO Design Principles / 69
31 Liskov Substitution Princple Preconditions, Postconditions & Invariants Roman Kern (ISDS, TU Graz) OO Design Principles / 69
32 Design by contract Developed by B. Meyer Its a contract between a class and its clients (other classes that use it) Specifies rules that need to be satisfied by the class and by the clients i.e., an expression that evaluates to true Preconditions and postconditions for methods, invariants for the class Roman Kern (ISDS, TU Graz) OO Design Principles / 69
33 Design by contract Example: Stack class Precondition for push(x) method: the stack is non-full Postcondition for push(x) method: x is on the top of the stack Invariants: stack size is non-negative, count is less than max_size, Roman Kern (ISDS, TU Graz) OO Design Principles / 69
34 Design by contract In some languages you can formally specify such rules With predicate logic you can even check the correctness of such rules Certain approaches to generate code from such specifications In Java: assert keyword to check if rules are satisfied: Testing Roman Kern (ISDS, TU Graz) OO Design Principles / 69
35 Liskov Substitution Principle Closely related with Design by Contract Depends on preconditions and postconditions Definition If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behaviour of P is unchanged when o1 is substituted for o2 then S is a subtype of T. Roman Kern (ISDS, TU Graz) OO Design Principles / 69
36 Liskov Substitution Principle What does it mean? Anywhere you specify a base type, you should be able to use an instance of a derived type. Roman Kern (ISDS, TU Graz) OO Design Principles / 69
37 Liskov Substitution Principle But with polymorphism we can do it anyway, right? That is true but here we are concerned with the program correctness We are concerned with the question what happens when subclasses reimplement (override) methods The overridden methods need to satisfy rules specified by the superclass Roman Kern (ISDS, TU Graz) OO Design Principles / 69
38 Liskov Substitution Principle public interface Coffee { public CaffeineContent getcaffeinecontent(); public class EthiopianCoffee { public CaffeineContent getcaffeinecontent(){ return new CaffeineContent(100); public class DecafCoffee { public CaffeineContent getcaffeinecontent(){ return null; Roman Kern (ISDS, TU Graz) OO Design Principles / 69
39 Liskov Substitution Principle Then this code will break in some cases (postcondition was not satisfied)... public void buycoffee(coffee coffee) { CaffeineContent caf = coffee.getcaffeinecontent(); if(caf.isstrong()) { Roman Kern (ISDS, TU Graz) OO Design Principles / 69
40 Liskov Substitution Principle But is DecafCoffee really Coffee? Maybe we need to reconsider (refactor) our class hierarchy For example, HotBeverage as superclass Coffee and DecafCoffee as subclasses at the same level of the hierarchy Roman Kern (ISDS, TU Graz) OO Design Principles / 69
41 Liskov Substitution Principle Rule: It is allowed to weaken preconditions and strengthen postconditions in overridden methods, but not vice versa If you can not satisfy this rule then you need to refactor the class hierarchy Typically, those two types should be in the same level of the hierarchy There is a superclass for both of these classes Roman Kern (ISDS, TU Graz) OO Design Principles / 69
42 Liskov Substitution Principle Bank Accounts A bank manages different types of accounts for its customers. There is a standard current account that can be closed only if the balance on that account is positive. To close another type of account, a so-called timed current account, the balance needs to be positive and it needs to be open for at least 6 months. Roman Kern (ISDS, TU Graz) OO Design Principles / 69
43 Liskov Substitution Principle public class CurrentAccount { protected double balance = 0.0; public boolean close() { if (balance > 0.0) { return true; return false;... Roman Kern (ISDS, TU Graz) OO Design Principles / 69
44 Liskov Substitution Principle public class TimedCurrentAccount extends CurrentAccount { private int date = 0; public boolean close() { if ((balance > 0.0) && (date > 180) { return true; return false; Roman Kern (ISDS, TU Graz) OO Design Principles / 69
45 Liskov Substitution Principle public class Customer {... public void manageaccount(currentaccount account) { account.close(); // dangerous Roman Kern (ISDS, TU Graz) OO Design Principles / 69
46 Liskov Substitution Principle Customer knows that the account balance is positive A TimedCurrentAccount however will not be always closed What is the problem here? We strengthened precondition It was: balance > 0 Now: balance > 0 + more than 6 months since we opened the account Roman Kern (ISDS, TU Graz) OO Design Principles / 69
47 Liskov Substitution Principle Well, TimedCurrentAccount is not really a subtype of CurrentAccount Refactor: Account is the superclass with close() method and stronger preconditions CurrentAccount is a subclass that weakens preconditions TimedCurrentAccount is a subclass with the same precondition We can substitute CurrentAcount and TimedCurrentAccount for Account But we can not substitute CurrentAccount and TimedCurrentAccount, nor should we! Roman Kern (ISDS, TU Graz) OO Design Principles / 69
48 Liskov Substitution Principle Collection library Implement a collection library with list and set collections. Both of the collections store arbitrary number of objects. Lists store objects in a sequence. Sets can not have duplicate objects. A collection can be copied into another collection. This includes copying of all objects from one collection to another collection. Roman Kern (ISDS, TU Graz) OO Design Principles / 69
49 Liskov Substitution Principle public class List {... public void add(object element) { elements[count++] = element; // copy arrays public Object get(int i){ return elements[i]; public boolean haselement(object element){ Roman Kern (ISDS, TU Graz) OO Design Principles / 69
50 Liskov Substitution Principle... public void copy(list dest) { for(int i=0; i < elements.length; i++) { dest.add(elements[i]); Roman Kern (ISDS, TU Graz) OO Design Principles / 69
51 Liskov Substitution Principle public class Set extends List{... // we inherit other methods public void add(object element) { if(!haselement(element)) { elements[count++] = element; // copy arrays Roman Kern (ISDS, TU Graz) OO Design Principles / 69
52 Liskov Substitution Principle Postcondition: all objects should be copied Postcondition of copy() method is violated We have a list: 1, 2, 3, 1, 2 When we copy it into a set: 1, 2, 3 Violation! Set is not really a List! Roman Kern (ISDS, TU Graz) OO Design Principles / 69
53 Liskov Substitution Principle Consequence of LSP You should be very careful with overriding Very often you will need to refactor the class hierarchy Overriding is inheritance with reuse of implementation Recollect inheritance should be reuse of interface Better: use abstract base classes and interfaces to ensure reuse of interface Roman Kern (ISDS, TU Graz) OO Design Principles / 69
54 Single Responsibility Principle Do one thing only Roman Kern (ISDS, TU Graz) OO Design Principles / 69
55 Single Responsibility Principle An entity (an object, a class) should have only a single responsibility A responsibility is in fact a reason to change An entity should have only single reason to change Roman Kern (ISDS, TU Graz) OO Design Principles / 69
56 Single Responsibility Principle public class Student {... public void readstudent(int id) { // read student data from a database... public String printhtml() { // print student data in a nice HTML format... Roman Kern (ISDS, TU Graz) OO Design Principles / 69
57 Single Responsibility Principle Student class has two responsibilities: reading from a database and printing HTML Two reasons for change: if a table is modified and if another HTML format is needed We need to separate responsibilities: reading and printing This is also very often called: separation of concerns Roman Kern (ISDS, TU Graz) OO Design Principles / 69
58 Single Responsibility Principle public class Student {... public void readstudent(int id) { // read student data from a database... Roman Kern (ISDS, TU Graz) OO Design Principles / 69
59 Single Responsibility Principle public class StudentPrinter {... public String printstudent(student student) { // print in a nice HTML format... Roman Kern (ISDS, TU Graz) OO Design Principles / 69
60 Law of Demeter Which methods to call Roman Kern (ISDS, TU Graz) OO Design Principles / 69
61 Law of Demeter A method of an object should only invoke methods of: 1 Itself 2 Its parameters 3 Objects it creates 4 Its members Methods should not invoke methods of other objects members Roman Kern (ISDS, TU Graz) OO Design Principles / 69
62 Law of Demeter public class StudentPrinter {... public String printstudent(student student) {... // violation String course = student.getcourse(i).getname(); Roman Kern (ISDS, TU Graz) OO Design Principles / 69
63 Law of Demeter public class Student {... public String getcoursename(int i) { return courses[i].getname(); Roman Kern (ISDS, TU Graz) OO Design Principles / 69
64 Law of Demeter Chaining of method calls is dangerous because you depend on all parts of that chain Provide a method in Student class that gets you names of the courses That method delegates to the Course class If the Course class changes only Student class needs to be updated not StudentPrinter Consequence for composition/delegation: only delegate to own members! Roman Kern (ISDS, TU Graz) OO Design Principles / 69
65 Enterprise Architecture Principles Collection of best practices Roman Kern (ISDS, TU Graz) OO Design Principles / 69
66 Enterprise Architecture Principles Reuse Existing components must be reused where applicable Instead of developing them from scratch In practice there is often a trade-off, as the existing component might not exactly match the requirements Optionally, extend the existing component Roman Kern (ISDS, TU Graz) OO Design Principles / 69
67 Enterprise Architecture Principles Buy rather than build If a component exist (commercially or open-source) that match the requirements, it should rather be used Unless there are strategical (e.g. competition) or technical issues Motivation: higher quality of existing solutions, might provide extra functionality Often, it is cheaper to use existing solutions instead of developing them Roman Kern (ISDS, TU Graz) OO Design Principles / 69
68 Enterprise Architecture Principles Single point of view If there are multiple sources of data (e.g. file-system and database system) Develop components to make them appear as a single source Motivation: will lead to more simple components (that use the source), logic to combine the sources is bundled in a single place Roman Kern (ISDS, TU Graz) OO Design Principles / 69
69 The End Next: Analysis and Design Roman Kern (ISDS, TU Graz) OO Design Principles / 69
CSC207H: Software Design Lecture 6
CSC207H: Software Design Lecture 6 Wael Aboelsaadat wael@cs.toronto.edu http://ccnet.utoronto.ca/20075/csc207h1y/ Office: BA 4261 Office hours: R 5-7 Acknowledgement: These slides are based on material
More informationAdvanced JML Erik Poll Radboud University Nijmegen
JML p.1/23 Advanced JML Erik Poll Radboud University Nijmegen JML p.2/23 Core JML Remember the core JML keywords were requires ensures signals invariant non null pure \old, \forall, \result JML p.3/23
More informationType Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004
Type Hierarchy Comp-303 : Programming Techniques Lecture 9 Alexandre Denault Computer Science McGill University Winter 2004 February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 1 Last lecture...
More informationCS 215 Software Design Sample midterm solutions
Software Design Sample midterm solutions 1. The administration at Happy Valley School District is redesigning the software that manages information about its students. It has identified an abstract class
More informationToday's Agenda. References. Open Closed Principle. CS 247: Software Engineering Principles. Object-Oriented Design Principles
CS 247: Software Engineering Principles Reading: none Object-Oriented Design Principles Today's Agenda Object-Oriented Design Principles - characteristics, properties, and advice for making decisions that
More informationIndex. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107
A abbreviations 17 abstract class 105 abstract data types 105 abstract method 105 abstract types 105 abstraction 92, 105 access level 37 package 114 private 115 protected 115 public 115 accessors 24, 105
More informationWhy Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012
Why Design by Contract What s the difference with Testing? CS 619 Introduction to OO Design and Development Design by Contract Fall 2012 Testing tries to diagnose (and cure) defects after the facts. Design
More informationAssertions. Assertions - Example
References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 11/13/2003 1 Assertions Statements about input to a routine or state of a class Have two primary roles As documentation,
More informationCSC Advanced Object Oriented Programming, Spring Specification
CSC 520 - Advanced Object Oriented Programming, Spring 2018 Specification Specification A specification is an unambiguous description of the way the components of the software system should be used and
More informationCS 520 Theory and Practice of Software Engineering Fall 2018
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
More informationOutline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism
Subtype, Subtyping vs. Subclassing, Liskov Substitution Principle Outline Subtype polymorphism Subtyping vs. subclassing Liskov Substitution Principle (LSP) Function subtyping Java subtyping Composition:
More informationSoftware Engineering
Software Engineering CSC 331/631 - Spring 2018 Object-Oriented Design Principles Paul Pauca April 10 Design Principles DP1. Identify aspects of the application that vary and separate them from what stays
More informationSoftware Engineering CSC40232: SOFTWARE ENGINEERING. Guest Lecturer: Jin Guo SOLID Principles sarec.nd.edu/courses/se2017
CSC40232: SOFTWARE ENGINEERING Guest Lecturer: Jin Guo SOLID Principles sarec.nd.edu/courses/se2017 Department of Computer Science and Engineering http://www.kirkk.com/modularity/2009/12/solid-principles-of-class-design/
More information6.170 Recitation #5: Subtypes and Inheritance
6.170 Recitation #5: Subtypes and Inheritance What is true subtyping? True Subtyping is not exactly the same as Inheritance. As seen in an earlier lecture, class A is a true subtype of class B if and only
More informationReferences: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1
References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1 Assertions Statements about input to a routine or state of a class Have two primary roles As documentation,
More informationInheritance and object compatibility
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not the other way around Examples: reference/pointer can
More informationCS 520 Theory and Practice of Software Engineering Fall 2017
CS 520 Theory and Practice of Software Engineering Fall 2017 OO design principles September 14, 2017 Today Code review and (re)design of an MVC application OO design principles Information hiding (and
More informationIntroduction to Object-Oriented Programming
Polymorphism 1 / 19 Introduction to Object-Oriented Programming Today we ll learn how to combine all the elements of object-oriented programming in the design of a program that handles a company payroll.
More informationn 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
Subtype, Subtyping vs. Subclassing, Liskov Substitution Principle Announcements 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
More informationObject-Oriented Design
Object-Oriented Design Lecturer: Raman Ramsin Lecture 15: Object-Oriented Principles 1 Open Closed Principle (OCP) Classes should be open for extension but closed for modification. OCP states that we should
More informationType Hierarchy. Lecture 6: OOP, autumn 2003
Type Hierarchy Lecture 6: OOP, autumn 2003 The idea Many types have common behavior => type families share common behavior organized into a hierarchy Most common on the top - supertypes Most specific at
More informationObject-Oriented Design
Object-Oriented Design Department of Computer Engineering Lecture 12: Object-Oriented Principles Sharif University of Technology 1 Open Closed Principle (OCP) Classes should be open for extension but closed
More informationAdvanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II
Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II Mahmoud El-Gayyar elgayyar@ci.suez.edu.eg Ad hoc-polymorphism Outline Method overloading Sub-type Polymorphism Method overriding Dynamic
More informationPrinciples of Object-Oriented Design
Principles of Object-Oriented Design 1 The Object-Oriented... Hype What are object-oriented (OO) methods? OO methods provide a set of techniques for analysing, decomposing, and modularising software system
More informationa correct statement? You need to know what the statement is supposed to do.
Using assertions for correctness How can we know that software is correct? It is only correct if it does what it is supposed to do. But how do we know what it is supposed to do? We need a specification.
More informationAP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS
AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS PAUL L. BAILEY Abstract. This documents amalgamates various descriptions found on the internet, mostly from Oracle or Wikipedia. Very little of this
More informationAnnouncements. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Intuition: Type Signature is a Specification.
Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle Announcements HW5 is due today, March 26, 2018 at 11:59:59 pm Exam 2 is next Thursday, March 29, 2018. Practice exam has been
More information17. Assertions. Outline. Built-in tests. Built-in tests 3/29/11. Jelle Slowack, Bart Smets, Glenn Van Loon, Tom Verheyen
17. Assertions Jelle Slowack, Bart Smets, Glenn Van Loon, Tom Verheyen Outline Introduction (BIT, assertion, executable assertion, why?) Implementation-based vs responsability-based assertions Implementation
More information17. Assertions. Jelle Slowack, Bart Smets, Glenn Van Loon, Tom Verheyen
17. Assertions Jelle Slowack, Bart Smets, Glenn Van Loon, Tom Verheyen Outline Introduction (BIT, assertion, executable assertion, why?) Implementation-based vs responsability-based assertions Implementation
More informationInheritance and Substitution (Budd chapter 8, 10)
Inheritance and Substitution (Budd chapter 8, 10) 1 2 Plan The meaning of inheritance The syntax used to describe inheritance and overriding The idea of substitution of a child class for a parent The various
More informationDesign Principles: Part 2
Liskov Substitution Principle (LSP) Dependency-Inversion Principle (DIP) Interface-Segregation Principle (ISP) Design Principles: Part 2 ENGI 5895: Software Design Andrew Vardy Faculty of Engineering &
More information11/2/09. Code Critique. What goal are we designing to? What is the typical fix for code smells? Refactoring Liskov Substitution Principle
Code Critique Identifying smells Refactoring Liskov Substitution Principle What goal are we designing to? What is the typical fix for code smells? What is a limitation of those fixes? How do we address
More informationOutline. Design Principles: Part 2. e.g. Rectangles and Squares. The Liskov Substitution Principle (LSP) ENGI 5895: Software Design.
Liskov Substitution Principle (LSP) Dependency-Inversion Principle (DIP) Interface-Segregation Principle (ISP) Liskov Substitution Principle (LSP) Dependency-Inversion Principle (DIP) Interface-Segregation
More informationObject Oriented Issues in VDM++
Object Oriented Issues in VDM++ Nick Battle, Fujitsu UK (nick.battle@uk.fujitsu.com) Background VDMJ implemented VDM-SL first (started late 2007) Formally defined. Very few semantic problems VDM++ support
More informationReadability [Skrien 4.0] Programs must be written for people to read, and only incidentally for machines to execute.
Readability [Skrien 4.0] Programs must be written for people to read, and only incidentally for machines to execute. Abelson & Sussman Use a good set of coding conventions, such as the ones given in the
More informationCSE 70 Final Exam Fall 2009
Signature cs70f Name Student ID CSE 70 Final Exam Fall 2009 Page 1 (10 points) Page 2 (16 points) Page 3 (22 points) Page 4 (13 points) Page 5 (15 points) Page 6 (20 points) Page 7 (9 points) Page 8 (15
More informationName Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?
1. Overriding Methods A subclass can modify behavior inherited from a parent class. A subclass can create a method with different functionality than the parent s method but with the same: Name Return type
More informationHAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.
HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all
More informationAbstraction. Design fundamentals in OO Systems. Fundamental Software Development Principles
Abstraction Design fundamentals in OO Systems Tool for abstraction: object Object structure: properties and values for those properties operations to query and update those properties We refer to the collection
More informationHighlights of Previous Lecture
Highlights of Previous Lecture Final Project Goals 1. Set up collections of Flights 2. Maintain information about reservation availability on flights 3. Respond to reservation requests 4. Set up collections
More informationSummary of the course lectures
Summary of the course lectures 1 Components and Interfaces Components: Compile-time: Packages, Classes, Methods, Run-time: Objects, Invocations, Interfaces: What the client needs to know: Syntactic and
More informationContracts. Dr. C. Constantinides. June 5, Department of Computer Science and Software Engineering Concordia University Montreal, Canada 1/71
Contracts Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada June 5, 2018 1/71 Contracts in human affairs In human affairs we form legally
More informationSoftware Engineering Design & Construction
Winter Semester 16/17 Software Engineering Design & Construction Dr. Michael Eichberg Fachgebiet Softwaretechnik Technische Universität Darmstadt Liskov Substitution Principle 2 Liskov Substitution Principle
More information[ L5P1] Object-Oriented Programming: Advanced Concepts
[ L5P1] Object-Oriented Programming: Advanced Concepts Polymorphism Polymorphism is an important and useful concept in the object-oriented paradigm. Take the example of writing a payroll application for
More informationC12a: The Object Superclass and Selected Methods
CISC 3115 TY3 C12a: The Object Superclass and Selected Methods Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/4/2018 CUNY Brooklyn College 1 Outline The Object class and
More informationDesign Principles: Part 2
Liskov Substitution Principle (LSP) Dependency-Inversion Principle (DIP) Interface-Segregation Principle (ISP) Design Principles: Part 2 ENGI 5895: Software Design Andrew Vardy Faculty of Engineering &
More informationChapter 5 Object-Oriented Programming
Chapter 5 Object-Oriented Programming Develop code that implements tight encapsulation, loose coupling, and high cohesion Develop code that demonstrates the use of polymorphism Develop code that declares
More informationObject-Oriented Design II
Object-Oriented Design II SWEN-261 Introduction to Software Engineering Department of Software Engineering Rochester Institute of Technology Controller Pure fabrication Open/close Polymorphism Liskov substitution
More informationLast Time: Object Design. Comp435 Object-Oriented Design. Last Time: Responsibilities. Last Time: Creator. Last Time: The 9 GRASP Patterns
Last Time: Object Design Comp435 Object-Oriented Design Week 7 Computer Science PSU HBG The main idea RDD: Responsibility-Driven Design Identify responsibilities Assign them to classes and objects Responsibilities
More informationCS 320 Introduction to Software Engineering Spring March 06, 2017
CS 320 Introduction to Software Engineering Spring 2017 March 06, 2017 Recap: types of Polymorphism Recap: types of Polymorphism Ad-hoc polymorphism (e.g., operator overloading) a + b String vs. int, double,
More informationCS 215 Software Design Sample Midterm Questions
Software Design 1. The administration at Happy Valley School District is redesigning the software that manages information about its students. It has identified an abstract class Student, with two subclasses:
More informationInheritance. One class inherits from another if it describes a specialized subset of objects Terminology:
Inheritance 1 Inheritance One class inherits from another if it describes a specialized subset of objects Terminology: the class that inherits is called a child class or subclass the class that is inherited
More informationAssertions, pre/postconditions
Programming as a contract Assertions, pre/postconditions Assertions: Section 4.2 in Savitch (p. 239) Specifying what each method does q Specify it in a comment before method's header Precondition q What
More informationObject-Oriented Concepts and Design Principles
Object-Oriented Concepts and Design Principles Signature Specifying an object operation or method involves declaring its name, the objects it takes as parameters and its return value. Known as an operation
More informationPROCESS DEVELOPMENT METHODOLOGY The development process of an API fits the most fundamental iterative code development
INTRODUCING API DESIGN PRINCIPLES IN CS2 Jaime Niño Computer Science, University of New Orleans New Orleans, LA 70148 504-280-7362 jaime@cs.uno.edu ABSTRACT CS2 provides a great opportunity to teach an
More informationSubtypes and Subclasses
Subtypes and Subclasses 6.170 Lecture 14 Fall - ocw.mit.edu Reading: Chapter 7 of Program Development in Java by Barbara Liskov 1 Subtypes We have used closed arrows in module dependence diagrams and object
More informationChapter 11 Classes Continued
Chapter 11 Classes Continued The real power of object-oriented programming comes from its capacity to reduce code and to distribute responsibilities for such things as error handling in a software system.
More informationCS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM
CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM Objectives Defining a wellformed method to check class invariants Using assert statements to check preconditions,
More informationMSO Exam November 7, 2016, 13:30 15:30, Educ-Gamma
MSO 2016 2017 Exam November 7, 2016, 13:30 15:30, Educ-Gamma Name: Student number: Please read the following instructions carefully: Fill in your name and student number above. Be prepared to identify
More informationClasses and Objects. Object Orientated Analysis and Design. Benjamin Kenwright
Classes and Objects Object Orientated Analysis and Design Benjamin Kenwright Outline Review Previous Weeks Object Model, Complexity,.. What do we mean by Classes and Objects? Summary/Discussion Review
More information11/4/15. Review. Objec&ves. Refactoring for Readability. Review. Liskov Subs&tu&on Principle (LSP) LISKOV SUBSTITUTION PRINCIPLE
Objec&ves Liskov Subs&tu&on Principle Good enough design Refactoring for Extensibility Review What are some metrics of code design? Ø How can we use the metric? Ø What is the intui&on behind the metric?
More informationAssertions & Design-by-Contract using JML Erik Poll University of Nijmegen
Assertions & Design-by-Contract using JML Erik Poll University of Nijmegen Erik Poll - JML p.1/39 Overview Assertions Design-by-Contract for Java using JML Contracts and Inheritance Tools for JML Demo
More informationCOURSE 2 DESIGN PATTERNS
COURSE 2 DESIGN PATTERNS CONTENT Fundamental principles of OOP Encapsulation Inheritance Abstractisation Polymorphism [Exception Handling] Fundamental Patterns Inheritance Delegation Interface Abstract
More informationSoftware Specifications. CMSC 433 Programming Language Technologies and Paradigms Spring Specifications Help You Write Code
Software Specifications CMSC 433 Programming Language Technologies and Paradigms Spring 2007 Documentation Feb. 20, 2007 A specification defines the behavior of an abstraction This is the contract between
More informationJava Object Oriented Design. CSC207 Fall 2014
Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code
More informationObject Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming
Overview of OOP Object Oriented Programming is a programming method that combines: a) Data b) Instructions for processing that data into a self-sufficient object that can be used within a program or in
More informationCMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.
CMSC131 Inheritance Object When we talked about Object, I mentioned that all Java classes are "built" on top of that. This came up when talking about the Java standard equals operator: boolean equals(object
More informationMore About Objects. Zheng-Liang Lu Java Programming 255 / 282
More About Objects Inheritance: passing down states and behaviors from the parents to their children. Interfaces: requiring objects for the demanding methods which are exposed to the outside world. Polymorphism
More informationType Checking in COOL (II) Lecture 10
Type Checking in COOL (II) Lecture 10 1 Lecture Outline Type systems and their expressiveness Type checking with SELF_TYPE in COOL Error recovery in semantic analysis 2 Expressiveness of Static Type Systems
More informationCSE 331 Final Exam 3/16/15 Sample Solution
Question 1. (12 points, 3 each) A short design exercise. Suppose Java did not include a Set class in the standard library and we need to store a set of Strings for an application. We know that the maximum
More informationUC Santa Barbara. CS189A - Capstone. Christopher Kruegel Department of Computer Science UC Santa Barbara
CS189A - Capstone Christopher Kruegel Department of Computer Science http://www.cs.ucsb.edu/~chris/ Design by Contract Design by Contract and the language that implements the Design by Contract principles
More informationCH. 2 OBJECT-ORIENTED PROGRAMMING
CH. 2 OBJECT-ORIENTED PROGRAMMING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OBJECT-ORIENTED
More informationThe Liskov Substitution Principle
Agile Design Principles: The Liskov Substitution Principle Based on Chapter 10 of Robert C. Martin, Agile Software Development: Principles, Patterns, and Practices, Prentice Hall, 2003 and on Barbara Liskov
More informationCMSC 132: Object-Oriented Programming II
CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation
More informationLiskov Substitution Principle
Liskov Substitution Principle Produced by: Eamonn de Leastar (edeleastar@wit.ie) Dr. Siobhán Drohan (sdrohan@wit.ie) Department of Computing and Mathematics http://www.wit.ie/ SOLID Class Design Principles
More informationSpecification tips and pitfalls
Specification tips and pitfalls David Cok, Joe Kiniry, and Erik Poll Eastman Kodak Company, University College Dublin, and Radboud University Nijmegen David Cok, Joe Kiniry & Erik Poll - ESC/Java2 & JML
More informationWhat is Inheritance?
Inheritance 1 Agenda What is and Why Inheritance? How to derive a sub-class? Object class Constructor calling chain super keyword Overriding methods (most important) Hiding methods Hiding fields Type casting
More informationMSO Lecture Design by Contract"
1 MSO Lecture Design by Contract" Wouter Swierstra (adapted by HP, AL) October 8, 2018 2 MSO SO FAR Recap Abstract Classes UP & Requirements Analysis & UML OO & GRASP principles Design Patterns (Facade,
More informationInheritance (Part 5) Odds and ends
Inheritance (Part 5) Odds and ends 1 Static Methods and Inheritance there is a significant difference between calling a static method and calling a non-static method when dealing with inheritance there
More informationInheritance (Chapter 7)
Inheritance (Chapter 7) Prof. Dr. Wolfgang Pree Department of Computer Science University of Salzburg cs.uni-salzburg.at Inheritance the soup of the day?! Inheritance combines three aspects: inheritance
More informationPrelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer
Prelim 1 SOLUTION CS 2110, September 29, 2016, 7:30 PM 0 1 2 3 4 5 Total Question Name Loop invariants Recursion OO Short answer Exception handling Max 1 15 15 25 34 10 100 Score Grader 0. Name (1 point)
More informationProgramming Languages and Techniques (CIS120)
Programming Languages and Techniques (CIS120) Lecture 24 October 29, 2018 Arrays, Java ASM Chapter 21 and 22 Announcements HW6: Java Programming (Pennstagram) Due TOMORROW at 11:59pm Reminder: please complete
More informationChapter 8: Class and Method Design
Chapter 8: Class and Method Design Objectives Become familiar with coupling, cohesion, and connascence. Be able to specify, restructure, and optimize object designs. Be able to identify the reuse of predefined
More informationSOLID: Principles of OOD
SOLID: Principles of OOD CS480 Software Engineering http://cs480.yusun.io February 18, 2015 Yu Sun, Ph.D. http://yusun.io yusun@cpp.edu Software Nature Software Entropy Software tends to degrade / decay
More informationJava Magistère BFA
Java 101 - Magistère BFA Lesson 3: Object Oriented Programming in Java Stéphane Airiau Université Paris-Dauphine Lesson 3: Object Oriented Programming in Java (Stéphane Airiau) Java 1 Goal : Thou Shalt
More informationTest Code Patterns. How to design your test code
Test Code Patterns How to design your test code Testing and Inheritance n Should you retest inherited methods? n Can you reuse superclass tests for inherited and overridden methods? n To what extent should
More informationDesign by Contract in Eiffel
Design by Contract in Eiffel 2002/04/15 ctchen@canthink.com.com.tw.tw Reference & Resource Bertrand Meyer, Object-Oriented Oriented Software Construction 2nd,, 1997, PH. Bertrand Meyer, Eiffel: The Language,,
More informationCSC207H: Software Design SOLID. CSC207 Winter 2018
SOLID CSC207 Winter 2018 1 SOLID Principles of Object-Oriented Design How do we make decisions about what is better and what is worse design? Principles to aim for instead of rules. e.g. there is no maximum
More informationSoftware Construction
Lecture 7: Type Hierarchy, Iteration Abstraction Software Construction in Java for HSE Moscow Tom Verhoeff Eindhoven University of Technology Department of Mathematics & Computer Science Software Engineering
More informationINHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U
C A N A D I A N I N T E R N A T I O N A L S C H O O L O F H O N G K O N G INHERITANCE & POLYMORPHISM P2 LESSON 12 P2 LESSON 12.1 INTRODUCTION inheritance: OOP allows a programmer to define new classes
More informationIn-Class Exercises. ETH Zurich. ETH students recently designed a special kind of oven for cooking potatoes. Here are some facts about such an oven:
In-Class Exercises ETH Zurich 1 Contracts ETH students recently designed a special kind of oven for cooking potatoes. Here are some facts about such an oven: each oven is equipped with a door which is
More informationOpen Closed Principle (OCP)
Open Closed Principle (OCP) Produced by: Eamonn de Leastar (edeleastar@wit.ie) Dr. Siobhán Drohan (sdrohan@wit.ie) Department of Computing and Mathematics http://www.wit.ie/ SOLID Class Design Principles
More informationTesting and Inheritance. Test Code Patterns. Inheritance-related bugs. Inheritance. Testing of Inheritance. Inheritance-related bugs
Test Code Patterns How to design your test code Testing and Inheritance n Should you retest inherited methods? n Can you reuse superclass tests for inherited and overridden methods? n To what extent should
More informationUniversity of Utah School of Computing
University of Utah School of Computing CS 1410 / CS 2000 Study Notes December 10, 2011 This study guide is designed to help you prepare and study the appropriate material for the final exam. For the multiple
More informationTopic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC
Topic 7: Inheritance Reading: JBD Sections 7.1-7.6 1 A Quick Review of Objects and Classes! An object is an abstraction that models some thing or process! Examples of objects:! Students, Teachers, Classes,
More informationExam in TDDB84: Design Patterns,
Exam in TDDB84: Design Patterns, 2014-10-24 14-18 Information Observe the following, or risk subtraction of points: 1) Write only the answer to one task on one sheet. Use only the front side of the sheets
More informationFirst IS-A Relationship: Inheritance
First IS-A Relationship: Inheritance The relationships among Java classes form class hierarchy. We can define new classes by inheriting commonly used states and behaviors from predefined classes. A class
More informationUNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED
UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED EXERCISE 11.1 1. static public final int DEFAULT_NUM_SCORES = 3; 2. Java allocates a separate set of memory cells in each instance
More informationCS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?
CS6501 - Internet programming Unit- I Part - A 1 Define Java. Java is a programming language expressly designed for use in the distributed environment of the Internet. It was designed to have the "look
More informationCSC207 Week 3. Larry Zhang
CSC207 Week 3 Larry Zhang 1 Announcements Readings will be posted before the lecture Lab 1 marks available in your repo 1 point for creating the correct project. 1 point for creating the correct classes.
More information