The modern view on
implementation of
classic design patterns in
Java
Mikalai Alimenkou
http://xpinjection.com
@xpinjection
Disclaimer
This is only my
personal experience
Familiar with design patterns?
Holy bible for developers 
Java have long/slow roadmap
You could follow code samples
https://github.com/xpinjection/design-patterns
#1: Singleton
DI fight against Singleton
This is common anti-
pattern now
Isolate existing
Singletons from new
code with DI
DI containers ‘killed’
Singleton pattern
#2: Builder
‘Storing’ VS ‘runtime’
style builders
Builder is the most
common pattern
Java is still bad in
handling properties
#3: Prototype
DI container handle
prototypes in their
own way
Clone method in
Object is confusing
Useful for domain
model with DDD style
#4: Factory Method
Use basic functional
interfaces if possible
Doesn’t work for
abstract factory
Constructor is also a
function
#5: Adapter
Functional interfaces
are interchangeable
Doesn’t work for
complex interfaces
Use basic functional
interfaces if possible
#6: Proxy
May be done with
composite functions
Inline lambda
wrapping is also
available
Main usage with AOP
and DI containers
Libs for manual proxy creation
• java.lang.reflect.Proxy
• Byte Buddy (http://bytebuddy.net)
• cglib (https://github.com/cglib/cglib)
• javassist (https://github.com/jboss-
javassist/javassist)
#7: Template Method
Utilize basic functional
interfaces
Inline needed parts
in tests and simple
clients
Use composition
instead of inheritance
#8: Command
Collect commands in
collection or combine
them
Doesn’t work with
complex commands
(with ‘undo’)
Dude, this is just a
plain lambda!
#9: Chain of Responsibility
Combine consumers
in the chain
Doesn’t work for
“workflow” handlers
May be use typed
functions pipeline?
#10: Strategy
Inline strategy in tests
and other light clients
Store strategies in
single holder as
static methods
Dude, this is just a
plain lambda again!
#11: Iterator
Use Streams
everywhere
Create own streams
with Spliterator
Forget about manual
iterations
#12: Decorator
Wrap function with
lambda
Combine functions
to provide additional
behavior
Use the Proxy
approaches
#13: Observer
Pass methods as
listeners
Observer and
Listener are
functional interfaces
Inline listener logic
in lambda
#14: Visitor
Use general
implementation
Try to use default
methods where
applicable
Very rarely used design
pattern
And remember…
@xpinjection
http://xpinjection.com
mikalai.alimenkou@xpinjection.com

The modern view on implementation of classic design patterns in Java