Académique Documents
Professionnel Documents
Culture Documents
Design principles
presenter: Sergey Karpushin
Agenda
Justification
DRY
SLAP
Single Responsibility
Open/Closed
Liskov substitution
Interface segregation
Dependency inversion
2
Justification 1/4
Fixing tons of
bugs
Im scared! This
will break
something
I need to expand
consciousness to have
a clue
Justification 2/4
Fixing tons of
bugs
Im scared! This
will break
something
I need to expand
consciousness to have
a clue
Justification 3/4
Fixing tons of
bugs
Im scared! This
will break
something
I need to expand
consciousness to have
a clue
Justification 4/4
OOD & other related principles
Takes time to understand and feel for
the first time
Relatively easy to apply later
Disclaimer:
NAMING CONVENTION
Naming convention
Design principles are common for all Object-Oriented
languages
Java naming conventions used within this
presentation
Name example for interfaces, DTOs and simple
classes:
UserService
Name example for classes which implements
interfaces
UserServiceImpl
All variable and parameter names example
camelCaseName
8
DRY + SLAP +
SOLID
DONT REPEAT YOURSELF
DRY 1/15
Do not Repeat Yourself
Reuse your code, do not duplicate it
Assign clear names
Choose right location
10
11
12
13
14
stm t.execute(
"SELECT nam e, em ailFRO M users W H ERE id = ?",
id);
15
stm t.execute(
"SELECT last_nam e,em ailFRO M users W H ERE id = ?",
id);
16
getArticleN am eById(id);
stm t.execute(
"S ELEC T n am e FR O M articles W H ER E id = ?",
id);
17
18
19
?!?!?!?
getO bjectN am eById(table, id);
?!?!?!?
?!?!?!?
stm t.execute(
"SELECT nam e FRO M ? W H ERE id = ?",
id);
?!?!?!?
20
21
23
24
DRY + SLAP +
SOLID
SINGLE LAYER OF
ABSTRACTION PRINCIPLE
SLAP 1/3
Single Layer of Abstraction Principle
for methods
Have common things with DRY, SRP, ISP
Basic idea is:
Do not write endless methods which logic is
hard to follow.
All instructions for each method should
operate on same level of abstraction and
related to one task
26
27
28
SOLID
OPEN-CLOSED PRINCIPLE
Open-Closed Principle
Effects
Increased stability existing code
(almost) never changes
Increased modularity, but many small
classes
30
Open-Closed Principle
31
Open-Closed Principle
32
Open-Closed Principle
33
Open-Closed Principle
34
Open-Closed Principle
35
Open-Closed Principle
36
Open-Closed Principle
Open-Closed Principle
38
Open-Closed Principle
39
Open-Closed Principle
40
SOLID
SINGLE RESPONSIBILITY
PRINCIPLE
48
SOLID
INTERFACE SEGREGATION
PRINCIPLE
2: SpamDetector depends on
QuickMessage interface, but there is no
need to access author, why we need it
here?!
Q: What will happen if you need to detect
spam in file or, lets say, email?
52
53
54
SOLID
DEPENDENCY INVERSION
PRINCIPLE
DI 1/4 - idea
Dependency Inversion
Code to abstraction, not to implementation
Objects that use other objects, shouldnt
create latter ones
Profits
Unit testing is possible
Easy change of concrete implementation
Each module depends on minimum and
well-defined outer abstrations
56
DI 2/4 i.e.
public interface H tm lParser {
H tm lD ocum ent parseU rl(String url);
}
public class D om BasedH tm lParser im plem ents H tm lParser {
public H tm lD ocum ent parseU rl(String url) {
// do the best
}
}
public class Craw ler {
public void saveH tm lD ocum ent() {
D om B ased H tm lParser p arser = n ew D om B ased H tm lP arser();
H tm lD ocum ent docum ent = parser.parseU rl("http://.....");
// do craw l
}
}
DI 3/4 i.e.
public class Craw ler {
p rivate D om B ased H tm lP arser d om B ased H tm lP arser;
p u b lic void C raw ler(D om B ased H tm lP arser d om B ased H tm lP arser) {
this.dom BasedH tm lParser = dom BasedH tm lParser;
}
public void saveH tm lD ocum ent() {
H tm lD ocu m en t d ocu m en t = d om B ased H tm lP arser.p arseU rl("h ttp ://.....");
// do craw l
}
}
DI 4/4 i.e.
public class Craw ler {
p rivate H tm lP arser h tm lParser;
p u b lic void C raw ler(H tm lParser h tm lParser) {
this.h tm lP arser = h tm lParser;
}
public void saveH tm lD ocum ent() {
H tm lD ocu m en t d ocu m en t = h tm lParser.p arseU rl("h ttp ://.....");
// do craw l
}
}
SOLID
LISKOV SUBSTITUTION
PRINCIPLE
Friendly principles
DI
SRP, ISP
61
63
64
65
66
67
68
p.s. Please,
dont write BS
which makes my
eyes bleeding