perspectives on design principles – semantic invariants and design entropy
DESCRIPTION
Perspectives on Design Principles – Semantic Invariants and Design Entropy. Catalin Tudor. Liskov Substitution. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/1.jpg)
1
![Page 2: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/2.jpg)
Perspectives on Design Principles – Semantic Invariants and Design Entropy
Catalin Tudor
2
![Page 3: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/3.jpg)
Liskov SubstitutionIf 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 behavior of P is unchanged when
o1 is substituted for o2 then S is a subtype of T.
![Page 4: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/4.jpg)
Notations
![Page 5: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/5.jpg)
Imagine
You’ve created an application that was successfully deployedYour application knows how to handle rectanglesUsers ask that it should also manage squares
![Page 6: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/6.jpg)
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
![Page 7: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/7.jpg)
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);}
![Page 8: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/8.jpg)
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
![Page 9: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/9.jpg)
Semantic Invariants
![Page 10: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/10.jpg)
Constraints
Pre-conditionsPost-conditionsInvariants
![Page 11: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/11.jpg)
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
![Page 12: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/12.jpg)
Rules
Pre-conditions cannot be strengthened in a subtypePost-conditions cannot be weakened in a subtypeInvariants of the supertype must be preserved in a subtype
![Page 13: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/13.jpg)
Design Entropy
![Page 14: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/14.jpg)
Entropy (information theory)
Shannon entropy is a measure of the uncertainty associated with a random variableShannon 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
![Page 15: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/15.jpg)
Definition
For a random variable X with n outcomes (x1, x2, x3…, xn) the Shannon entropy is:
is the probability of the outcome
.
where
𝐻 ( 𝑋 )=−∑𝑖=1
𝑛
𝑝 (𝑥𝑖) log𝑏𝑝 (𝑥 𝑖)
𝑝 (𝑥𝑖 ) 𝑥𝑖
![Page 16: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/16.jpg)
Example
How much information is required to store X = {0, 1}. Consider ?
.
𝐻 ( 𝑋 )=−∑𝑖=1
2 12log 2
12= 1 (bit)
![Page 17: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/17.jpg)
Example
How much information is required for X = {00, 01, 10, 11}? Consider
.
𝐻 ( 𝑋 )=−∑𝑖=1
4 14log 2
14= 2 (bits)
![Page 18: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/18.jpg)
Class Entropy - Rectangle
How much information is Rectangle exposing?We look at class fieldsWidth = w (0/1)Height = h (0/1)Rectangle is defined by a random variable
.
![Page 19: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/19.jpg)
Class Entropy - Square
How much information is Square exposing?Width = w (0/1)Height = h (0/1)Width and Height are no longer independent
.
![Page 20: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/20.jpg)
Necessary Condition for LSP
Whenever class S extends class R is necessary that
In the Square vs. Rectangle case
.
![Page 21: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/21.jpg)
Breaking the rules
What happens when the entropy rule is broken?Let’s say we have method m using objects of type RIf 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)
.
![Page 22: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/22.jpg)
Real world examples of what can happen when the Entropy rule is broken
![Page 23: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/23.jpg)
Example 1
![Page 24: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/24.jpg)
A door
![Page 25: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/25.jpg)
Same door in a room with less volume
![Page 26: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/26.jpg)
Example 2
![Page 27: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/27.jpg)
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
![Page 28: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/28.jpg)
Practice
You need to create a class that stores one file system pathExamples:
/home/repository/build.doc../../test.sh./special_folder
You have String class at your disposal (you can either use inheritance or aggregation)
.
![Page 29: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/29.jpg)
Solution 1
Clearly FilePath is a stringReuse String classBenefit for free from functionality offered by other libraries handling String objectsBreaks entropy rule: some characters are not allowed in a path ex: NUL and / some apps may not allow ?, *, :, + and %
.
![Page 30: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/30.jpg)
Solution 2
FilePath is a string sometimes…Most of the times is a FilePathWe should reuse String but only as a Strategy not as a Template
.
This solution allows for entropy variation in both directions
![Page 31: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/31.jpg)
Design Entropy Challenges
Invent a way to automatically detect design flaws in existing applications based on design entropy rulesFind entropy rules for:
• Open Close Principle• Dependency Inversion Principle • Interface Segregation Principle• Single Responsibility Principle
Find the correct entropy rule for aggregation / compositionLook for an entropy rules that settle the inheritance vs. aggregation debate
.
![Page 32: Perspectives on Design Principles – Semantic Invariants and Design Entropy](https://reader035.vdocuments.mx/reader035/viewer/2022062521/568161b4550346895dd17f20/html5/thumbnails/32.jpg)
Resources
OOD articles http://www.objectmentor.comShannon Entropy http://en.wikipedia.org/wiki/Entropy_(information_theory)