Java 8 introduces new toolkit which enables new model of thinking for java developers – functional. It encourage us to rethink traditional approaches of Object Oriented programming which were commonly accepted for decades. In this talk we will review many Gang of Four design patterns showing how they can be re-implemented in more concise and often more simple and elegant ways. Also we will look beyond that and will see which new design patterns and programming technics functional thinking and new java toolkit bring for us.
2. www.luxoft.com
About me:
• Lead Software Engineer in Luxoft
• More then 6 years experience in Java
• Agile and Lean practicioner
• Pragmatic and product oriented engineer
• Still learning and learn
Email: hunter041@gmail.com
Twitter: @apashinskiy_cv
Alexander Pashinskiy
10. www.luxoft.com
• OOP (Resource acquisition is initialization, Servant,
DI, Pool Objects, Null object …)
• FP (Functions, Monads, Lenz, Curring, …)
• Concurrency (Double-checked locking, Read-write
lock, Thread-specific storage, …)
• Domain Specific (security, money, …)
• and many, many more
11. www.luxoft.com
• Problem - Solution pairs
• Similar (or same) solution - different intention (Strategy,
State …)
• inheritance - composition game (OOP)
• Adding level of indirection
• Almost always trade-off
Design Patterns:
12. www.luxoft.com
• 1994 year
• For C++
• OOP
Two main principles:
• "Program to an interface, not an implementation."
• "Favor object composition over class inheritance."
Sad:(
Some GoF patterns do not stick to this principles
16. www.luxoft.com
Template method
Type: behavioral
Definition: Define the skeleton of an
algorithm in an operation, deferring
some steps to subclasses. Template
Method lets subclasses redefine
certain steps of an algorithm without
changing the algorithm's structure.
17. www.luxoft.com
• Not only for data
• BLSP
• Violates “Single Responsibility” and encapsulation
• High Coupling
• Future behavior and frameworks inhereted
• Broken “protected” in Java
• Big hierarchy produces hell
Inheritance
18. www.luxoft.com
Template method
• Inheritance is evil
• Use composition
• Part of an algorithm can be passed as a function
• Utilize basic functional interfaces
• Use function composition if possible
27. www.luxoft.com
Chain of Responsibility
Type: behavioral
Definition: Avoid coupling the sender
of a request to its receiver by giving
more than one object a chance to
handle the request. Chain the receiving
objects and pass the request along the
chain until an object handles it.
32. www.luxoft.com
Adapter
• Utilise basic functional interfaces
• Functional interfaces are interchangable
• For different numbers of parameters - partially applied
function
35. www.luxoft.com
Partial application
• allows to set some (not all) parameters
• let the other parameters to be set later
• partially applied function can be reused and
composed
• Powerful decoupling tool
55. www.luxoft.com
Builder
• classic runtime builder
• classic reusable builder (a lot of boiler-plate)
• or just use a consumer if applicable
- no boiler-plate
- reusable (function composition)
- chaining DSL
- consumer has internal control on objet
62. www.luxoft.com
Monad
Represents (2 and more) states as a unified
value in order to compose transformations
[User | Error] -> validate1 -> [User | Error] -> validate2 -> [User | Error ] -> get
User Error
User
of
77. www.luxoft.com
Visitor
interface Visitor<T> {
T visit(Square element);
T visit(Circle element);
T visit(Rectangle element);
}
def visit(e: Element): T = e match {
case Square => do1();
case Circle => do2();
case Rectangle => do3();
}
79. www.luxoft.com
Paradigm shift:
• object centric -> function centric
• function composition reduce complexity
• data to code -> code to data
• external -> internal (mechanic incapsulation)
80. www.luxoft.com
• functions
• higher order function
• function composition
• partially applied functions
• monads
• pattern matching
• …
New Designers Toolbox
82. www.luxoft.com
“In the book we only tell when to apply pattern, but we
never talk about when to remove a pattern. Removing a
pattern can simplify a system and a simple solution
should almost always win. Coming up with a simple
solutions is the real challenge.”
E.Gamma
83. www.luxoft.com
• no silver bullet
• OOP is alive
• GoF “Design Patterns” is still valid
• you just have additional design tools
• almost always trade-off
• use your mind
And remember: