3901 Slides
3901 Slides
Concepts
Mike McAllister
Fall 2020
CSCI 3901
2
Learning Outcomes
3
Learning Outcomes
Databases
Describe the properties of multiuser database transactions (ACID).
Describe the purpose, function, evolution, classification, and building
blocks of data models and data modeling.
Describe the basic components of a relational model and how relations
are implemented.
Derive business rules from requirements' specifications and translate
these rules into database table and relationship designs.
Use SQL data definition and manipulation operations.
Construct an entity relationship diagram (ERD).
Describe normalization and denormalization, and their role in database
design.
Perform normalization and denormalization on a database.
4
Learning Outcomes
Software Engineering
Design a software system and prepare detailed design documentation.
Implement moderate-sized programs individually and as a team.
Use general data representations like XML
Apply standard software processes for build and deployment
management.
Apply standard software processes for risk management.
Apply standard software processes for version control.
Apply concepts of software engineering to plan, execute and manage a
small software project.
Create a Test Plan for a software development project.
Effectively debug a program.
5
Grading
Labs – 10%
Assignments – 40% (6 assignments, equally weighed)
Module quizzes – 20%
Participation – 10%
Final project – 20%
6
Important Dates
November 9 – 13
Fall study break
December 8
Last day of classes
October 2
Last day to add or drop courses without penalty
November 2
Last day to drop courses without academic penalty
7
Tentative Schedule
Consult syllabus
8
Course textbook
No official textbook
We cover so much material that you would need several
9
Homework
no
Student no
Faculty-level hearing accepts?
yes yes
no Infraction Remedy sent to
happened student Remedy implemented
11
Concluding Reminders
Accessibility
Responsible computing
Code of Conduct
Culture of Respect
Scent-free university
12
Lessons from last time
13
Lessons learned from last time
14
Postage problem
16
Problem Solving – Starting the process
Who are the users and how will they use it?
What is the target environment?
How stable are the requirements?
17
Postage Problem
18
Postage problem
19
Problem Solving – Starting the process
20
Evolution of solving problems
22
Postage problem
24
Postage Problem
25
Encode Cases in the Code
26
Part data structure, part code
27
Data structures
28
Independent “if”
if (weight <= 100) report $1.80 /* Canada and 50 < weight <= 100 */
else report $2.95 /* Canada and 100 < weight <= 200 */
}
/* Canada and weight > 200 */
} else {
if (weight <= 400) { /* Canada and 200 < weight <= 400 */
if (weight <= 300) report $4.10 /* Canada and 200 < weight <= 300 */
else report $4.70 /* Canada and 300 < weight <= 400 */
boundaries = array with values 30, 50, 100, 200, 300, 500
Find index i such that boundaries[i-1] < weight <= boundaries[i]
rates = 2d array:
0.85, 1.20, 1.80, 2.95, 4.10, 4.70, 5.05
1.20, 1.80, 2.95, 5.15, 10.30, 10.30, 10.30
Report rates[country][i]
33
Switch solution
Get the country and weight
boundaries = array with values 0, 30, 50, 100, 200, 300, 500
Find index i such that boundaries[i] < weight <= boundaries[i+1]
/* We know that there are at most 7 rates, so combine the country and weight into one
integer: 10’s digit is country, unit digit is weight category. */
Class = country * 10 + i
Switch (class) {
10: report $0.85
11: report $1.20
12: report $1.80
…
20: report $1.20
21: report $1.80
22: report $2.95
…
}
34
Big table solution
// Create an array with 0 rows and 501 columns. Each row corresponds to a country
// and each row gives the postage rate for each weight, in grams, of the envelope
rates = 2d array {
{ 0, 0.85, 0.85, …, 0.85, 1.20, 1.20, …, 1.20, 1.80, 1.80, …, 1.80, 2.95, … } ,
{ 0, 1.20, 1,20, …, 1.20, 1.80, 1.80, …, 1.80, 2.95, 2.95, …, 2.95, 5.15, …}
}
35
Trade-offs in the algorithms?
36
Trade-offs
37
Trade-offs
38
Does our program work? Test cases
40
Does our program work? Test cases
44
Abstract Data Type vs Data Structure
Data Structure
“a data organization, management and storage format that
enables efficient access and modification. More precisely, a data structure is a
collection of data values, the relationships among them, and the functions or
operations that can be applied to the data.”
(https://en.wikipedia.org/wiki/Data_structure, September 6, 2018. Emphasis added.)
45
Abstract Data Type and Data Structures
Abstract Data Type What the structure should do / how it should behave?
Implemented by
46
Abstract Data Type and Data Structures
Implemented by
47
Abstract Data Type vs. Data Structure
ADT Data Structure
Stack Array
- Push, Pop, IsEmpty Hash table
Queue - Relationship between data
- Enqueue, Dequeue, IsEmpty and storage location
PriorityQueue Linked list
- AddWithPriority, Binary tree
RemoveHighestPriority, IsEmpty - Relationship between
Deque children and parents
- AddHead, AddTail, RemoveHead, Heap
RemoveTail, IsEmpty - Relationship between
Map children and parents
- Store, Retrieve, Delete
Set
- Add, ElementOf, Union, Intersection,
48
Complement, Cardinality
Stack
Queue
Map
Array
Hash table
51
Stack
Behaviour
Stores a collection of items
Items retrieved from the stack in last in first out order (LIFO)
Cannot access random elements of the collection
Operations
Push( value )
Pop( ) -> value
IsEmpty( ) -> boolean
Push( value )
Pop()
52
Queue
Behaviour
Stores a collection of items
Items retrieved from the queue in first in first out order (FIFO)
Cannot access random elements of the collection
Operations
Enqueue( value )
Dequeue( ) -> value
IsEmpty( ) -> boolean
Enqueue( value ) Dequeue ()
53
Priority Queue
Behaviour
Stores a collection of items where each item has a “priority”
to designate importance
Items retrieved in order of priority
Cannot access random elements of the collection
Operations
AddWithPriority( value, priority )
RemoveWithHighestPriority( ) -> value
IsEmpty( ) -> boolean
54
Priority Queue
55
Deque (doubly-ended queue)
Behaviour
Stores a collection of items
Concept of a queue where you can add or remove from either
end (but not from the middle)
Cannot access random elements of the collection
Operations
AddHead( value ) AddTail( value )
RemoveHead( ) -> value RemoveTail( ) -> value
IsEmpty( ) -> boolean
Behaviour
Stores a collection of items
Concept of a mathematical set
- Only one copy of each item allowed
Can access any item
No presumed order to the items
Operations
Add( value )
Remove( value )
ElementOf( value ) -> Boolean
Union( set1, set2 ) -> set
Intersection( set1, set2 ) -> set
Complement( universe, set ) -> set (optional)
57 Cardinality( set ) -> integer
Map (or dictionary or associative array)
Behaviour
Associates some meaningful data (the ”key”) with a value
Stores and accesses values using the key
No specific ordering of the data
Operations:
Put (key, value)
Get (key) -> value
Size () -> integer
ContainsKey (key) -> Boolean
ContainsValue (value) -> Boolean (optional)
Remove (key) -> Boolean (optional)
58
Map examples
Trivial map
Key is the sequence of integers 1, 2, 3, 4, …
Implementation: a standard array
59
Recognizing a spot for a standard ADT
Stack
Doing a set of operations that might need undoing in the reverse order
Exploring options that involve backtracking (changing or removing the most
recent choice)
Recursion (implicit or explicit stack)
Situations where proper nesting is involved
Exploring connected problems that handle depth of coverage before breadth of
coverage
Queue
Simulations of scheduling with items arriving at different times
Processing a growing list of items in a way that ensures that each item is handled
in a “fair” timeframe
Exploring connected problems that handle breadth of coverage before depth of
coverage
60
Recognizing a spot for a standard ADT
Priority Queue
I need to store items and retrieve them in an order that I define (order can change)
Often used in scheduling
Set
I have a collection if items to store
- I just care about having one copy
I want to access the items randomly
I want to iterate over the set
I don’t have any particular order needed for the data
List
I have a collection of items to store
- I might have several copies of the same thing
I want to access items randomly
I want to be able to impose an order to the set by sorting
I want to iterate over the set in the sorted order
Map
61 Random access to key, value pairs when exact matches to keys is all that is needed
What is stored in an ADT?
Objects
Important to understand exactly what is being stored
62
Is there a difference?
int a; myIntClass a;
int b; myIntClass b;
Local variables
64
Storing Objects
public int myMethod() {
int someInt;
static boolean test;
OtherClass anObject;
test
someInt
65
Storing Objects
public int myMethod() {
int someInt;
static boolean test;
OtherClass anObject;
someInt
anObject
66
Storing Objects
public int myMethod() {
int someInt;
static boolean test;
OtherClass anObject;
OtherClass anotherObject;
test
anotherObject = anObject;
someInt
}
anObject
anotherObject
67
Storing Objects
public int myMethod() {
int someInt;
static boolean test;
OtherClass anObject;
OtherClass anotherObject;
test
anotherObject.copy(anObject);
someInt
}
anObject
anotherObject
68
Storing Objects
public int myMethod() {
int someInt;
static boolean test;
OtherClass anObject;
OtherClass anotherObject;
test
anotherObject.copy(anObject);
someInt
}
anObject
anotherObject
OtherClass anotherObject;
test
anotherObject.deepCopy(
someInt
anObject);
anObject
}
anotherObject
70
What is stored in an ADT?
Take-away:
When you put an object into two ADTs, know whether you
expect to have each copy be shared or independent
- If shared, then put the reference into each ADT
- If independent then put a copy into each ADT
Understand if you need a shallow or a deep copy
71
Combine ADTs
72
Data structures with a fixed size
73
Array
74
Declaring an array in Java
75
How would you create a 2d array?
76
Hash table
77
Hash table example
Array size: 13
Data stored: alphabetic lower-case strings
Hash function: the position in the alphabet of the first letter of
the string (starting at position 0)
Array index: take the hash value modulo 13
Expected collisions:
All the strings that start with the same letter end up at the same index
Two letters of the alphabet converge on the same index
78
Hash table example
apple
Add “quiet”
pancake
density Hash value is 16 (‘q’ – ‘a’)
Index is 16 mod 13 = 3
gorilla
Store “quiet” here
umbrella
yoyo
79
How to deal with hash table collisions
Have a data structure at each array index to catch all values that
belong at the index (called “open hashing”)
Linked list, binary tree, …
In-place: look for another “predictable” place in the array to
store the entry (called “closed hashing”)
Move forward k entries in the array until you find an entry spot
- Linear probing: k=1
- Quadratic probing: k follows a sequence 12, 22, 32, 42, …
- Double hashing: k is the result of applying a second hash function to the
value to be stored
More complex resolution schemes
- Eg. Cuckoo hashing
80
Hash table example with linear probing
apple
Add “mandrake”
pancake
density Hash value is 13 (‘m’ – ‘a’)
Index is 13 mod 13 = 0
gorilla
Try to store “mandrake” here, but the entry is full
umbrella
81
Hash table collisions
84
Using Hash Tables in Java
Common trick: when creating a new array or table, make it twice as big
- Not reallocating the table all of the time to grow by 1 or 2
- Have at most half of the space in the new array or table as unused
87
Dynamically-growing structures
apple
Given a hash table, why don’t we just pancake
copy over all the data directly? umbrella pancake
density
apple
pancake
The hash function for the ??
larger table may not put umbrella
gorilla
each value in the same density
place. umbrella
gorilla
88
Data structures without a fixed size
89
Basics
SomeClass value;
}
90
Linked List
91
Linked List
Class Node {
Node next;
int value;
93
Variants of linked lists
Design differences
Singly-connected linked list (forward only)
Implementation differences
Sentinel or dummy node as the start (list never empty)
f
Track both ends of the list (append is quick)
94
Binary search tree
95
Binary search tree
31 81
31 99
left right left right
left right left right
16 39 80 99
16 39
left right left right left right left right
left right left right
80
left right
42
left right
96
Binary search tree
97
Java type for a binary tree
101
Sample Binary Search Tree Rotation
n k
n
k
d a
m m
d
a
b c b c
k n
k
a b c d
a
b c
104
Min-Heap
105
Min-Heap
85 32
left right left right
86 91 100
left right left right left right
106
Min-Heap
86 91 100
left right left right left right
107
Min-Heap
To add an item:
Store it in the next spot in the bottom level
Continually swap it with its parent if it is smaller than the
parent
27 27 27
85 32 85 32 85 18
18
85 27
86 91 100 32
108
Min-Heap
To remove an item:
Remove the top-most item.
Move the last item in the lowest level to the top
Continually compare this moved item to its two children and
swap it with its smallest child
27 100 32
85 32 85 32 85 100
86 91 100 86 91 86 91
109
Min-Heap
110
Graph
Example:
Graph of people who know one another
- V = set of people, E = edges between people who know each other
111
Graph
Jill
Jack
Meg
Rob Todd
112
Sample uses of graphs
113
Graph representation
Jill
Jack
Meg
Rob Todd
115
Adjacency Matrix
116
Incidence Matrix
E1 E2 E3 E4 E5 E6
Jack 1 1
Jill 1 1 1
Meg 1 1
Todd 1 1
Rob 1 1 1
117
Adjacency List
Vertex Neighbours
Jack {Jill, Rob}
Jill {Jack, Meg, Rob}
Meg {Jill, Todd}
Todd {Meg, Rob}
Rob {Jack, Jill, Todd}
118
Types of Graphs
Edge influence
Undirected: you can traverse an edge in any direction
Directed: there is just one way by which you can follow an
edge
Connectivity
Connected: you can get from one vertex to any other vertex
Unconnected: there are some vertices that can’t reach one
another
119
Types of Graphs
Edge multiplicity
Simple: there are no edges back to the same vertex (a loop)
and at most one edge between pairs of vertices
Multi-edge: you can have loops and several edges between
the same pair of vertices.
Edge weight
Unweighted: no values associated with edges
Weighted: edges can have a “weight”, either as a cost to
traverse, a number of times that you can use it, a capacity for
the edge, …
120
Common graph problems
121
Text converter problem
122
Text converter problem -- sample
<html>
<body>
<p>The quick <b>brown</b> fox <i>jumps</i> over the
<i><b>lazy</b></i> dog.</p>
<p>For another day or two.</p>
</body>
123
</html>
Spreadsheet problem
124
Navigation simulation
The city of Halifax wants to assess the flow of traffic in the city.
They will provide a map of the city, the maximum speed of cars
on each street, and whether intersections are stop signs, traffic
lights, or roundabouts.
Given a rate at which cars enter the city from each road, and
having each car choose a destination on the peninsula
(weighted more to downtown destinations), we will simulate how
the traffic flows through the city and which roads or
intersections have the greatest or smallest delays.
125
Features of data structures
Linked list
Simple to implement
Lots of flexibility in deciding the order of items
- The burden is on you to maintain that order
Search time not too critical
Binary search tree
Data has some order to it
Want efficient searches
Ok with one of
- Complex code to keep the tree balanced
- Approximations to a balanced tree are ok
- You’re expecting the order of added data will keep the tree semi-balanced
Heap
The data is ordered and you only ever want the biggest/smallest item
Graph
126 You are representing items _and_ connections or relations between items
Data structure performance comparison
128
Definitions of “best”
Incomplete list:
Fastest / most efficient
Least memory
Ease of understanding
Ease of maintaining
Ease of implementing
Scalability
Parallelizability
Portability
Serializability
Flexibility
…
129
Java classes
Concrete class
Abstract class
Interface
130
Concrete class
131
Concrete class syntax
132
The issue with concrete classes
133
Interface
134
Interface syntax
135
File compressor -- interfaces to the files
136
File compressor – implementation of a reader
class
137
File compressor with an interface
138
Abstract Class
139
Abstract class syntax
public abstract class tournament {
private String tournament_name;
public abstract void add_team ( String team_name);
public abstract int number_of_teams ( );
public void set_tournament_name ( String name ) {
tournament_name = name;
}
public void print_signup_sheet ( ) {
int i;
System.out.println( “Tournament sign-up: “ + tournament_name);
for (i = 0; i < number_of_teams( ); i++) {
System.out.println( i + “. _____________”);
}
}
140
}
Example
141
When do you use each class type?
142
Java Collection Framework
144
Text converter problem -- sample
<html>
<body>
<p>The quick <b>brown</b> fox <i>jumps</i> over the
<i><b>lazy</b></i> dog.</p>
<p>For another day or two.</p>
</body>
145
</html>
Navigation simulation
The city of Halifax wants to assess the flow of traffic in the city.
They will provide a map of the city, the maximum speed of cars
on each street, and whether intersections are stop signs, traffic
lights, or roundabouts.
Given a rate at which cars enter the city from each road, and
having each car choose a destination on the peninsula
(weighted more to downtown destinations), we will simulate how
the traffic flows through the city and which roads or
intersections have the greatest or smallest delays.
146
Navigation simulation
In:
Map of Halifax
Street info like speed limit
Types of intersections
For entry streets to the city, the rate at which cars arrive and
a destination for each car
Out:
Simulate traffic flow
Ultimately: average travel time and bottlenecks
147
Iterators
148
Iterators – typical use
ArrayList<String> collection;
…
Iterator<String> iterate = collection.iterator();
while (iterate.hasNext()) {
String data = iterate.next();
// do something with ”data”
}
…
for ( Iterator<String> it2 = collection.iterator(); it2.hasNext(); ){
String data2 = it2.next();
// do something with “data2”
}
…
for( String data3 : collection ) { // like an implicit iterator
// do something with “data 3”
149 }
Why use iterators?
Limitations
Don’t want your data structure changing under you at the
same time
Generally uni-directional
150
Anatomy of a process
memory
Call stack
Local variables, history of method calls
151
The call stack
152
The call stack
153
The call stack
hardware hardware
hardware
Register Register
values Register
values values
main(){ foo(int i, int j){
foo(int i, int j){ bar local
… First thing
First thing variables
foo( a, b ) …
… Register
X bar( j )
bar( j ) values
… Y
} Y Value of j
… …
foo local } Y
}
variables
Register foo local
values variables
Value of a i in foo Register
values
Value of b j in foo
Value of a
X
Value of b
Main local Main local
X
variables variables
Main local
154 variables
The call stack
hardware
When we set a
Register value in bar, we
values change the value in
bar(int n){ the stack frame of
bar local bar local bar. When foo
…
variables variables runs, its copy of the
n = 42;
Register Register value isn’t changed!
…
values values
return;
Value of j } 42
Y Y
n in foo n in foo
foo local foo local
variables variables
Register Register
values values
Value of a i in foo Value of a i in foo
Value of b j in foo Value of b j in foo
X X
Main local Main local
155 variables variables
The call stack
hardware hardware
Register Register
values values
bar(int n){ foo(int i, int j){
bar local bar local
… First thing
variables variables
n = 42; …
Register Register
… bar( j )
values values
return; Y
42 } 42 …
Y X } When we return
n in foo from bar, we restore
foo local foo local
variables variables the previous
Register information from the
Register
values values stack.
Value of a i in foo Value of a i in foo
All the information
Value of b j in foo Value of b j in foo
from bar is still lying
X X around in memory,
Main local Main local though.
156 variables variables
The call stack
hardware
Register
values frotz local
foo(int i, int j){ variables
bar local bar local
First thing
variables Register variables
…
Register values Register
bar( j )
values Value of i values
Y The stack frame for
42 Value of j 42
… frotz overlaps the
X frotz( i, j); Z X
space previously
Z used by bar.
foo local foo local
… variables
variables
} All the old values
Register Register
values values for bar are still in
Value of a i in foo Value of a memory, so that’s
Value of b j in foo Value of b what frotz sees as
X X data unless you
initialize your
Main local Main local
variables.
variables variables
The call stack – recursive call
hardware
Register
values foo local
foo(int i, int j){ variables
First thing
Register
…
values
foo (i-1, j-1) i in recursive call to foo
9
Z j in recursive call to foo
41
…
} Z
160
Problem solving
161
Problem solving - recursion
Examples?
162
Recursion
164
Common uses of recursion
165
Binary tree traversal
3 types:
b e - Pre-order traversal
- In-order traversal
- Post-order traversal
166
Binary tree pre-order traversal
Pre-order( Node n ) {
if (n != null) {
b e process n
Pre-order (n.left);
Pre-order (n.right);
}
}
a c g
167
Binary tree in-order traversal
In-order( Node n ) {
b e if (n != null) {
In-order (n.left);
process n
In-order (n.right);
}
a c g
}
168
Binary tree post-order traversal
Post-order( Node n ) {
if (n != null) {
b e Post-order (n.left);
Post-order (n.right);
process n
}
}
a c g
169
Binary tree breadth-first traversal
BFT( Node n ) {
Queue q;
b e while (n != null) {
process n
q.add (n.left);
q.add (n.right);
n = q.remove();
a c g
}
}
171
Recursion
Pro
Code can look simpler
Fewer lines of code
The call stack manages data to remember
Naturally fits some problems
Con
Can consume lots of stack space
Typically less time-efficient than iterative solutions
Can inadvertently solve the same sub-problem multiple times
- Consider memoization
172
Keys to recursion
Practice
173
Sudoku
174
Defensive Programming
175
Defensive Programming
176
Defensive Programming for Robustness
177
Defensive Programming
178
How can others influence your code?
User input
Parameter values
Resource permissions
Environment variables
Data read in
Files
Database
Network
179
Input Validation
180
Input Validation – Check for unexpected data
181
Input Validation – Check for unexpected data
Special characters
Scan strings for any characters that might have a special
meaning to other libraries where you plan to pass the data
- Eg. & character if you’re sending out HTML
; character in an SQL statement
“ character in a string
182
Input Validation – Check for unexpected data
183
Input Validation
184
Return Codes
185
Return Codes
186
Return Codes
187
Common Structure in C
188
Return Codes
Advantages
Portable concept across many languages
Easily recognized
Can structure the codes
Disadvantages
Error-handling merged with regular control flow
Need to coordinate the meaning of the return codes
Relies on the calling function to check for and act on errors
189
Exceptions
190
Exceptions
191
Exceptions
192
Exceptions
193
Exception Hierarchy
…
}
196
Catching an Exception in Java
class File_notFound_Demo {
class File_notFound_Demo {
class File_notFound_Demo {
201
Exception Blocks Good Practices
202
Sizing Exceptions
203
How much is too much?
204
205
Programming paradigms
C, Fortran, Cobol
Procedural programming
Generally focuses on the operations, steps, and transformations needed
to achieve an outcome
Object oriented programming Java, C++, Python
Focuses on the data, concepts, or elements around which computation is
happening
Lisp, ML, Haskell, OCaml
Functional programming
Program flow modeled as a composition of function calls
Prolog
Logic programming
Focus on the rules behind all the computation and let the running
environment look to combine rules as they apply to reach an answer.
206
Object oriented design
207
Slides courtesy Dr. Alex Brodsky
Approach from Chapter 12 of Big Java: Late Object by
Cay Horstmann
The Software Development Life Cycle
Requirements gathering
Analyse
Design
Implementation
Quality Assurance Maintain Design
Deployment
Maintenance
Deploy Implement
Test
Requirements Gathering
The first step of any software project is to figure out what it is that
you are building
- Functional requirements: What should it do?
- Nonfunctional requirements: Other
Requirements are gathered from:
- RFP: Request for Proposal
- Existing user base
- Prospective user base
- Management
- Etc.
A Software Requirements Specification (SRS) is a document that
specifies the software to be built: all functional and non-functional
requirements
Functional Requirements
Functional requirements describe
the functions of the proposed software
- What should it do?
- What resources can it use?
- What performance benchmarks should it meet?
- What other systems should if integrate with?
- What security standards should it meet?
Example: The assignment and practicum specifications:
- Input
- Processing
- Output
This does not typically specify the “how”, just the “what”
Nonfunctional Requirements
Goals:
- Identify what classes / objects we can reuse
- Identify what new classes / objects we need
- Identify what methods the classes / objects need
- Identify how the classes / objects interact
The Noun-Verb Approach
Idea:
- Use nouns from the problem domain to identify classes
- Use verbs associated with the nouns to identify methods for the
classes
Example: The Bank Simulation:
- Nouns: Bank, Teller, Client, Person, Line-up (Queue)
- Verbs:
Bank: Open, Close
Person: Arrive, Leave
Teller Serve Client
Client: Line-up, Get Served
Design is…
Iterative
You will make mistakes and will revisit the design
About tradeoffs and priorities
Review your software quality objectives
Nondeterministic
Managing complexity and restrictions
Heuristic
216
Design – Manage Complexity
Accidental Complexity
Complexity that we inherit from the processes, environment,
choices
Often disconnected from the base problem itself
217
Design – Manage Complexity
Essential Complexity
Complexity that arise from the problem or the interlocking
set of concepts in the solution
Arise no matter where or how we deploy the solution
218
Design
219
Good Design?
220
Levels of Design
- User interface
- Database access
- System dependencies
Classes
Methods
Method algorithms
221
Class-level Design Steps
Single Responsibility
Identify the objects and their attributes Open / Closed Principle
Mind Map
Determine what can be done to each Noun-Verb Approach
object Liskov Substitution
Determine what each object is allowed to Principle
CRC Method
do to other objects Interface
Determine parts of each object that will be Segregation
Dependency
visible to other objects Inversion
Define each object’s public interface
222
DSU Online Elections
224
DSU Online Elections – Nouns and Verbs?
DSU President
Voter
Appoint election officer Election
Authenticate Election
Vote for position Position slate
Vote on plebiscite Position vote
Plebiscite
Election officer Plebiscite vote
Create slates Position slate
Validate slates Plebiscite
Publish slates Election Election
Create plebiscite
Load valid students Set position slate Election officer
Open voting Set plebiscite Voter
Close voting Open Position slate
Tabulate results Close Plebiscite
Publish results Authenticate voter
Record position vote
Record plebiscite vote
Tabulate results
229
DSU Online Election
Position slate
Position vote
DSU President
Election
Voter Voter list
Registrar’s Office
231
Heuristics for Design
Form abstractions
Encapsulate implementation details
Use inheritance
Hide secrets
Identify areas likely to change
Anticipate different degrees of change
Keep coupling loose
Look for common design patterns
232
Form Abstractions
233
DSU Online Election
Election
Voter Voter list
Person
Election Officer
Registrar’s Office
Complements abstraction
235
Use inheritance
236
Hide secrets – information hiding
238
Identify areas likely to change
240
Keep coupling loose
241
Look for common design patterns
242
Design Considerations
243
Design practices
244
Common Design Criteria -- Cohesion
Bad cohesion
Overloaded
246
Common Design Criteria – Coupling
247
Coupling
248
Cohesion vs. Coupling
250
Single Responsibility Principle
251
Single Responsibility Principle
Principle kept
252
Single Responsibility Principle – bad example
253
Single Responsibility Principle – correct use
255
Open / Closed Principle
256
Open
257
Closed
258
Closed – Effect of poor use
extends extends
259
Liskov Substitution Principle
260
Java Collection Framework
Code uses
private class cell {
…
protected Set<Character> possibleValues = new HashSet<>();
…
} Allows us to change our minds on the implementation
later without searching _all_ the code for the class
Rather than name to change.
private class cell {
…
protected HashSet<Character> possibleValues = new HashSet<>();
…
} Locks us in to one implementation.
262
Design by Contract
263
Design by Contract
264
Liskov Substitution Principle
265
Factory Pattern Example in Java
267
Interface Segregation Principle
268
Bad Interface Design
Big interface
implements implements
implements
269
Good Interface Design
Interface 1 Interface 2 Interface 3 Interface 4 Interface 5
implements implements
Starting to have
too many
responsibilities?
270
Dependency Inversion Principle
271
Dependency Inversion Principle
class class
272
Non-Coding Example – e-mail addresses
274
Using SOLID
276
Student Information System – Mind Map
ArrayList
Program
HashSet ArrayList
Schedule Courses Payments
ArrayList
Registration Tuition Scholarships
Finances
Academic Student
record TA
Co-curricular Personal
Grad level record info
Payroll
Transcript
Address
Undergrad Web UI
level Name Employee
Engineering Mobile UI
upper level Personal
info
277
Student Information System – Single
Responsibility
ArrayList
Program
HashSet ArrayList
Schedule Courses Payments
ArrayList
Registration Tuition Scholarships
Finances
Academic Student
record TA
Co-curricular Personal
Grad level record info
Payroll
Transcript
Address
Undergrad Web U
level interaction Name Employee
Engineering
Mobile UI
upper level Personal
interaction
info
278
Student Information System – Open/Closed
ArrayList
Program
HashSet ArrayList
Schedule Courses Payments
ArrayList
Registration Tuition Scholarships
Finances
Activity Student
record TA
Academic
Grad level record Transcript
Payroll
Co-curricular Person
record
Undergrad Web U
level interaction Personal Employee
Engineering info
Mobile UI
upper level
interaction
Address
Name
279
Student Information System – Liskov Substitution
Principle
List
Program
Set List
Schedule Courses Payments
List
Registration Tuition Scholarships
Finances
Transcript
Activity Student
record TA
Academic
Grad level record
Payroll
Co-curricular Person
record
Undergrad Web U
level interaction Personal Employee
Engineering info
Mobile UI
upper level
interaction
Address
Name
280
Student Information System – Interface
Segregation
List
Program
Set List
Web UI Schedule Courses Payments
interface
List
Mobile UI Registration Tuition Scholarships
interface
Finances
Transcript
Activity Student
record TA
Academic
Grad level record
Payroll
Co-curricular Person
record
Undergrad
level Personal Employee
Engineering info
upper level
Address
Name
281
Student Information System – Dependency
Inversion
List
Program
Set List
Web UI Schedule Courses Payments
interface
List
Mobile UI Registration Tuition Scholarships
interface
Finances
Transcript
Activity Student
record TA
Academic
Grad level record
Trackable Payroll
Co-curricular activity Person
record interface
Undergrad
level Personal Employee
Engineering info
upper level
Address
Name
282
Student Information System – Dependency
Inversion
List
Program
Set List
Web UI Schedule Courses Payments
interface
List
Mobile UI Registration Tuition Scholarships
interface
Finances
Transcript
Activity Student
record TA
Academic
Grad level record
Trackable Payroll
Co-curricular activity Person
record interface
Undergrad
level Personal Employee
Engineering How is our cohesion? info
upper level How is our coupling?
Address
Name
Refine more original red?
283
Dependency
285
Aggregation
Relationship: “has a”
This is a stronger version of
dependency
Objects of one class contain Car
objects of another class
A class has to have instance
variable(s) that store objects
of the other class Engine Tires
E.g., a Car is an aggregation
of an Engine, Tires, and
other parts
Note: A class may use a Piston Spark Plug
collection to store multiple
objects
Aggregation
Engine and Tires objects are attributes inside the Car class
287
Nested Classes
Relationship: “has a”
LinkedList
Class contains another
nested class
This is an aggregation Node Iterator
of classes rather than
objects
E.g. a LinkedList class
defines a Node class
within it
Nested
289
Inheritance
This is an “is a”
relationship
Between a more general Vehicle
class (superclass) and a
more specific class
(subclass) Car Truck
E.g.
- El Camino is a Car
- Porche is a Car El Camino Porche
- Car is a Vehicle
Inheritance
291
UML Relationship Symbols
http://blog.nuvemconsulting.com/interviewing-tips-for-
software-requirements-gathering/
https://stevenwilliamalexander.wordpress.com/2015/07/31/no
n-functional-requirements-cart-before-horse/
https://www.teacherspayteachers.com/Product/Parts-of-
Speech-Printable-Posters-Noun-Verb-Adjective-Adverb-
218930
https://www.travel-palawan.com/palawan-dos-dont/
http://pengetouristboard.co.uk/vote-best-takeaway-se20/
295
What is software engineering?
Moments of brilliance
A lot can happen in the right
Productivity
time
Difficult to predict or plan for
Difficult to align a team
Time
Time
One size fits all for software engineering?
Entrepreneur
Corporate software
We are still seeking
out a market, so you The target market is
might choose to generally well-known
change the target of as are the key
the software quickly requirements
Web software
The market reach is
broad and you can
Hobbyist have trouble
Consulting identifying everyone
The focus is on the using it. Quick
challenging part and You are hired by a feedback and quick
the beautiful part specific client for a turnaround on
specific task. The changes to the
Real-time systems task could change or market.
adapt as the client
The task is well- learns more
defined, but failure is
not an option.
Styles of processes
Deploy and
maintain
here
Clients involved here
Plan-based development
eg. Waterfall method
Deploy and
Requirement Design Executable Proof of maintain
s document documents code test here
results
Iterative development
eg. Agile methods
Deploy and
Each row is an iteration. maintain
Working (but limited) here
code at the end of each Clients involved here
iteration.
Tradeoffs
Plan-based
+ You get a full picture, so you optimize the design and
implementation
- Errors detected late in the process are costly to undo
- The client sees little until everything is built
Iterative
+ Continual client feedback to adjust the direction
+ Not as much backtracking when requirement or design
errors are detected
- Feature creep
- Rewriting / reorganizing code (called refactoring) is more
commonly needed
Agile development
Characteristics of agile development
Too high-level
Break it down
Describes a development task
Wrong “who”
Too specific in giving the method
Limiting the options and not focusing on the functionality
Reason not linked to generating value to the “who”
Creates a hodge podge of functionality
What makes a good user story
324
Acceptance Tests
Business risk
Are we developing something of value?
Technical risk
Is there something that could go wrong with the technology
- in development?
- in deployment?
Personnel risk
Usually performed
Risk identification
Risk analysis
Risk review
Risk Identification
Weak areas
- Eg. Unknown technology
Set priorities
Risk Analysis – Overall Impact
343
Software Architecture
344
What can you tell from this shelter?
345
Images from Google images, March 4, 2019
Can you get a better sense of location or area
from architectures?
346
Images from Google images, March 4, 2019
Architecture Definitions
348
Software Architectures
Hybrid architectures
(Categorization from
https://en.wikipedia.org/wiki/List_of_software_architecture_styles_and_patterns )
349
Structure architectures
Monolithic
Layered
Pipes and filters
350
Monolithic Architecture
351
Monolithic Architecture
352
Monolithic Architecture
353
Layered Architecture
354
Layered Architecture
355
Mac OSX
356
Computer Network Structure
357
Layered Architectures - Advantages
Easy to understand
Quick to locate where some tasks must be done
Allows us to change the implementation of one layer
without affecting the whole system
Strong cohesion in each layer and loose coupling
among layers
358
Layered Architectures - Disadvantages
359
Layered Architectures
360
Pipe and Filter Architecture
361
Pipe-and-Filter Architecture
A sequence of special-purpose
programs (filters) that pass data
from one to the other sequentially
to achieve a result
363 For the UNIX astute, ls will already sort, so the example is a bit contrived
Business Process Workflows
364
Data Stream Databases
366
Pipe-and-Filter Architectures
367
Messaging architectures
Event-driven
Finite state machines
Model-View-Controller
Publish-subscribe
Message queues
368
Event Driven Architecture
369
Event-Driven Architecture
370
Event-Driven Architecture
372
Model-View-Controller Architecture
373
Publish-Subscribe Architecture
374
Publish-Subscribe Architecture
375
Message Queue Architecture
377
Message Queue Architecture - Advantages
Simple
Portable
Agents handle data when it’s convenient for them
Long-lived style of communication, so available just
about everywhere
378
Message Queue Architecture - Disadvantages
379
Distributed system architectures
Client-server
2-tier
3-tier (presentation, domain logic, data storage)
- Common for the web (client tier, web server tier, DBMS tier)
Peer-to-peer
Representational state transfer (REST)
Service-oriented
Precursor to cloud systems
380
Client Server Architecture
381
Client Server Architecture
382
Client Server Architecture
The server
Has all the data / services
Is always present on the network
Waits around to be contacted by clients
- Doesn’t initiate contact with clients
The client
Wants the data or service
- Has no data or service to offer to others
Initiates the contact with the server
Can come and go
- Is unreliable as far as the network is concerned
383
Client Server Advantages
384
Client Server Disadvantages
385
3 Tier Architecture
386
Peer to Peer Architecture
387
Peer to Peer Architecture
388
Peer to Peer Architecture - Advantages
Scales easily
Shares the workload across many computers
Reconfigures itself automatically as nodes come and
go
389
Peer to Peer Architecture – Disadvantages
390
REST Framework
391
REST Framework
392
REST Framework
393
REST Framework
394
Service-Oriented Architecture
395
Service Oriented Architecture
396
Service Oriented Architectures
398
Network “Architectures”
Cloud
Your entire program is run / serviced on someone else’s hardware
Definitions vary:
“A large-scale distributed computing paradigm that is driven by
economies of scale, in which a pool of abstracted virtualized,
dynamically-scalable, managed computing power, storage, platforms,
and services are delivered on demand to external customers over the
Internet” [Foster et al., 2008]
“A style of computing where scalable and elastic IT capabilities are
provided as a service to multiple external customers using Internet
technologies.” [Plummer et al., 2008]
“Cloud computing is a model for enabling convenient, on-demand
network access to a shared pool of configurable computing resources
(e.g. networks, servers, storage, applications, and services) that can be
rapidly provisioned and released with minimal management effort or
service provider interaction.” [Mell and Grance, 2010]
Cloud Computing Characteristics
Common elements:
Virtualization
- hardware can host many independent simulated servers
Multi-tenancy
- multiple clients can occupy the same physical hardware
Security
- clients are protected from each other and their data is secure
Elasticity
- resources can be added and removed in real-time, often at the request of
the client and without the intervention of the service provider
Cloud Computing Characteristics
Common elements:
Availability
- the service provider gives performance / QoS guarantees
Reliability
- failure of any piece still allows the services to be offered
Agility
- the resource allocations can adapt dynamically
Pay-as-you-go
- the client just pays for the resources used
Client
the network
virtualization
Underlying hardware and host OS
Hybrid Architectures
404
Hybrid Architectures
406
Microkernel Architecture
412
Microkernel Architecture
413
Microkernel Architecture
414
Microkernel
415
Shared memory architectures
Blackboard
Rule-based
416
Blackboard Architecture
417
Blackboard Architecture
418
Blackboard - Advantages
419
Blackboard - Disadvantages
420
Blackboard Architecture
421
Rule-Based Architecture
422
Rule-Based Architecture
423
Rule-Based Architecture
Consists of
A set of rules (knowledge base)
A means by which we acquire data from users, agents,
or the environment
An inference engine that
matches new data against
the knowledge base
424
Rule-Based Architecture
425
Rule-Based Architecture - Advantages
426
Rule-Based Architecture - Disadvantages
Performance
Searching the knowledge base can take time
Hard to get a full picture of the whole operation from
just the rules
Need to have the right set of rules
Should ensure that we don’t have contradictory rules in
the system
427
Databases
428
What are we dealing with?
Data
Stored representations of objects and events that have meaning and
importance in the user’s environment
Information
Data that has been processed in such a way as to increase the
knowledge of the person who uses the data
Metadata
Data that describe the properties or characteristics of end-user data, and
the context of that data.
Eg: name, type, range restrictions on numeric data, …
Database management system (DBMS)
A software system that is used to create, maintain, and provide
controlled access to user databases.
429 Definitions from “Modern Database Management” by Jeffrey Hoffer, Mary Prescott, and Heikki Topi, 9th edition
Database basics
430
Sample table of data
(from excel)
Term Subject Course Number
Section CRN Schedule Type
Monday Tuesday Wednesday Thursday Friday Begin Time End Time Building Room
201810 ACAD 20 1 18188 L W 935 1025 HALEY INSTITUTE 116
201810 ACAD 1050 1 18112 L M W 1005 1125 BANTING BUILDING 32
201810 ACSC 4703 1 14095 L T R F 1035 1125 CHASE BLDG 319
201810 ACSC 4720 1 14096 L M W F 1135 1225 CHASE BLDG 319
201810 ACSC 4950 1 18296 L
201810 AGRI 1000 1 14097 L T R 1005 1125 AGRICULTURAL COX INSTITUTE
24
201810 AGRI 1000 B01 14098 B F 1235 1425 AGRICULTURAL COX INSTITUTE
260
201810 AGRI 1000 B01 14098 B F 1235 1425 AGRICULTURAL COX INSTITUTE
261
201810 AGRI 1000 B01 14098 B F 1235 1425 AGRICULTURAL COX INSTITUTE
262
201810 AGRI 1000 B02 14099 B F 1435 1625 AGRICULTURAL COX INSTITUTE
260
201810 AGRI 1000 B02 14099 B F 1435 1625 AGRICULTURAL COX INSTITUTE
261
201810 AGRI 1000 B02 14099 B F 1435 1625 AGRICULTURAL COX INSTITUTE
262
201810 AGRI 4000 1 14102 L W 1735 2025 HALEY INSTITUTE 110
Program-data independence
Planned data redundancy (or removal of redundancy)
Improved data consistency
Improved data sharing
Increased productivity of application development
Enforcement of standards
Improved data quality
Improved data accessibility and responsiveness
Reduced program maintenance
432
Improved decision support
Disadvantages of a DBMS
433
Some more terminology…
Schema
The structure that contains descriptions of objects created
by a user, such as base tables, views, and constraints, as
part of a database.
Catalog
A set of schemas that, when put together, constitute a
description of a database.
434
Schemas
436
Example table description - mysql
437
Mysql data types
Insert
Query
“select” statement
Delete
Update
439
Insert basics
440
Query basics
441
Set restriction with a predicate
442
Basic select statement
Example:
443
Basic select statement
Where…
444
Select “from” element – input specification
Examples:
446
Select column list – output specification
447
Can name outgoing columns
Select column list
Examples
448
Select column list - transformations
Avg()
Count()
Min()
Max()
Std()
Variance()
Sum()
Format()
449
Select column list - transformations
Concat()
Lcase() or lower()
Ucase() or upper()
Left(), Right, or Mid()
Length()
Ltrim(), Rtrim(), Trim()
Lpad() or Rpad()
…
450
Select transformations
Examples
Standard comparators
=, !=, <>, >, <, >=, =<, !<, !>
Numeric ranges – “between”
Select name from person where salary between 32000 and 50000
Set inclusion – “in”
Select person_id from registration where course_id in (1, 2, 3)
Select distinct person_id from registration where course_id in (1, 2, 3)
Near matches – “like”
% matches 0 or more characters, _ matches 1 character
Select name from person where name like “C%”
Works on numbers too: select * from person where salary like “3%”
NULL check – “is null”
453
Additional “select” specifications
7
2996
110
23
326
122
7 273
457
Typical join “shortcut”
459
Example
becomes
Select *
from course join registration using (course_id);
or
Equi-join
Natural join
462
Join examples
sample1 sample2
Left join
Right join
463
Join conditions
Outer joins often invoke “is null” or “is not null” in the
where condition to filter the results
sample1 sample2
464
Query Execution
Column selection
Identifies what to keep as you process. Low cost, unless you’re using
transformations
From clause
Generates combinations of records. High cost if you’re generating many records
that you will just throw away
Where clause
Does winnowing as you process. Can have complex sets of conditions to
evaluate. Medium cost
Group by clause
Need the final data to make this work. Throwing away most of the generated
records to create summaries.
Order by clause
Need the final data to execute. Cost relative to output size, not generated records
size.
466
Helping performance
467
Are select statements unique?
468
Subqueries
Select *
from person join registration using (person_id)
where person_id = 3;
versus
select *
from
(select * from person where person_id = 3) as interested
join registration using (person_id);
Is there a difference?
470
Subquery example
select *
from person as p, course as c, registration as r
where p.person_id = r.person_id
and c.course_id = r.course_id
and p.name like "A%"; How many rows
does each query
versus create?
select * from
(
(select * from person where name like "A%") as p
join registration using (person_id)
) join course as c using (course_id);
471
Subqueries
473
Combining outputs of queries
494
Union example
496
Intersection Example
497
Except / minus in mysql
498
Except example
499
Views
500
View syntax
503
Just the tip of the SQL iceberg
504
Just the tip of the SQL iceberg
505
Case statement
Example
Select city, case when territory = "NA" then "North America"
else territory end as Territory from offices;
506
With statement example
Represent
select EmployeeID, FirstName, LastName
from employees
where EmployeeID in (select distinct ReportsTo from employees);
as
with supervisorIDs as
( select EmployeeID, FirstName, LastName )
507
Changing records
508
Removing records
509
CRUD operations
Create
Insert into … values …
Read
Select … from … where …
Update
Update … set … where …
Delete
Delete from … where …
510
Effect of timing
512
ACID properties – key for a DBMS to maintain
Atomic
The transaction cannot be subdivided. It is either complete
done or no part is done.
Consistent
Any database constraint / property / relation that existed
before the transaction must also exist after the transaction
Isolated
Changes to the database are not revealed to users until the
transaction is committed
Durable
Changes are permanent
513
ACID properties
514
Describing the database
515
Entity Relation Diagrams (ER Diagrams or ERD)
Includes
Entities – a person, place, object, event, or concept in the user
environment about which the organization wishes to maintain data
Relations – a meaningful association between or among entities
516
Entities
Aim for:
A singular noun – helps to keep it to a single concept
Something specific to the organization
Something concise
Named for a result/artefact, not a process or procedure
Comprised of attributes
517
Attributes
519
Attributes
520
ERM symbols – entities
strong entity weak entity associative entity
Table Name Table Name Table Name
Cardinality
The number of entities that participate in the relation
522
ERM symbols – relations
Optional one
Mandatory one
Optional many
Mandatory many
523
Database lab
7
2996
110
23
326
122
7 273
525
Data Modeling
Defines
which entities you have,
how they are grouped,
the relation between entities, and
the cardinality of the relations.
526
Creating tables
Primary key
An attribute (or combination of attributes) that uniquely
identifies each row in a relation
- The choice of primary key may not be unique
Composite key
A primary key that consists of more than one attribute.
528
Relational keys
Foreign key
An attribute in a relation that serves as the primary key of
another relation in the same database.
Surrogate key
A serial number or other system assigned primary key for a
relation
Often created to replace
- a complex or highly-composite primary key
- an expensive primary key (often big strings)
- a primary key that could be re-used over time
529
SQL Context
Eg.
532
Deleting tables
533
Converting to a database
534
Conversion steps
535
Map regular entities
536
Multivalued Attributes
Solution 1: Create just one table that can list the skills:
employee employeeSkill
employeeID employeeID One employee can have many
rows in the employeeSkill table.
skillName
No quick way to ensure that we’re
typing the skill names
consistently.
537
Multivalued Attributes
538
Map weak entities
539
Map binary relations
FK id1
540
Map binary relations
541
Binary relations example
Business side: each order will have at most one payment and we don’t allow
payments to cover more than one order
orders payments
orderID paymentID Note: 2 different ways to
declare the primary keys.
Both are ok.
orderID
create table orders (orderID int not null auto_increment primary key);
Business side: each order will have at most one payment and we don’t allow
payments to cover more than one order
orders payments
orderID paymentID Alternatively, have the
paymentID in the order, but
allow it to be NULL for the “no
payments” option (less
paymentID desirable solution, but still
works).
create table payments (paymentID int not null auto_increment);
offices employees
officeID The foreign key side is on the
employeeID
“many” table.
create table offices (officeID int not null auto_increment primary key);
employees customers
employeeID customerID
545
Binary relations – example many-to-many
Business rule: customers have one or more employee contacts and
employees look after multiple customers
employees contacts customers
employeeID employeeID customerID
customerID
Create table employees ( employeeID int not null auto_increment primary key);
Create table customers (customerID int not null auto_increment primary key);
Create table contacts ( employeeID int not null,
customerID int not null,
primary key (employeeID, customerID),
foreign key (employeeID) references employees (employeeID),
foreign key (customerID) references customers (customerID ) );
546
Map associative entities
547
Map unary relations
548
Map ternary relations
549