c02 s1 Collections Solid
c02 s1 Collections Solid
2
Time Complexity Examples
O (N^2) Bubble Sort
3
Time Complexity Notations
5
Why Big-O??
• There are 2 eggs.
• Eggs can be used again until it is broken.
• What is the minimum attempt count to find which
floor breaks an egg?
6
Why Big-O??
Algorithm 1:
•Start from floor 1.
•Drop egg.
•If it doesn’t break, go to upper floor.
7
Why Big-O??
Algorithm 2:
•Try middle floor.
•Drop egg.
•If it doesn’t break, try new middle floor.
•If it breaks, use algorithm 1 between last two attempts.
•Eg:
• Drop at 50th floor. -> NOT BROKEN
• Drop at 75th floor. -> BROKEN
• Use Algorithm 1 between 51-74 floor.
8
•Expected attempt count differs from 2 to 50
Why Big-O??
Algorithm 3:
•Try {14, 27, 39, 50, 60, 69, 77, 84, 90, 95, 99} floors until
egg breaks.
•If it breaks, use algorithm 1 between last 2 attempts.
•Eg:
• Drop at 14th floor. -> NOT BROKEN
• Drop at 27th floor. -> NOT BROKEN
• Drop at 39th floor. -> BROKEN
• Use Algorithm 1 between 28-38 floor.
9
Why Big-O??
All possible solutions for algo-3
10
Why Big-O??
Results of all algorithms.
11
Why Big-O??
Comparison
12
Collections
13
Read/Write Time
Complexity
14
Array
15
Linked List
16
Array vs Linked List
17
HashMap
• HashMap is combination of
Array and Linked List
• If the inner table is big
enough, read time can be
O(1)
• If the elements have same
hash number, reading and
writing may be O(N)
18
Queue and Stack
• Queue and Stack is
derived from
LinkedList.
• Each element shows
the next one.
• Also, array based
implementations exist.
19
Tree
• Tree is enhanced version of
LinkedList. In linked list, each
element shows next. In tree,
each element may have
more than one pointer.
• For standard version, binary
tree, all nodes have to
pointers. The left one points
smaller nodes, the right one
points bigger nodes.
• There are also different tree
types based on children
count;
• Binary: has two children
per node
• Ternary tree: 3 children
• N-ary: random number
of children
20
Comparison of Collections
21
Demo Solid Project
You can download the whole project from google
drive.
Please extract it.
Open pom.xml file in IntellijIdea.
Then you can run SolidMain and NonSolidMain
classes.
22
Non Solid Example
23
Solid Example
24
Solid and NonSolid Output
25
What is SOLID
SOLID is an acronym that stands for five key design principles.
The principles provide markable benefits to software.
26
Single responsibility
“A class should have one, and only one, reason to change.”
27
Open-closed
“You should be able to extend a class’s behavior without modifying
it.”
The code should be open for extension and close for modification.
While adding new features, just add new files. Don’t modify
existing files.
28
Liskov substitution
This is the most difficult to understand principle!
It is named for Barbara Liskov
29
Interface segregation
“Make fine grained interfaces that are client-specific. Clients
should not be forced to implement interfaces they do not use.”
30
Dependency inversion
“High level modules should not depend upon low level modules.
Both should depend on abstractions…abstractions should not
depend on details. Details should depend upon abstractions.”
31
Why is it NonSolid?
Single responsibility principle: Employee class has 3
responsibilities.
1- It is data object. (DTO)
2- It has “work” logic.
3- It has “calculateSalary” logic.
32
Why is it NonSolid?
Open-closed principle: In order to add new department or to
change department behavior, Employee class has to be modified!
In solid version, changing ITEmployee class is enough.
33
Why is it NonSolid?
Liskov substitution principle: There is no inner interface for
Employee class.
Also sub classes must not change Employee behaviour.
34
Why is it NonSolid?
Liskov substitution principle 2nd example
35
Why is it NonSolid?
36
Why is it NonSolid?
Interface segregation principle: There is no interface at all!
37
Why is it NonSolid?
Interface segregation principle 2nd example
38
Why is it NonSolid?
Dependency inversion principle: Employee class has work and
calculateSalary logic. All different flows should be separated
classes.
Employess class is dependent to department behaviors.
39