CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced Information Systems CERN – Geneva, Switzerland Design Patterns Someone has already solved your problem! A design pattern is a general repeatable solution to a commonly occurring problem in software design 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 – – – – – – – – CERN GS-AIS 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 Problem Can my SquareObject fit the RoundHole? 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 CERN GS-AIS Adapter Pattern in Java (1) 1. Create an interface 2. Refer to it in your code 3. Adapt the behaviour CERN GS-AIS Adapter Pattern in Java (2) CERN GS-AIS Adapter Pattern in Groovy (1) No need of an interface, as long as the object has a radius property CERN GS-AIS Adapter Pattern in Groovy (2) We can even inherit, as not type is defined in objectFits CERN GS-AIS Adapter Pattern in Groovy (3) If an interface were to exist We could create a closure And use it like this 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 CERN GS-AIS Problem What if Bernard get fired but stays in the database ? 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 CERN GS-AIS Null Object Pattern in Java NO! YES! CERN GS-AIS Null Object Pattern in Groovy 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 CERN GS-AIS Problem Let’s extend my simple Logger behaviour Add time stamp Add upper message Add both 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 CERN GS-AIS Decorator Pattern in Java CERN GS-AIS Decorator Pattern in Groovy (1) Use @Delegate annotation to delegate method calls CERN GS-AIS Decorator Pattern in Groovy (2) A touch of dynamic behavior All String arguments will be lowered CERN GS-AIS Decorator Pattern in Groovy (3) Is my logger slow? Let’s trace it CERN GS-AIS Decorator Pattern in Groovy (4) 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-Interception CERN GS-AIS Problem I want to reuse my algorithm with two other encodings CERN GS-AIS Template Method Pattern Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure CERN GS-AIS Template Method Pattern in Java CERN GS-AIS Template Method Pattern in Groovy 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 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 CERN GS-AIS Strategy Pattern in Java CERN GS-AIS Strategy Pattern in Java CERN GS-AIS Strategy Pattern in Groovy Let’s use some closure CERN GS-AIS Strategy Pattern This pattern is really used to have first class functions Groovy already has first class functions 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 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 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 CERN GS-AIS Useful urls Design patterns in Groovy – http://groovy.codehaus.org/Design+Patterns+with+Groovy Design patterns in Java – http://sourcemaking.com/design_patterns CERN GS-AIS Thank You CERN GS-AIS