11 Design Principles
11 Design Principles
1 2
Architectures/Framework
ITSS SOFTWARE DEVELOPMENT (Financial System, J2EE,…)
11. DESIGN PRINCIPLES OOD Patterns
General + OO Concepts
1 2
3 4
1
12/12/23
5 6
5 6
7 8
7 8
2
12/12/23
9 10
9 10
11 12
SRP: Example
• Often we need to sort students by their
name, or ssn.
è Make Class Student implement the
Java Comparable interface
11 12
3
12/12/23
13 14
Register
SSN
Name
Comparable compareTo() method needs to be changed and
int compareTo()
add(Course d, Student s); getSSN()
getName() Students needs to be recompiled and also its
major
getMajor() clients
int compareTo()
• Student is a business entity, it does not know in
When a new requirement needs to
what order it should be sorted since the order of
sort students in a different order, sorting is imposed by the client of Student.
Student, Register, and AClient all AClient
need to be recompiled, even Register
op() { ;}
• Cause of the problem: we bundled two separate
has nothing to do with the new responsibilities (i.e., a business entity and
ordering of Students. It invokes
Collections.sort(aListofStudents); ordering) into one class – a violation of SRP
13 14
15 16
StudentBySSN
5. SOLID: Dependency Inversion Principle
int compare(Object o1, Object o2)
6. Case study: Reminder program
The solution is to separate the
two responsibilities into two ClientB
separate classes and use another op() { ;} It invokes
Collections.sort(aListofStudents,
version of Collections.sort(). StudentBySSN);
15 16
4
12/12/23
17 18
17 18
19 20
19 20
5
12/12/23
21 22
Abstract Employee
+int EmpType
Supplier
21 22
23 24
23 24
6
12/12/23
25 26
25 26
27 28
22
27 28
7
12/12/23
29 30
LSP: Liskov Substitution Principle (2) LSP: Liskov Substitution Principle (3)
• Demand no more: The subclass would accept any • Why LSP is so important? If violates LSP:
arguments that the superclass would accept.
• Class hierarchy would be a mess and if subclass
• Promise no less: Any assumption that is valid
instance was passed as parameter to methods method,
when the superclass is used must be valid when
the subclass is used. strange behavior might occur.
• Unit tests for the Base classes would never succeed for
the subclass.
• Interface Inheritance: LSP should be conformed to
• Implementation (or Class) Inheritance: è LSP is just an extension of Open-Close
• Multi-layer inheritance hierarchy: Provide more Principle!!!
abstraction levels for subclasses
• Use composition instead of inheritance (in Java) or use
private base classes (in C++)
29 30
31 32
31 32
8
12/12/23
33 34
33 34
35 36
35 36
9
12/12/23
37 38
37 38
39 40
39 40
10
12/12/23
41 42
41 42
43 44
43 44
11
12/12/23
45 46
Mechanism Mechanism
<<interface>>
Layer Policy
Layer
Service Utility
45 46
47 48
47 48
12
12/12/23
50
49 50
51
Design exercise
• Write a typing break reminder program
• Offer the hard-working user occasional reminders of the
health issues, and encourage the user to take a break
from typing
• Naive design
• Make a method to display messages and offer
exercises
• Make a loop to call that method from time to time
(Let's ignore multi-threaded solutions for this
discussion)
51
13