1. Perspectives on Design Principles – Semantic Invariants and Design Entropy Catalin Tudor 2.

Slides:



Advertisements
Similar presentations
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Advertisements

CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
1 Computer Science 340 Software Design & Testing Inheritance & Design By Contract.
CS 2430 Day 28. Announcements Program 5 was posted (on Tuesday, 4/2/2013) Can work with a partner (sign up by today at 3:52pm) Exam 2 handed back on Monday.
5/17/2015 OO Design: Liskov Substitution Principle 1.
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
Testing and Inheritance What is the relation between the test suite of a superclass and a subclass?
1 CS4850 Programming Languages Wuwei Shen. 2 Aministrivia Course home page: Reference books: –Programming Languages,
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
More Inheritance and LSP CS340100, NTHU Yoshi. More about Inheritance Reuse? – Q1: 你有沒有程式 ” 砍掉重練 ” 的經驗 ? – Q2: 你有沒有 ” 再造輪子 ” 的經驗 ? class Rectangle – Firstly,
1 Software Testing and Quality Assurance Lecture 12 - The Testing Perspective (Chapter 2, A Practical Guide to Testing Object-Oriented Software)
Chapter 25 GRASP: More Objects with Responsibilities 1CS6359 Fall 2011 John Cole.
Typing Issues and LSP Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 Typing Static typing Readability (Java vs. Pearl) Catching errors.
Software Testing and Quality Assurance
Inheritance (notes for 10/26 lecture). Inheritance Inheritance is the last of the relationships we will study this semester. Inheritance is (syntactically)
1 CS4850 Programming Languages Wuwei Shen. 2 Aministrivia Course home page: Reference books: –Programming Languages,
Feb. 23, 2004CS WPI1 CS 509 Design of Software Systems Lecture #5 Monday, Feb. 23, 2004.
Typing Issues and LSP David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 Typing Static typing Reliability Catching errors early Readability.
Lecture a: Additional UML Models: Package, Activity, Deployment Lecture b: Generalization, Aggregation and Additional Domain Model Notation Copyright W.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
CSc 335: Three More OO Design Principles
OOP Languages: Java vs C++
Design II Today: Design Principles…can we define them intro.12.ppt CS 121 “Ordering Chaos” “Mike” Michael A. Erlinger.
CLASS DESIGN PRINCIPLES Lecture 2. The quality of the architecture What is a good design? It is the design that at least does not have signs of “bad”.
Introduction to Object-oriented programming and software development Lecture 1.
Controlling Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Inheritance Modes When deriving.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
More Design. Next Tuesday First draft of architectural design –use cases –class diagrams for major classes with responsibilities –sequence diagrams for.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
Class Design III: Advanced Inheritance Additional References “Object-Oriented Software Development Using Java”, Xiaoping Jia, Addison Wesley, 2002 “Core.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
Design Principles iwongu at gmail dot com.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
OO as a language for acm l OO phrase l Mental model of key concepts.
Software Design Principles
1 Single Responsibility Principle Open Closed Principle Liskov Substitution Principle Law of Demeter CSC 335: Object-Oriented Programming and Design.
Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency.
Testing and Inheritance What is the relation between the test suite of a superclass and a subclass?
Chapter FifteenModern Programming Languages1 A Second Look At Java.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Testing OO software. State Based Testing State machine: implementation-independent specification (model) of the dynamic behaviour of the system State:
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
(c) University of Washington02-1 CSC 143 Java Object and Class Relationships: Interfaces Reading: Ch. 9 (on Java interfaces)
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
Classes, Interfaces and Packages
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Test Code Patterns How to design your test code. 2 Testing and Inheritance Should you retest inherited methods? Can you reuse superclass tests for inherited.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
More Design Heuristics
Software Design Principles
Inheritance ITI1121 Nour El Kadri.
Component Based Software Engineering
CSE 331 Subtyping slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
EECE 310: Software Engineering
– Introduction to Object Technology
Object Oriented Practices
Software Design Principles
Subtype Polymorphism, Subtyping vs
Object Oriented Practices
Object Oriented Practices
Liskov Substitution Principle (LSP)
Presentation transcript:

1

Perspectives on Design Principles – Semantic Invariants and Design Entropy Catalin Tudor 2

Liskov Substitution If for each object o 1 of type S there is an object o 2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o 1 is substituted for o 2 then S is a subtype of T.

Notations

Imagine You’ve created an application that was successfully deployed Your application knows how to handle rectangles Users ask that it should also manage squares

A design choice Square is a rectangle for all normal intents Inheritance is an ISA relationship ISA relationship is one of the fundamental concepts of OOA

A design choice class Rectangle { public: void SetWidth(double w) { width = w; } double GetWidth() const { return width; } void SetHeight(double h) { height = h; } double GetHeight() const { return height; } private: double height; double width; }; void Square::SetWidth(double w) { Rectangle::SetWidth(w); Rectangle::SetHeight(w); } void Square::SetHeight(double h) { Rectangle::SetHeight(h); Rectangle::SetWidth(h); }

What is wrong? Square class uses a member that is not needed (unnecessary memory) You have to override certain access methods, this seems to break encapsulation Functions that don’t fail with Rectangles might fail with Squares Behaviorally, a Square is not a Rectangle and it is behavior that software is really all about It’s generally accepted that it’s not possible to detect a Liskov Substitution violation for a class in isolation

Semantic Invariants

Constraints Pre-conditions Post-conditions Invariants

Rectangle constraints (SetHeight) Pre-conditions Height needs to be a positive integer And in a predefined range (between 1 and 1000) Post-conditions The rectangle will have the specified height Invariants Width remains unchanged

Rules Pre-conditions cannot be strengthened in a subtype Post-conditions cannot be weakened in a subtype Invariants of the supertype must be preserved in a subtype

Design Entropy

Entropy (information theory) Shannon entropy is a measure of the uncertainty associated with a random variable Shannon entropy is typically measured in bits (base 2), nats (base e), bans (base 10) A single toss of a fair coin has an entropy of one bit

Definition For a random variable X with n outcomes (x 1, x 2, x 3 …, x n ) the Shannon entropy is: is the probability of the outcome. where

Example. = 1 (bit)

Example. = 2 (bits)

Class Entropy - Rectangle.

Class Entropy - Square.

Necessary Condition for LSP.

Breaking the rules What happens when the entropy rule is broken? Let’s say we have method m using objects of type R If class S extends class R by breaking the entropy rule then method m will have to account for the missing entropy in class S (read this as strong coupling).

Real world examples of what can happen when the Entropy rule is broken

Example 1

A door

Same door in a room with less volume

Example 2

Water pump. Pump m coupled with R Now attach S Water pump model Object instances – water tanks Method calls – pumping water Class entropy – tank volume

Practice You need to create a class that stores one file system path Examples: /home/repository/build.doc../../test.sh./special_folder You have String class at your disposal (you can either use inheritance or aggregation).

Solution 1 Clearly FilePath is a string Reuse String class Benefit for free from functionality offered by other libraries handling String objects Breaks entropy rule: some characters are not allowed in a path ex: NUL and / some apps may not allow ?, *, :, + and %.

Solution 2 FilePath is a string sometimes… Most of the times is a FilePath We should reuse String but only as a Strategy not as a Template. This solution allows for entropy variation in both directions

Design Entropy Challenges Invent a way to automatically detect design flaws in existing applications based on design entropy rules Find entropy rules for: Open Close Principle Dependency Inversion Principle Interface Segregation Principle Single Responsibility Principle Find the correct entropy rule for aggregation / composition Look for an entropy rules that settle the inheritance vs. aggregation debate.

Resources OOD articles Shannon Entropy ory) ory)