CERN – European Organization for Nuclear ResearchGS Department – Administrative Information Services
Design Patterns in Groovy
Nicolas DécrevelAdvanced Information SystemsCERN – Geneva, Switzerland
CERNGS-AIS
Design PatternsSomeone has already solved your
problem!A design pattern is a general repeatable
solution to a commonly occurring problem in software design
CERNGS-AIS
Design Patterns Creational patterns
– Abstract Factory Design Pattern– Builder Design Pattern– Factory Method Design Pattern– Object Pool Design Pattern– Prototype Design Pattern– Singleton Design Pattern
Structural patterns– Adapter Design Pattern– Bridge Design Pattern– Composite Design Pattern– Decorator Design Pattern– Facade Design Pattern– Flyweight Design Pattern– Private Class Data– Proxy Design Pattern
Behavioral patterns– Chain of Responsibility– Command Design Pattern– Interpreter Design Pattern– Iterator Design Pattern– Mediator Design Pattern– Memento Design Pattern– Null Object Design Pattern– Observer Design Pattern– State Design Pattern– Strategy Design Pattern– Template Method Design Pattern– Visitor Design Pattern
CERNGS-AIS
Problem
Can my SquareObject fit the RoundHole?
CERNGS-AIS
Adapter PatternAllows objects satisfying one interface
to be used where another type of interface is expected
The SquareObject should be used where we expect a RoundObject
CERNGS-AIS
Adapter Pattern in Java (1)
1. Create an interface
2. Refer to it in your code
3. Adapt the behaviour
CERNGS-AIS
Adapter Pattern in Java (2)
CERNGS-AIS
Adapter Pattern in Groovy (1)
No need of an interface, as long as the object has a radius property
CERNGS-AIS
Adapter Pattern in Groovy (2)
We can even inherit, as not type is defined in objectFits
CERNGS-AIS
Adapter Pattern in Groovy (3)If an interface were to exist
We could create a closure
And use it like this
CERNGS-AIS
Adapter PatternTwo flavours of the pattern:
– The delegation flavour– The inheritance flavour
Not a lot of difference between Java and Groovy
CERNGS-AIS
Problem
What if Bernard get fired but stays in the database ?
CERNGS-AIS
Null Object PatternThe intent of a Null Object is to
encapsulate the absence of an object by providing a substitutable alternative that offers suitable default do nothing behaviour
CERNGS-AIS
Null Object Pattern in JavaNO!
YES!
CERNGS-AIS
Null Object Pattern in Groovy
CERNGS-AIS
Null Object Pattern in Groovy?Due to syntax simplification, the Null
Object Pattern may be less useful in Groovy than in Java for simple cases
It will still be useful when the object to nullify is complex and has multiple collaboration with the client
CERNGS-AIS
ProblemLet’s extend my simple Logger behaviour
Add time stamp
Add upper message
Add both
CERNGS-AIS
Decorator PatternAttach additional responsibilities to an
object dynamically Provide a flexible alternative to
subclassing for extending functionalityDoesn’t modify source codeDecorators can be combined in flexible
waysAvoid class explosion
CERNGS-AIS
Decorator Pattern in Java
CERNGS-AIS
Decorator Pattern in Groovy (1)
Use @Delegate annotation to delegate method calls
CERNGS-AIS
Decorator Pattern in Groovy (2)
A touch of dynamic behavior
All String arguments will be lowered
CERNGS-AIS
Decorator Pattern in Groovy (3)
Is my logger slow?
Let’s trace it
CERNGS-AIS
Decorator Pattern in Groovy (4)
CERNGS-AIS
Decorator or Observer Pattern?Using interceptor as decorator looks like
the observer patternExcept that the observation code is part
of its own class (no need to change the observed class)
You can use a MultiInterceptorProxyMetaClass to pass multiple observer– http://groovy.codehaus.org/JN3515-Interception
CERNGS-AIS
Problem
I want to reuse my algorithm with two other encodings
CERNGS-AIS
Template Method PatternTemplate Method lets subclasses
redefine certain steps of an algorithm without changing the algorithm’s structure
CERNGS-AIS
Template Method Pattern in Java
CERNGS-AIS
Template Method Pattern in Groovy
CERNGS-AIS
Template Method Pattern Problem We still have to create two classes
– ReversingRot13 extends TwoPhaseEncryption– Base64Reversing extends TwoPhaseEncryption
What if we want to inverse the execution of the phases? – We have to create two other classes
• Rot13Reversing extends TwoPhaseEncryption• ReversionBase64 extends TwoPhaseEncryption
What if we want to use Base64 with Rot13 ?– new classes, etc, etc
Is there an other way to write TwoPhaseEncryption?– Extract the parts which are changing : the encryptions
CERNGS-AIS
Strategy PatternDefine a family of algorithms,
encapsulate each one, and make them interchangeable
Strategy lets the algorithm vary independently from the clients that use it
CERNGS-AIS
Strategy Pattern in Java
CERNGS-AIS
Strategy Pattern in Java
CERNGS-AIS
Strategy Pattern in GroovyLet’s use some closure
CERNGS-AIS
Strategy PatternThis pattern is really used to have
first class functions Groovy already has first class functions
CERNGS-AIS
Is-A vs Has-AWe have seen two ways of implementing
a solution using either inheritance (Is-A) or delegation (Has-A)
One should prefer the Has-A version of an algorithm which will be more resistant to changes
Remember the @Delegate
CERNGS-AIS
Grails FrameworkA lot of the Design Patterns are getting
useless with usage of complex Frameworks like Spring or Grails
These Frameworks already implement a lot of these Design Patterns to simplify coding
Examples: Template, Strategy, Proxy, Builder, Abstract Factory, Singleton, Observer, etc
CERNGS-AIS
ConclusionAll Design Patterns can be coded the
same way in Groovy as in JavaSome Patterns have been designed
because of restriction of the languageGroovy, allowing first class function and
weak typing, highlights similarities of different patterns
CERNGS-AIS
Useful urls
Design patterns in Groovy– http://groovy.codehaus.org/Design+Patterns+with+Groovy
Design patterns in Java– http://sourcemaking.com/design_patterns
CERNGS-AIS
Thank You
Top Related