design patterns in java and groovy - agile developer...design patterns in java and groovy...
TRANSCRIPT
![Page 1: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/1.jpg)
DESIGN PATTERNS INJAVA AND GROOVY
speaker.identity { name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer', 'Author', 'Trainer' blog 'http://agiledeveloper.com/blog' email '[email protected]'}
![Page 2: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/2.jpg)
AbstractYou're most likely familiar with the Gang-of-four design patterns and how to implement them in Java. However, you wouldn't want to implement those patterns in a similar way in Groovy. Furthermore, there are a number of other useful patterns that you can apply in Java and Groovy. In this presentation we'll look at two things: How to use patterns in Groovy and beyond Gang-of-four patterns in Groovy and Java.
Patterns overview
Implementing common patterns in Groovy
Beyond Gang-of-four patterns in Java and Groovy
Lots of examples2
![Page 3: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/3.jpg)
Abstract Factory
You want to abstract the object creation process
Extensible to add new types of objects
Helpful to create consistent families of objects
3
![Page 4: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/4.jpg)
Abstract Factory
I have a set of operation that I want to perform on objects of different type. I need to create these objects first, then call methods.
In Java, the Class class acts as a factory as well
4
![Page 5: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/5.jpg)
Abstract Factory–Java
5
![Page 6: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/6.jpg)
Abstract Factory–Groovy
6
In Groovy, name of class refers to Class object
Groovy GDK also has added methods to Class
![Page 7: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/7.jpg)
Pluggable Behavior-Strategy
You want to vary the guts or internal implementation of certain algorithm or task
You want to be able to plug some behavior or variations in the middle of a certain computation or task
7
![Page 8: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/8.jpg)
Pluggable Behavior–Java
8
![Page 9: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/9.jpg)
Pluggable Behavior–Groovy
9
Closures in Groovy make it real simple to realize this
![Page 10: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/10.jpg)
Pluggable Behavior–Groovy
10
You can store away the pluggable code for later use, if you like
![Page 11: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/11.jpg)
Execute Around Method(EAM)
You have a pair of operation that needs to be performed before and after operations
You have a resource that needs to be opened/connected to and then safely/automatically closed
You want deterministic control of when resource is deallocated (after all you can’t rely on the finalizer)
11
![Page 12: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/12.jpg)
EAM–Java
12Hmm, not very elegant, is it?
![Page 13: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/13.jpg)
EAM–Groovy
13
![Page 14: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/14.jpg)
Iterator
You want to traverse a collection of objects in a consistent manner independent of the type of collection
An external iterator allows you to control the actual traversal
An internal iterator takes care of that—you provide code that needs to be executed for each element in the collection
14
![Page 15: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/15.jpg)
Iterator–JavaYou can use for (for-each) on any class that implements Iterable interface—external iterator
15
![Page 16: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/16.jpg)
Iterator–GroovyGroovy each method provides internal iterator
16
![Page 17: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/17.jpg)
Cascade
You want to perform a series of operations on an object
The operations form a cohesive sequence or thread of calls
Provides a context, reduces noise
Call the method on the result of previous call or establish a context object
17
![Page 18: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/18.jpg)
Cascade–Java
18
![Page 19: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/19.jpg)
Cascade–GroovyGroovy provides a special set of methods: with and identity
19
![Page 20: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/20.jpg)
Intercept, Cache, Invoke
You’re interested in synthesizing method
However, you don’t want to take a performance hit each time your dynamic method is called
You want to intercept the call, create and cache the implementation, and invoke it
20
![Page 21: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/21.jpg)
Intercept, Cache, Invoke
21
![Page 22: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/22.jpg)
Delegation
You want to delegate calls to methods of another object
You prefer to use delegation over inheritance
22
![Page 23: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/23.jpg)
Delegation–Groovy
23
See Next Page
![Page 24: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/24.jpg)
Delegation–Groovy...
24
![Page 25: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/25.jpg)
Delegation–Groovy...
25
![Page 26: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/26.jpg)
Delegation–Groovy...
That’s nice, but...
That’s a lot of work
You don’t want to pay that toll each time you want to delegate
Let’s refactor
26
![Page 27: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/27.jpg)
Delegation–Refactored
27
What’s that?
Next Page...
Elegant, eh?!
![Page 28: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/28.jpg)
Delegation–Refactored
28
![Page 29: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/29.jpg)
ReferencesDesign Patterns: Elements of Reusable Object-Oriented Software, by Erich Gamma, Richard Helm, Ralph Johnson, John M. Vlissides, Addison-Wesley.
Smalltalk Best Practice Patterns, by Kent Beck, Prentice Hall.
Programming Groovy: Dynamic Productivity for the Java Developers—Venkat Subramaniam, Pragmatic Bookshelf.
29
You can download examples and slides fromhttp://www.agiledeveloper.com - download
![Page 30: design patterns in java and groovy - Agile Developer...DESIGN PATTERNS IN JAVA AND GROOVY speaker.identity {name 'Venkat Subramaniam' company 'Agile Developer, Inc.' credentials 'Programmer',](https://reader030.vdocument.in/reader030/viewer/2022040606/5eae81e2652276674505f8cb/html5/thumbnails/30.jpg)
Thank You!
Please fill in your session evaluations
30