CSC207H Software Design Course Overview

C
S
C
2
0
7
h
t
t
p
:
/
/
w
w
w
.
t
e
a
c
h
.
c
s
.
t
o
r
o
n
t
o
.
e
d
u
/
~
c
s
c
2
0
7
h
/
w
i
n
t
e
r
/
I
n
s
t
r
u
c
t
o
r
s
:
 
L
i
n
d
s
e
y
 
S
h
o
r
s
e
r
,
 
J
a
i
s
i
e
 
S
i
n
E
m
a
i
l
:
 
c
s
c
2
0
7
1
8
s
@
c
s
.
t
o
r
o
n
t
o
.
e
d
u
Winter 2018
1
An introduction to software design and development concepts,
methods, and tools using a statically-typed object-oriented
programming language such as Java. Topics from: version
control, unit testing, refactoring, object-oriented design and
development, design patterns, advanced IDE usage, regular
expressions, and reflection. Representation of floating-point
numbers and introduction to numerical computation.
— The Arts and Science Calendar
Y
o
u
 
… know the CSC108, CSC148, and (usually) the CSC165
material: lists, dictionaries, functions, classes, stacks, queues,
trees, recursion, unit testing, logical notation and reasoning,
the basics of computational complexity (big-Oh), and an
approach to developing functions (the function design recipe).
… presumably want to do very well in this course.
… should expect to spend 8–10 hours a week on each of your
courses (including lectures and labs).
… might think about this course as training for a software
internship.
2
Learning goal:
Object-oriented programming in a
statically-typed language
Strong typing
Lots of inheritance
Unit testing
File handling
A memory model for Java
Exception handling
Floating-point issues
How to think about and plan a large program
How to analyze requirements
How to safely refactor code
Design patterns
Version control (using git)
Aspects of team dynamics
An Integrated Development Environment (IntelliJ)
Learning goal:
F
u
n
d
a
m
e
n
t
a
l
 
c
o
d
e
 
d
e
v
e
l
o
p
m
e
n
t
t
e
c
h
n
i
q
u
e
s
 
u
s
e
d
 
p
r
o
f
e
s
s
i
o
n
a
l
l
y
4
H
o
w
 
w
e
r
e
 
g
o
i
n
g
 
t
o
 
t
e
a
c
h
2
 
l
e
c
t
u
r
e
 
h
o
u
r
s
 
/
 
w
e
e
k
1
0
 
(
i
s
h
)
 
1
-
h
o
u
r
 
l
a
b
s
1
 
4
5
-
m
i
n
u
t
e
 
m
i
d
t
e
r
m
 
i
n
 
w
e
e
k
 
5
2
 
i
n
d
i
v
i
d
u
a
l
 
a
s
s
i
g
n
m
e
n
t
s
A
 
t
w
o
-
p
h
a
s
e
 
p
r
o
j
e
c
t
 
i
n
 
t
h
e
 
s
e
c
o
n
d
 
h
a
l
f
 
o
f
 
t
h
e
 
c
o
u
r
s
e
1
 
f
i
n
a
l
 
e
x
a
m
5
C
o
u
r
s
e
w
o
r
k
 
O
v
e
r
v
i
e
w
6
R
e
s
o
u
r
c
e
s
P
o
r
t
a
l
/
B
l
a
c
k
b
o
a
r
d
:
 
A
n
n
o
u
n
c
e
m
e
n
t
s
,
 
G
r
a
d
e
s
C
o
u
r
s
e
 
w
e
b
s
i
t
e
:
 
R
e
a
d
i
n
g
s
,
 
L
i
n
k
s
www.teach.cs.toronto.edu/~csc207h/winter/
D
i
s
c
u
s
s
i
o
n
 
b
o
a
r
d
O
f
f
i
c
e
 
h
o
u
r
s
BA3219: Tuesdays 10 am -12 pm, Wednesdays 2–4 pm
L
e
c
t
u
r
e
s
 
a
n
d
 
l
a
b
s
!
A
n
o
n
y
m
o
u
s
 
f
e
e
d
b
a
c
k
 
(
i
f
 
y
o
u
 
d
o
n
t
 
w
a
n
t
 
t
o
 
e
m
a
i
l
 
u
s
 
o
r
 
p
o
s
t
 
o
n
t
h
e
 
b
o
a
r
d
s
)
:
 
p
l
e
a
s
e
 
g
i
v
e
 
u
s
 
c
o
n
s
t
r
u
c
t
i
v
e
 
s
u
g
g
e
s
t
i
o
n
s
!
H
e
l
p
 
C
e
n
t
r
e
 
i
n
 
B
A
2
2
3
0
 
e
v
e
r
y
 
w
e
e
k
d
a
y
 
2
-
6
 
p
m
,
 
e
x
c
e
p
t
 
h
o
l
i
d
a
y
s
P
C
R
S
 
a
n
d
 
O
n
-
L
i
n
e
 
T
u
t
o
r
i
a
l
s
 
(
s
e
e
 
c
o
u
r
s
e
 
w
e
b
s
i
t
e
)
7
G
e
n
e
r
a
l
 
R
e
s
o
u
r
c
e
s
Lectures and Labs
Office Hours
Discussion Forum
Help Centre
On-Line Communities?
PCRS and on-line tutorials
Oracle Website
8
J
a
v
a
 
R
e
f
e
r
e
n
c
e
 
M
a
t
e
r
i
a
l
s
Course website (readings, lecture notes, links)
This reference is particularly useful:
http://docs.oracle.com/javase/tutorial/java/TOC.html
Java PCRS
This website does a nice job walking you through Java if the PCRS isn’t enough:
https://www.sololearn.com/Course/Java/
Email registration is required
Have you heard about disposable email addresses?
https://en.wikipedia.org/wiki/Disposable_email_address
Here’s a top-15 article about the topic: 
www.updateland.com/15-best-fake-email-
address-generator-online/
9
8
1
0
 
h
o
u
r
s
 
b
e
f
o
r
e
 
t
h
e
 
e
n
d
o
f
 
t
h
e
 
d
a
y
 
n
e
x
t
 
W
e
d
n
e
s
d
a
y
Attend lecture (2 hours)
Attend lab next week (1 hour)
Log into the Teaching Labs and run IntelliJ IDEA (1/2 hour)
Install Git, Java, and IntelliJ on your own computer (1 hour)
Work through Quest 1 on the PCRS and practice in IntelliJ (4
hours)
Lab next week will involve Java code, so you should try to
get through as much as you can before this Monday
10
W
h
a
t
 
d
o
e
s
 
i
t
 
m
e
a
n
 
t
o
 
r
u
n
 
a
p
r
o
g
r
a
m
?
What is a program?
A set of instructions for a computer to follow.
To 
run
 a program, it must be translated from a high-level
programming language
 to a low-level 
machine language
 whose
instructions can be executed.
Roughly, two flavours of translation:
Interpretation
Compilation
11
I
n
t
e
r
p
r
e
t
e
d
 
v
s
.
 
C
o
m
p
i
l
e
d
Interpreted (like Python)
Translate and execute one statement at a time
Compiled (like C)
Compile the entire program (once), then execute (any
number of times)
Hybrid (like Java)
Compile to something intermediate (in Java, bytecode)
The Java Virtual Machine (JVM) runs this intermediate
code
12
C
o
m
p
i
l
i
n
g
 
J
a
v
a
If using command line, you need to do this manually.
First, compile using “javac”:
jsin@laptop
$
 javac HelloWorld.java
This produces file “HelloWord.class”:
jsin@laptop
$
 ls
HelloWorld.class  HelloWorld.java
Now, run the program using “java”:
jsin@laptop
$
 java HelloWorld
Hello world!
Most modern IDEs offer to do this for you (IntelliJ does).
But you should know what’s happening under the hood!
13
S
O
L
I
D
 
P
r
i
n
c
i
p
l
e
s
 
o
f
O
b
j
e
c
t
-
O
r
i
e
n
t
e
d
 
D
e
s
i
g
n
How do we make decisions about what is better and
what is worse design?
Principles to aim for instead of rules.
For example, there is no maximum number of
class you should have in your program, nor a
minimum. But if the number of classes violates a
generally accepted principle, you should
reconsider your class structure.
The SOLID principles are useful and cover most major
situations you are likely to encounter.
14
S
o
f
t
w
a
r
e
 
d
e
s
i
g
n
 
g
o
a
l
s
A major goal when programming is to write an easy-
to-read, hard-to-break, maintainable, efficient
program.
Software design has you use a set of principles and
techniques that help you do this. This lecture is an
introduction to the tools and techniques we’ll see in
this course.
We’ll cover each of these in more detail later in the
course.
15
F
u
n
d
a
m
e
n
t
a
l
 
O
O
P
c
o
n
c
e
p
t
s
A
b
s
t
r
a
c
t
i
o
n
 
 
t
h
e
 
p
r
o
c
e
s
s
 
o
f
 
d
i
s
t
i
l
l
i
n
g
 
a
 
c
o
n
c
e
p
t
 
t
o
 
a
 
s
e
t
 
o
f
 
e
s
s
e
n
t
i
a
l
c
h
a
r
a
c
t
e
r
i
s
t
i
c
s
.
E
n
c
a
p
s
u
l
a
t
i
o
n
 
 
t
h
e
 
p
r
o
c
e
s
s
 
o
f
 
b
i
n
d
i
n
g
 
t
o
g
e
t
h
e
r
 
d
a
t
a
 
w
i
t
h
 
m
e
t
h
o
d
s
t
h
a
t
 
m
a
n
i
p
u
l
a
t
e
 
t
h
a
t
 
d
a
t
a
,
 
a
n
d
 
h
i
d
i
n
g
 
t
h
e
 
i
n
t
e
r
n
a
l
 
r
e
p
r
e
s
e
n
t
a
t
i
o
n
.
The result of applying abstraction and encapsulation is (often) a class
with instance variables and methods that together model a concept from
the real world. (Further reading: what’s the difference between
Abstraction, Encapsulation, and Information hiding?)
I
n
h
e
r
i
t
a
n
c
e
 
 
t
h
e
 
c
o
n
c
e
p
t
 
t
h
a
t
 
w
h
e
n
 
a
 
s
u
b
c
l
a
s
s
 
i
s
 
d
e
f
i
n
e
d
 
i
n
 
t
e
r
m
s
a
n
o
t
h
e
r
 
c
l
a
s
s
,
 
t
h
e
 
f
e
a
t
u
r
e
s
 
o
f
 
t
h
a
t
 
o
t
h
e
r
 
c
l
a
s
s
 
a
r
e
 
i
n
h
e
r
i
t
e
d
 
b
y
 
t
h
e
s
u
b
c
l
a
s
s
.
P
o
l
y
m
o
r
p
h
i
s
m
 
(
m
a
n
y
 
f
o
r
m
s
)
 
 
t
h
e
 
a
b
i
l
i
t
y
 
o
f
 
a
n
 
e
x
p
r
e
s
s
i
o
n
 
(
s
u
c
h
 
a
s
a
 
m
e
t
h
o
d
 
c
a
l
l
)
 
t
o
 
b
e
 
a
p
p
l
i
e
d
 
t
o
 
o
b
j
e
c
t
s
 
o
f
 
d
i
f
f
e
r
e
n
t
 
t
y
p
e
s
.
16
F
u
n
d
a
m
e
n
t
a
l
 
O
O
D
 
g
o
a
l
s
:
l
o
w
 
c
o
u
p
l
i
n
g
,
 
h
i
g
h
 
c
o
h
e
s
i
o
n
C
o
u
p
l
i
n
g
 
 
h
o
w
 
m
u
c
h
 
a
 
c
l
a
s
s
 
i
s
 
d
i
r
e
c
t
l
y
 
l
i
n
k
e
d
 
t
o
 
a
n
o
t
h
e
r
c
l
a
s
s
.
High coupling means that changes to one class may lead to
changes in several other classes.
Low coupling is, therefore a desired goal.
C
o
h
e
s
i
o
n
 
 
h
o
w
 
m
u
c
h
 
t
h
e
 
f
e
a
t
u
r
e
s
 
o
f
 
a
 
c
l
a
s
s
 
b
e
l
o
n
g
 
t
o
g
e
t
h
e
r
.
Low cohesion means that methods in a class operate on
unrelated tasks. This means the class does jobs that are
unrelated.
High cohesion means that the methods have strongly-related
functionality.
17
F
u
n
d
a
m
e
n
t
a
l
 
O
O
D
p
r
i
n
c
i
p
l
e
s
SOLID: five basic principles of object-oriented
 (Developed by
Robert C. Martin, affectionately known as “Uncle Bob”.)
Single responsibility principle
Open/closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle
18
S
i
n
g
l
e
 
R
e
s
p
o
n
s
i
b
i
l
i
t
y
P
r
i
n
c
i
p
l
e
Every class should have a single responsibility.
Another way to view this is that a class should only have one reason to
change.
But who causes the change?
This principle is about people.
 … When you write a software module, you
want to make sure that when changes are requested, those changes can only
originate from a single person, or rather, a single tightly coupled group of
people representing a single narrowly defined business function. You want to
isolate your modules from the complexities of the organization as a whole, and
design your systems such that each module is responsible (responds to) the
needs of just that one business function.” [Uncle Bob, 
The Single
Responsibility Principle
]
O
p
e
n
/
C
l
o
s
e
d
 
P
r
i
n
c
i
p
l
e
(
s
i
m
p
l
i
f
i
e
d
)
S
o
f
t
w
a
r
e
 
e
n
t
i
t
i
e
s
 
(
c
l
a
s
s
e
s
,
 
m
o
d
u
l
e
s
,
 
f
u
n
c
t
i
o
n
s
,
 
e
t
c
.
)
 
s
h
o
u
l
d
 
b
e
o
p
e
n
 
f
o
r
 
e
x
t
e
n
s
i
o
n
,
 
b
u
t
 
c
l
o
s
e
d
 
f
o
r
 
m
o
d
i
f
i
c
a
t
i
o
n
.
Add new features not by modifying the original class, but rather
by extending it and adding new behaviours, or by adding 
plugin
capabilities.
“I’ve heard it said that the OCP is wrong, unworkable,
impractical, and not for real programmers with real work to do.
The rise of plugin architectures makes it plain that these views
are utter nonsense. On the contrary, a strong plugin
architecture is likely to be the most important aspect of future
software systems.” [Uncle Bob, 
The Open Closed Principle
]
O
p
e
n
/
C
l
o
s
e
d
 
P
r
i
n
c
i
p
l
e
(
s
i
m
p
l
i
f
i
e
d
)
An example, using inheritance:
 
area
 calculates the area of all 
Rectangle
s in the input.
 What if we need to add more shapes?
O
p
e
n
/
C
l
o
s
e
d
 
P
r
i
n
c
i
p
l
e
(
s
i
m
p
l
i
f
i
e
d
)
With this design, we can add any number of shapes (open for extension)
and we don't need to re-write the 
AreaCalculator
 class (closed for
modification).
L
i
s
k
o
v
 
S
u
b
s
t
i
t
u
t
i
o
n
P
r
i
n
c
i
p
l
e
 
(
s
i
m
p
l
i
f
i
e
d
)
If S is a subtype of T, then objects of type S may be
substituted for objects of type T, without altering any of the
desired properties of the program.
“S is a subtype of T”?
 In Java, S is a 
child class
 of T,  or S 
implements
 interface T.
For example, if C is a child class of P, then we should be able
to substitute C for P in our code without breaking it.
L
i
s
k
o
v
 
S
u
b
s
t
i
t
u
t
i
o
n
P
r
i
n
c
i
p
l
e
 
(
s
i
m
p
l
i
f
i
e
d
)
 A classic example of breaking this principle:
L
i
s
k
o
v
 
S
u
b
s
t
i
t
u
t
i
o
n
P
r
i
n
c
i
p
l
e
 
(
s
i
m
p
l
i
f
i
e
d
)
In OO programming and design, unlike in math, it is not the
case that a Square is a Rectangle!
This is because a Rectangle has 
more
 behaviours than a
Square, not less.
The LSP is related to the Open/Closed principle: the
subclasses should only extend (add behaviours), not modify
or remove them.
I
n
t
e
r
f
a
c
e
 
S
e
g
r
e
g
a
t
i
o
n
P
r
i
n
c
i
p
l
e
Here, 
interface
 means the public methods in a class. (In Java, these are
often specified using a Java 
interface
,
 which you’ll learn about soon.)
Context: a class that provides a service for other “client” programmers
usually requires that the clients write code that has a particular set of
features. The service provider says “your code needs to have this
interface”.
No client should be forced to implement irrelevant methods of an
interface. Better to have lots of small, specific interfaces than fewer
larger ones: easier to extend and modify the design.
(Uh oh: “The interface keyword is harmful.” [Uncle Bob, 
'Interface'
Considered Harmful
])
D
e
p
e
n
d
e
n
c
y
 
i
n
v
e
r
s
i
o
n
p
r
i
n
c
i
p
l
e
When building a complex system, programmers are often
tempted to define “low-level” classes first and then build
“higher-level” classes that use the low-level classes directly.
But this approach is not flexible! What if we need to replace
a low-level class? The logic in the high-level class will need to
be replaced — an indication of high coupling.
To avoid such problems, we introduce an 
abstraction layer
between low-level classes and high-level classes.
D
e
p
e
n
d
e
n
c
y
 
i
n
v
e
r
s
i
o
n
p
r
i
n
c
i
p
l
e
Goal:
You want to decouple your system so that you can change
individual pieces without having to change anything more
than the individual piece.
Two aspects to the dependency inversion principle:
High-level modules should not depend on low-level
modules. Both should depend on abstractions.
Abstractions should not depend upon details. Details
should depend upon abstractions.
Example: you have a large system, and part of it has Managers
manage Workers. Let’s say that the company is restructuring and
introducing new kinds of workers, and wants the code updated to
reflect this.
Your code current has a Manager class and a Worker class, and
the Manager class has several methods that have Worker
parameters.
Now there’s a new kind of worker called SuperWorker, and their
behaviour and features are separate from regular Workers.
Oh dear …
D
e
p
e
n
d
e
n
c
y
 
i
n
v
e
r
s
i
o
n
 
p
r
i
n
c
i
p
l
e
(
e
x
a
m
p
l
e
 
f
r
o
m
 
D
e
p
e
n
d
e
n
c
y
 
I
n
v
e
r
s
i
o
n
P
r
i
n
c
i
p
l
e
 
o
n
 
O
O
D
e
s
i
g
n
)
D
e
p
e
n
d
e
n
c
y
 
i
n
v
e
r
s
i
o
n
 
p
r
i
n
c
i
p
l
e
(
e
x
a
m
p
l
e
 
f
r
o
m
 
D
e
p
e
n
d
e
n
c
y
 
I
n
v
e
r
s
i
o
n
P
r
i
n
c
i
p
l
e
 
o
n
 
O
O
D
e
s
i
g
n
)
To make Manager work with SuperWorker, we would need to
rewrite the code in Manager.
Solution: create an IWorker interface and have Manager use
it.
In this design, Manager does not know anything about
Worker, nor about SuperWorker. It can work with any
IWorker, the code in Manager does not need rewriting.
D
e
p
e
n
d
e
n
c
y
 
i
n
v
e
r
s
i
o
n
 
p
r
i
n
c
i
p
l
e
(
e
x
a
m
p
l
e
 
f
r
o
m
 
D
e
p
e
n
d
e
n
c
y
 
I
n
v
e
r
s
i
o
n
P
r
i
n
c
i
p
l
e
 
o
n
 
O
O
D
e
s
i
g
n
)
Slide Note

Both: Introductions

Embed
Share

Winter 2018's CSC207H Software Design course focuses on software design and development concepts, methods, and tools using Java. The course covers topics like version control, unit testing, refactoring, object-oriented design, design patterns, and more. Students are expected to spend 8-10 hours per week on the course, which serves as training for a software internship. The learning goals include object-oriented programming, unit testing, file handling, and understanding floating-point issues. The course structure consists of lectures, labs, midterm, assignments, projects, and a final exam.

  • Software Design
  • CSC207H
  • Java
  • Object-oriented Programming
  • Winter 2018

Uploaded on Sep 28, 2024 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CSC207H: Software Design CSC207 http://www.teach.cs.toronto.edu/~csc207h/winter/ Instructors: Lindsey Shorser, Jaisie Sin Email: csc20718s@cs.toronto.edu Winter 2018 An introduction to software design and development concepts, methods, and tools using a statically-typed object-oriented programming language such as Java. Topics from: version control, unit testing, refactoring, object-oriented design and development, design patterns, advanced IDE usage, regular expressions, and reflection. Representation of floating-point numbers and introduction to numerical computation. The Arts and Science Calendar 1

  2. CSC207H: Software Design You know the CSC108, CSC148, and (usually) the CSC165 material: lists, dictionaries, functions, classes, stacks, queues, trees, recursion, unit testing, logical notation and reasoning, the basics of computational complexity (big-Oh), and an approach to developing functions (the function design recipe). presumably want to do very well in this course. should expect to spend 8 10 hours a week on each of your courses (including lectures and labs). might think about this course as training for a software internship. 2

  3. CSC207H: Software Design Learning goal: Object-oriented programming in a statically-typed language Strong typing Lots of inheritance Unit testing File handling A memory model for Java Exception handling Floating-point issues

  4. CSC207H: Software Design Learning goal: Fundamental code development techniques used professionally How to think about and plan a large program How to analyze requirements How to safely refactor code Design patterns Version control (using git) Aspects of team dynamics An Integrated Development Environment (IntelliJ) 4

  5. CSC207H: Software Design How we re going to teach 2 lecture hours / week 10 (ish) 1-hour labs 1 45-minute midterm in week 5 2 individual assignments A two-phase project in the second half of the course 1 final exam 5

  6. CSC207H: Software Design Coursework Overview Work Weight Comment Labs (8) 8% 1% each, best 8 out of 10 A1 5% Individual A2 10% Individual Project: Phase 1 10% Group: team of four from the same lecture section Project: Phase 2 17% during lab time (bring your TCard) You must get 40% on the final exam to pass CSC108! Test 10% Final Exam 40% 6

  7. CSC207H: Software Design Resources Portal/Blackboard: Announcements, Grades Course website: Readings, Links www.teach.cs.toronto.edu/~csc207h/winter/ Discussion board Office hours BA3219: Tuesdays 10 am -12 pm, Wednesdays 2 4 pm Lectures and labs! Anonymous feedback (if you don t want to email us or post on the boards): please give us constructive suggestions! Help Centre in BA2230 every weekday 2-6 pm, except holidays PCRS and On-Line Tutorials (see course website) 7

  8. CSC207H: Software Design Java Reference Materials Course website (readings, lecture notes, links) This reference is particularly useful: http://docs.oracle.com/javase/tutorial/java/TOC.html Java PCRS This website does a nice job walking you through Java if the PCRS isn t enough: https://www.sololearn.com/Course/Java/ Email registration is required Have you heard about disposable email addresses? https://en.wikipedia.org/wiki/Disposable_email_address Here s a top-15 article about the topic: www.updateland.com/15-best-fake-email- address-generator-online/ 9

  9. CSC207H: Software Design 8 10 hours before the end of the day next Wednesday Attend lecture (2 hours) Attend lab next week (1 hour) Log into the Teaching Labs and run IntelliJ IDEA (1/2 hour) Install Git, Java, and IntelliJ on your own computer (1 hour) Work through Quest 1 on the PCRS and practice in IntelliJ (4 hours) Lab next week will involve Java code, so you should try to get through as much as you can before this Monday 10

  10. CSC207H: Software Design What does it mean to run a program? What is a program? A set of instructions for a computer to follow. To run a program, it must be translated from a high-level programming language to a low-level machine language whose instructions can be executed. Roughly, two flavours of translation: Interpretation Compilation 11

  11. CSC207H: Software Design Interpreted vs. Compiled Interpreted (like Python) Translate and execute one statement at a time Compiled (like C) Compile the entire program (once), then execute (any number of times) Hybrid (like Java) Compile to something intermediate (in Java, bytecode) The Java Virtual Machine (JVM) runs this intermediate code 12

  12. CSC207H: Software Design Compiling Java If using command line, you need to do this manually. First, compile using javac : jsin@laptop$ javac HelloWorld.java This produces file HelloWord.class : jsin@laptop$ ls HelloWorld.class HelloWorld.java Now, run the program using java : jsin@laptop$ java HelloWorld Hello world! Most modern IDEs offer to do this for you (IntelliJ does). But you should know what s happening under the hood! 13

  13. CSC207H: Software Design SOLID Principles of Object-Oriented Design How do we make decisions about what is better and what is worse design? Principles to aim for instead of rules. For example, there is no maximum number of class you should have in your program, nor a minimum. But if the number of classes violates a generally accepted principle, you should reconsider your class structure. The SOLID principles are useful and cover most major situations you are likely to encounter. 14

  14. CSC207H: Software Design Software design goals A major goal when programming is to write an easy- to-read, hard-to-break, maintainable, efficient program. Software design has you use a set of principles and techniques that help you do this. This lecture is an introduction to the tools and techniques we ll see in this course. We ll cover each of these in more detail later in the course. 15

  15. CSC207H: Software Design Fundamental OOP concepts Abstraction the process of distilling a concept to a set of essential characteristics. Encapsulation the process of binding together data with methods that manipulate that data, and hiding the internal representation. The result of applying abstraction and encapsulation is (often) a class with instance variables and methods that together model a concept from the real world. (Further reading: what s the difference between Abstraction, Encapsulation, and Information hiding?) Inheritance the concept that when a subclass is defined in terms another class, the features of that other class are inherited by the subclass. Polymorphism ( many forms ) the ability of an expression (such as a method call) to be applied to objects of different types. 16

  16. CSC207H: Software Design Fundamental OOD goals: low coupling, high cohesion Coupling how much a class is directly linked to another class. High coupling means that changes to one class may lead to changes in several other classes. Low coupling is, therefore a desired goal. Cohesion how much the features of a class belong together. Low cohesion means that methods in a class operate on unrelated tasks. This means the class does jobs that are unrelated. High cohesion means that the methods have strongly-related functionality. 17

  17. CSC207H: Software Design Fundamental OOD principles SOLID: five basic principles of object-oriented (Developed by Robert C. Martin, affectionately known as Uncle Bob .) Single responsibility principle Open/closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle 18

  18. Single Responsibility Principle Every class should have a single responsibility. Another way to view this is that a class should only have one reason to change. But who causes the change? This principle is about people. When you write a software module, you want to make sure that when changes are requested, those changes can only originate from a single person, or rather, a single tightly coupled group of people representing a single narrowly defined business function. You want to isolate your modules from the complexities of the organization as a whole, and design your systems such that each module is responsible (responds to) the needs of just that one business function. [Uncle Bob, The Single Responsibility Principle]

  19. Open/Closed Principle (simplified) Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification. Add new features not by modifying the original class, but rather by extending it and adding new behaviours, or by adding plugin capabilities. I ve heard it said that the OCP is wrong, unworkable, impractical, and not for real programmers with real work to do. The rise of plugin architectures makes it plain that these views are utter nonsense. On the contrary, a strong plugin architecture is likely to be the most important aspect of future software systems. [Uncle Bob, The Open Closed Principle]

  20. Open/Closed Principle (simplified) An example, using inheritance: area calculates the area of all Rectangles in the input. What if we need to add more shapes?

  21. Open/Closed Principle (simplified) With this design, we can add any number of shapes (open for extension) and we don't need to re-write the AreaCalculator class (closed for modification).

  22. Liskov Substitution Principle (simplified) If S is a subtype of T, then objects of type S may be substituted for objects of type T, without altering any of the desired properties of the program. S is a subtype of T ? In Java, S is a child class of T, or S implements interface T. For example, if C is a child class of P, then we should be able to substitute C for P in our code without breaking it.

  23. Liskov Substitution Principle (simplified) A classic example of breaking this principle:

  24. Liskov Substitution Principle (simplified) In OO programming and design, unlike in math, it is not the case that a Square is a Rectangle! This is because a Rectangle has more behaviours than a Square, not less. The LSP is related to the Open/Closed principle: the subclasses should only extend (add behaviours), not modify or remove them.

  25. Interface Segregation Principle Here, interface means the public methods in a class. (In Java, these are often specified using a Java interface, which you ll learn about soon.) Context: a class that provides a service for other client programmers usually requires that the clients write code that has a particular set of features. The service provider says your code needs to have this interface . No client should be forced to implement irrelevant methods of an interface. Better to have lots of small, specific interfaces than fewer larger ones: easier to extend and modify the design. (Uh oh: The interface keyword is harmful. [Uncle Bob, 'Interface' Considered Harmful])

  26. Dependency inversion principle When building a complex system, programmers are often tempted to define low-level classes first and then build higher-level classes that use the low-level classes directly. But this approach is not flexible! What if we need to replace a low-level class? The logic in the high-level class will need to be replaced an indication of high coupling. To avoid such problems, we introduce an abstraction layer between low-level classes and high-level classes.

  27. Dependency inversion principle Goal: You want to decouple your system so that you can change individual pieces without having to change anything more than the individual piece. Two aspects to the dependency inversion principle: High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions.

  28. Dependency inversion principle (example from Dependency Inversion Principle on OODesign) Example: you have a large system, and part of it has Managers manage Workers. Let s say that the company is restructuring and introducing new kinds of workers, and wants the code updated to reflect this. Your code current has a Manager class and a Worker class, and the Manager class has several methods that have Worker parameters. Now there s a new kind of worker called SuperWorker, and their behaviour and features are separate from regular Workers. Oh dear

  29. Dependency inversion principle (example from Dependency Inversion Principle on OODesign) To make Manager work with SuperWorker, we would need to rewrite the code in Manager. Solution: create an IWorker interface and have Manager use it.

  30. Dependency inversion principle (example from Dependency Inversion Principle on OODesign) In this design, Manager does not know anything about Worker, nor about SuperWorker. It can work with any IWorker, the code in Manager does not need rewriting.

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#