Download presentation
Presentation is loading. Please wait.
Published bySydney Bradley Modified over 9 years ago
1
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced Information Systems CERN – Geneva, Switzerland
2
CERN GS-AIS Design Patterns Someone has already solved your problem! A design pattern is a general repeatable solution to a commonly occurring problem in software design
3
CERN GS-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
4
CERN GS-AIS Problem Can my SquareObject fit the RoundHole?
5
CERN GS-AIS Adapter Pattern Allows objects satisfying one interface to be used where another type of interface is expected The SquareObject should be used where we expect a RoundObject
6
CERN GS-AIS Adapter Pattern in Java (1) 1. Create an interface 2. Refer to it in your code 3. Adapt the behaviour
7
CERN GS-AIS Adapter Pattern in Java (2)
8
CERN GS-AIS Adapter Pattern in Groovy (1) No need of an interface, as long as the object has a radius property
9
CERN GS-AIS Adapter Pattern in Groovy (2) We can even inherit, as not type is defined in objectFits
10
CERN GS-AIS Adapter Pattern in Groovy (3) If an interface were to exist We could create a closure And use it like this
11
CERN GS-AIS Adapter Pattern Two flavours of the pattern: –The delegation flavour –The inheritance flavour Not a lot of difference between Java and Groovy
12
CERN GS-AIS Problem What if Bernard get fired but stays in the database ?
13
CERN GS-AIS Null Object Pattern The 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
14
CERN GS-AIS Null Object Pattern in Java NO! YES!
15
CERN GS-AIS Null Object Pattern in Groovy
16
CERN GS-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
17
CERN GS-AIS Problem Let’s extend my simple Logger behaviour Add time stamp Add upper message Add both
18
CERN GS-AIS Decorator Pattern Attach additional responsibilities to an object dynamically Provide a flexible alternative to subclassing for extending functionality Doesn’t modify source code Decorators can be combined in flexible ways Avoid class explosion
19
CERN GS-AIS Decorator Pattern in Java
20
CERN GS-AIS Decorator Pattern in Groovy (1) Use @Delegate annotation to delegate method calls
21
CERN GS-AIS Decorator Pattern in Groovy (2) A touch of dynamic behavior All String arguments will be lowered
22
CERN GS-AIS Decorator Pattern in Groovy (3) Is my logger slow? Let’s trace it
23
CERN GS-AIS Decorator Pattern in Groovy (4)
24
CERN GS-AIS Decorator or Observer Pattern? Using interceptor as decorator looks like the observer pattern Except 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-Interceptionhttp://groovy.codehaus.org/JN3515-Interception
25
CERN GS-AIS Problem I want to reuse my algorithm with two other encodings
26
CERN GS-AIS Template Method Pattern Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure
27
CERN GS-AIS Template Method Pattern in Java
28
CERN GS-AIS Template Method Pattern in Groovy
29
CERN GS-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
30
CERN GS-AIS Strategy Pattern Define a family of algorithms, encapsulate each one, and make them interchangeable Strategy lets the algorithm vary independently from the clients that use it
31
CERN GS-AIS Strategy Pattern in Java
32
CERN GS-AIS Strategy Pattern in Java
33
CERN GS-AIS Strategy Pattern in Groovy Let’s use some closure
34
CERN GS-AIS Strategy Pattern This pattern is really used to have first class functions Groovy already has first class functions
35
CERN GS-AIS Is-A vs Has-A We 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
36
CERN GS-AIS Grails Framework A 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
37
CERN GS-AIS Conclusion All Design Patterns can be coded the same way in Groovy as in Java Some Patterns have been designed because of restriction of the language Groovy, allowing first class function and weak typing, highlights similarities of different patterns
38
CERN GS-AIS Useful urls Design patterns in Groovy –http://groovy.codehaus.org/Design+Patterns+with+Groovyhttp://groovy.codehaus.org/Design+Patterns+with+Groovy Design patterns in Java –http://sourcemaking.com/design_patternshttp://sourcemaking.com/design_patterns
39
CERN GS-AIS Thank You
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.