410 likes | 618 Views
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. Design Patterns.
E N D
Design Patterns in Groovy Nicolas DécrevelAdvanced Information Systems CERN – Geneva, Switzerland
Design Patterns • Someone has alreadysolvedyourproblem! • A design pattern is a general repeatable solution to a commonly occurring problem in software design
Design Patterns • 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 • 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
Problem Can mySquareObject fit the RoundHole?
Adapter Pattern • Allows objects satisfying one interface to be used where another type of interface is expected • The SquareObjectshouldbeusedwhereweexpect a RoundObject
Adapter Pattern in Java (1) 1. Create an interface 2. Refer to it in your code 3. Adapt the behaviour
Adapter Pattern in Groovy (1) No need of an interface, as long as the object has a radius property
Adapter Pattern in Groovy (2) Wecaneveninherit, as not type isdefinedin objectFits
Adapter Pattern in Groovy (3) If an interface were to exist Wecouldcreate a closure And use itlikethis
Adapter Pattern • Twoflavours of the pattern: • The delegationflavour • The inheritanceflavour • Not a lot of differencebetween Java and Groovy
Problem What if Bernard getfired but stays in the database ?
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
Null Object Pattern in Java NO! YES!
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
Problem Let’sextendmy simple Loggerbehaviour Addtime stamp Addupper message Addboth
Decorator Pattern • Attach additional responsibilities to an object dynamically • Provide a flexible alternative to subclassing for extending functionality • Doesn’tmodify source code • Decoratorscanbecombined in flexible ways • Avoid class explosion
Decorator Pattern in Groovy (1) Use @Delegate annotation to delegatemethod calls
Decorator Pattern in Groovy (2) A touch of dynamicbehavior All String arguments willbelowered
Decorator Pattern in Groovy (3) Is mylogger slow? Let’s trace it
Decorator or Observer Pattern? • Usinginterceptor as decorator looks like the observer pattern • Exceptthat the observation code is part of itsown class (no need to change the observed class) • You can use a MultiInterceptorProxyMetaClassto pass multiple observer • http://groovy.codehaus.org/JN3515-Interception
Problem I want to reusemyalgorithmwithtwootherencodings
Template Method Pattern • Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure
Template Method Pattern Problem • Westill have to createtwo classes • ReversingRot13 extendsTwoPhaseEncryption • Base64Reversing extendsTwoPhaseEncryption • What if wewant to inverse the execution of the phases? • We have to createtwoother classes • Rot13Reversing extendsTwoPhaseEncryption • ReversionBase64 extendsTwoPhaseEncryption • What if wewant to use Base64 with Rot13 ? • new classes, etc, etc • Is there an otherway to writeTwoPhaseEncryption? • Extract the parts which are changing: the encryptions
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
Strategy Pattern in Groovy Let’s use someclosure
Strategy Pattern • This pattern is really used to have first class functions • Groovy already has first class functions
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
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
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
Usefulurls • Design patterns in Groovy • http://groovy.codehaus.org/Design+Patterns+with+Groovy • Design patterns in Java • http://sourcemaking.com/design_patterns