Software Reengineering P3: OO Design Principles and Violations
Software Reengineering P3: OO Design Principles and Violations
Martin Pinzger
Delft University of Technology
Design Smells
Conclusions
2
The Reengineering Life-Cycle
(3) problem
detection (4) problem
resolution
Designs
(2) model
capture
Code
3
Design Smells
4
The Broken Window Theory
5
S.O.L.I.D. Design Principles
S.O.L.I.D Design Principles
7
SRP: The Single-Responsibility Principle
SRP is one of the simplest of the principles, and the one of the
hardest to get right
8
SRP heuristics
9
Exercise: SRP
RuleParser
- current: String
- variables: HashMap
- currentPosition: int
+ evaluate(String rule) : int
- branchingExpression(Node left, Node right) : int
- causualExpression(Node left, Node right) : int
- variableExpression(Node node) : int
- valueExpression(Node node) : int
- nextTerm() : String
- hasMoreTerms() : boolean
+ addVariable(String name, int value)
10
Example: SRP (possible) solution
TermTokenizer
+ nextTerm() : String
+ hasMoreTerms() : boolean
RuleEvaluator
RuleParser
+ evaluate(String rule)
+ parse(String rule) : Expression
+ addVariables(String, int)
creates
{abstract}
SymbolTable
Expression
+ addVariable(String, int)
+ evaluateWith(SymbolTable table)
11
OCP: The Open-Closed Principle
12
Example: OCP – Strategy Pattern
13
OCP heuristics
14
LSP: Liskov Substitution Principle
15
LSP violation example
public enum ShapeType {square, circle};
Violate OCP
public class Shape {
public static void DrawShape(Shape s) {
if(s.type == ShapeType.square)
(s as Square).Draw();
else if(s.type == ShapeType.circle) Not
substitutable
(s as Circle).Draw();
}
}
public class Circle : Shape {
public void Draw() {/* draws the circle */}
}
public class Square : Shape{
public void Draw() {/* draws the square */}
}
16
Another LSP violation example
void g(Rectangle r)
Square is not
{ Rectangle!
r.setWidth(5);
r.setHeight(4);
if(r.getArea() != 20)
throw new Exception("Bad area!");
}
Square’s behavior is
changed, so it is not
IS-A Relationship substitutable to
Rectangle
17
LSP heuristics
18
DIP: The Dependency Inversion Principle
19
A DIP example
DIP
DIP violation
20
DIP heuristics
Depend on abstractions
No variable should hold a reference to a concrete class
No class should derive from a concrete class
No method should override an implemented method of any of its base
classes
21
ISP: The Interface Segregation Principle
22
An violation of ISP example
ISP violation
23
An ISP Violation example: solution
Segregated
interface
24
ISP heuristics
25
LoD - Law of Demeter
Minimize coupling
26
LoD formal definition
27
Example LoD
class Demeter {
public A a;
public int func() {
// do something
}
public void example(Arg arg) {
C c = new C();
int f = func(); // functions belonging to itself
arg.invert(); // to passed parameters
a = new A();
a.setActive(); // to any objects it has created
c.print(); // to any held objects
}
}
28
LoD violation example
final String outputDir = ctxt.getOptions().getScratchDir().getAbsolutePath();
a.getB().getC().doSomething()
29
DRY – Don’t Repeat Yourself
30
More information on Design Principles
31
Intensive Coupling(120), Shotgun Surgery(133)
Classification Disharmonies (Chapter 7): Refused Parent Bequest(145),
Tradition Breaker(152)
Design Disharmonies
32
Collaboration
Disharmonies
Collaboration Disharmonies
34
provider classes is excessively verbose. Therefore, we named this de-
sign disharmony Intensive Coupling.
Intensive Coupling
37
Intensive Coupling: Class Blueprint
6.3 Intensive Coupling 125
Dispersed Coupling
39
conditionals
Detection The detection rule is defined in the same terms as the the one define
for Intensive Coupling(120), with only one complementary differenc
we capture only those operations that have a high dispersion of the
coupling (Fig. 6.9). The detection strategy in detail is:
ig. 6.10. In Dispersed Coupling operation calls a few methods from each of
Fig. 6.9. Dispersed Coupling detection strategy
large number of unrelated classes.
41
pling strength but also the coupling dispersion.
Shotgun Surgery
42
dependencies caused by function calls. In order to reveal especially
those cases where dependencies are harder to trace, we will count
Shotgun Surgery:
only those operations Detection
(and classes) Strategy
that are neither belonging to the
same class nor to the same class hierarchy with the measured oper-
ation.
CC > MANY
inCode
http://loose.upt.ro/incode/pmwiki.php/
45
More info on Detection Strategies
46
Summary
47