Documente Academic
Documente Profesional
Documente Cultură
Introduc)on to
OO
Design
Pa3erns
Victor
Rentea
December
9
&
16,
2013
Object-Oriented
Programming
Course
"Politehnica"
University
of
Bucharest
Part
1
Copyright IBM Corporation 2013
Contents
Part II
Part I
Agenda
IntroducEon
Principles
of
Good
OO
Design
CreaEonal
PaJerns
Structural
PaJerns
Behavioral
PaJerns
AnE-paJerns
Test
Test
Introduction to OO Design
Coupling
=
the
degree
to
which
components
depend
on
each
other
- Loose
Coupling:
changes
dont
ripple
throughout
the
code
Introduction to OO Design
Shared
Vocabulary
-
Introduction to OO Design
GoF
(USA)
(USA, IBM)
(Swiss, IBM)
Contents Part 1
Agenda
IntroducEon
Principles
of
Good
OO
Design
CreaEonal
PaJerns
Structural
PaJerns
Test
Introduction to OO Design
CHANGE
Design
for
change
7
Program
to
Abstrac)ons
=depend
on
abstrac*ons
rather
than
implementa*ons
OtherClass
MyClass
Favor
+reference
Composi)on
over
Inheritance
OtherClass
MyClass
No mulEple extends
10
SOLID
Principles
Single
Responsibility
~high Cohesion
Open/Closed
=
lower
the
impact
of
adding
new
funcEonality
on
exisEng
code
Liskov
SubsEtuEon
Interface
SegregaEon
=
keep
interfaces
as
minimal
as
possible
(dont
tell
if
not
asked)
Dependency
Inversion
domain
logic
from
low-level
components
=
decouple
hyour
igh-level
modules
11
Overengineering
PiWall
When
to
decouple
varying
parts
of
the
system
?
Premature
Encapsula)on
is
the
Root
of
all
Evil
Adam
Bien
Upfront
-
Later
on
-
12
Designing is a Tradeo
AbstracEons
Simplicity
13
Contents Part 1
Agenda
IntroducEon
Principles
of
Good
OO
Design
Crea)onal
Pa3erns
Structural
PaJerns
Test
14
Creational Patterns
Singleton
=have
only
one
instance
of
a
class
in
your
applica*on,
AlternaEve:
-
15
Creational Patterns
Fluent
Builder
public
class
Customer
{
private
String
name;
...
public
class
CustomerBuilder
{
public
static
class
Builder
{
private
Customer
customer=new
Customer();
private
Customer
customer=new
Customer();
public
Builder
withName(String
name)
{
customer.setName(name);
return
this;
}
public
Builder
withLabel(String
label)
{
Customer
customer
=
new
Customer();
customer.getLabels().add(label);
customer.setName("John
Doe");
return
this;
List<String>
labels
=
new
ArrayList<>();
}
...
labels.add("Label1");
public
Customer
build()
{
customer.setLabels(labels);
return
customer;
Address
address
=
new
Address();
}
adress.setStreetName("Viorele");
}
customer.setAddress(address);
}
16
Creational Patterns
Abstract
Factory
=dene
an
interface
for
crea*ng
families
of
objects
but
let
Factorys
realiza*ons
choose
the
realiza*ons
to
instan*ate
-
interface
ProductA
+createProductA()
+createProductB()
ConcreteFactory1
ConcreteFactory2
ConcreteFactory3
+createProductA()
+createProductA()
+createProductA()
+createProductB()
+createProductB()
+createProductB()
return
new
ConcreteProduct1A();
17
creates
ConcreteProduct1A
ConcreteProduct1A
ConcreteProduct1A
Creational Patterns
Summary
Singleton
Guarantees
at
most
1
instance
of
a
class
per
applicaEon
-
by
which
you
can
share
data
throughout
the
enEre
applicaEon
-
Fluent
Builder
-
Abstract
Factory
Lets
you
create
families
of
related
objects,
-
that
can
then
work
together
-
18
Contents Part 1
Agenda
IntroducEon
Principles
of
Good
OO
Design
CreaEonal
PaJerns
Structural
Pa3erns
Test
&
ExplanaEons
19
Structural Patterns
Wrapper
Adapter
AnE-CorrupEon
Layer
-
+opera:on()
@Override
public
String
operation(Date
date)
{
String
s
=
//
format
date
as
string
int
result
=
adaptee.externalOperation(s);
String
adaptedResult
=
//
adapt
result
return
adaptedResult;
}
public
int
externalOperation(String
dateStr)
Adapter
-adaptee
+operaEon()
20
ExternalClass
delegate
+externalOperaEon()
Copyright IBM Corporation 2013
Structural Patterns
Proxy
=placeholder
intermedia*ng
interac*ons
with
an
object
Remote
invocaEons
(e.g.
web
service
client
proxy/stub)
- AOP
uses
it
for
cross-cuzng
concerns
like
Access
Control,
TransacEons,
etc
In
containers
like
EJB
or
Spring,
injected
references
are
Dynamic
Proxy
-
interface
Subject
+request()
actually
uses
Can
be
created
at
run-Eme
with
java.lang.reflect.Proxy
Proxy
+request()
21
represents
RealSubject
delegate
HTTP
+request()
Structural Patterns
Decorator
=aAach
addi*onal
responsibili*es
to
objects
methods
-
Proxy
interface
Subject
+request()
actually
uses
Decorator
-delegate
Proxy
+request()
+request()
22
RealSubject
delegate
+request()
Structural Patterns
Adapt an
interface/contract
Proxy
Intermediate
interactions
Decorator
Attach
responsibilities
23
Client-needed
Invoked Object
(Subject)
(local call)
Anti-Corruption
Invoked Object
(Subject)
(local call)
Dynamic Proxy
24
Cnd vreau s
, folosesc
, iar n cod
13 minute
Singleton
Fluent
Builder
Factory
Adapter
Proxy
2 (Chair e o interfa)
j Decorator 6
Program
to
Abstrac)ons
compoziie,
nu
motenire
Single
Responsibility
Interface
Segrega)on
Dependency
Inversion
Cnd vreau s
Fluent
Builder
Factory
Proxy
2 (Chair e o interfa)
Program
to
mi
protejez
codul
de
schimbri
n
implem.
g
Abstrac)ons
sau
s
pot
alege
implementarea
la
run-Eme
compoziie,
nu
motenire
Single
h
Responsibility
7
scriu
clase
care
se
modic
dintr-un
singur
moEv
Interface
Segrega)on
Dependency
Inversion
Cnd vreau s
, folosesc
, iar n cod
Adapter
Single
Responsibility
Principle
Decorator
Factory
Fluent Builder
Interface
Segrega)on
Principle
compoziie,
nu
motenire
Proxy
Dependency
Inversion
Principle
Program
to
Abstrac)ons
Principle
What we covered
Part II
Part I
Summary
28
Introduc)on
Principles
of
Good
OO
Design
Crea)onal
Pa3erns
Structural
Pa3erns
Behavioral
PaJerns
AnE-paJerns
References
Head
First:
Design
Pa3erns
EJB
3
JPA
Spring
Java
EE
Design
PaJer.
Java
EE6
XML,
XSD
&
JAXB
RESTful
WS
R4
SOAP
WS
Servlets
&
JSP
HTML5/CSS3
Mobile
Apps
JSF
and
many
more