1 / 39

Design Patterns in Groovy

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.

nelson
Download Presentation

Design Patterns in Groovy

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design Patterns in Groovy Nicolas DécrevelAdvanced Information Systems CERN – Geneva, Switzerland

  2. Design Patterns • Someone has alreadysolvedyourproblem! • A design pattern is a general repeatable solution to a commonly occurring problem in software design

  3. 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

  4. Problem Can mySquareObject fit the RoundHole?

  5. Adapter Pattern • Allows objects satisfying one interface to be used where another type of interface is expected • The SquareObjectshouldbeusedwhereweexpect a RoundObject

  6. Adapter Pattern in Java (1) 1. Create an interface 2. Refer to it in your code 3. Adapt the behaviour

  7. Adapter Pattern in Java (2)

  8. Adapter Pattern in Groovy (1) No need of an interface, as long as the object has a radius property

  9. Adapter Pattern in Groovy (2) Wecaneveninherit, as not type isdefinedin objectFits

  10. Adapter Pattern in Groovy (3) If an interface were to exist Wecouldcreate a closure And use itlikethis

  11. Adapter Pattern • Twoflavours of the pattern: • The delegationflavour • The inheritanceflavour • Not a lot of differencebetween Java and Groovy

  12. Problem What if Bernard getfired but stays in the database ?

  13. 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. Null Object Pattern in Java NO! YES!

  15. Null Object Pattern in Groovy

  16. 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. Problem Let’sextendmy simple Loggerbehaviour Addtime stamp Addupper message Addboth

  18. 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

  19. Decorator Pattern in Java

  20. Decorator Pattern in Groovy (1) Use @Delegate annotation to delegatemethod calls

  21. Decorator Pattern in Groovy (2) A touch of dynamicbehavior All String arguments willbelowered

  22. Decorator Pattern in Groovy (3) Is mylogger slow? Let’s trace it

  23. Decorator Pattern in Groovy (4)

  24. 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

  25. Problem I want to reusemyalgorithmwithtwootherencodings

  26. Template Method Pattern • Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure

  27. Template Method Pattern in Java

  28. Template Method Pattern in Groovy

  29. 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

  30. 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. Strategy Pattern in Java

  32. Strategy Pattern in Java

  33. Strategy Pattern in Groovy Let’s use someclosure

  34. Strategy Pattern • This pattern is really used to have first class functions • Groovy already has first class functions

  35. 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. 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. 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. Usefulurls • Design patterns in Groovy • http://groovy.codehaus.org/Design+Patterns+with+Groovy • Design patterns in Java • http://sourcemaking.com/design_patterns

  39. Thank You

More Related