+ All Categories
Home > Documents > Design Patterns in Groovy

Design Patterns in Groovy

Date post: 24-Feb-2016
Category:
Upload: nelson
View: 59 times
Download: 1 times
Share this document with a friend
Description:
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. - PowerPoint PPT Presentation
Popular Tags:
39
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced Information Systems CERN – Geneva, Switzerland
Transcript
Page 1: Design  Patterns in Groovy

CERN – European Organization for Nuclear ResearchGS Department – Administrative Information Services

Design Patterns in Groovy

Nicolas DécrevelAdvanced Information SystemsCERN – Geneva, Switzerland

Page 2: Design  Patterns in Groovy

CERNGS-AIS

Design PatternsSomeone has already solved your

problem!A design pattern is a general repeatable

solution to a commonly occurring problem in software design

Page 3: Design  Patterns in Groovy

CERNGS-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

Page 4: Design  Patterns in Groovy

CERNGS-AIS

Problem

Can my SquareObject fit the RoundHole?

Page 5: Design  Patterns in Groovy

CERNGS-AIS

Adapter PatternAllows objects satisfying one interface

to be used where another type of interface is expected

The SquareObject should be used where we expect a RoundObject

Page 6: Design  Patterns in Groovy

CERNGS-AIS

Adapter Pattern in Java (1)

1. Create an interface

2. Refer to it in your code

3. Adapt the behaviour

Page 7: Design  Patterns in Groovy

CERNGS-AIS

Adapter Pattern in Java (2)

Page 8: Design  Patterns in Groovy

CERNGS-AIS

Adapter Pattern in Groovy (1)

No need of an interface, as long as the object has a radius property

Page 9: Design  Patterns in Groovy

CERNGS-AIS

Adapter Pattern in Groovy (2)

We can even inherit, as not type is defined in objectFits

Page 10: Design  Patterns in Groovy

CERNGS-AIS

Adapter Pattern in Groovy (3)If an interface were to exist

We could create a closure

And use it like this

Page 11: Design  Patterns in Groovy

CERNGS-AIS

Adapter PatternTwo flavours of the pattern:

– The delegation flavour– The inheritance flavour

Not a lot of difference between Java and Groovy

Page 12: Design  Patterns in Groovy

CERNGS-AIS

Problem

What if Bernard get fired but stays in the database ?

Page 13: Design  Patterns in Groovy

CERNGS-AIS

Null Object PatternThe 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

Page 14: Design  Patterns in Groovy

CERNGS-AIS

Null Object Pattern in JavaNO!

YES!

Page 15: Design  Patterns in Groovy

CERNGS-AIS

Null Object Pattern in Groovy

Page 16: Design  Patterns in Groovy

CERNGS-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

Page 17: Design  Patterns in Groovy

CERNGS-AIS

ProblemLet’s extend my simple Logger behaviour

Add time stamp

Add upper message

Add both

Page 18: Design  Patterns in Groovy

CERNGS-AIS

Decorator PatternAttach additional responsibilities to an

object dynamically Provide a flexible alternative to

subclassing for extending functionalityDoesn’t modify source codeDecorators can be combined in flexible

waysAvoid class explosion

Page 19: Design  Patterns in Groovy

CERNGS-AIS

Decorator Pattern in Java

Page 20: Design  Patterns in Groovy

CERNGS-AIS

Decorator Pattern in Groovy (1)

Use @Delegate annotation to delegate method calls

Page 21: Design  Patterns in Groovy

CERNGS-AIS

Decorator Pattern in Groovy (2)

A touch of dynamic behavior

All String arguments will be lowered

Page 22: Design  Patterns in Groovy

CERNGS-AIS

Decorator Pattern in Groovy (3)

Is my logger slow?

Let’s trace it

Page 23: Design  Patterns in Groovy

CERNGS-AIS

Decorator Pattern in Groovy (4)

Page 24: Design  Patterns in Groovy

CERNGS-AIS

Decorator or Observer Pattern?Using interceptor as decorator looks like

the observer patternExcept 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

Page 25: Design  Patterns in Groovy

CERNGS-AIS

Problem

I want to reuse my algorithm with two other encodings

Page 26: Design  Patterns in Groovy

CERNGS-AIS

Template Method PatternTemplate Method lets subclasses

redefine certain steps of an algorithm without changing the algorithm’s structure

Page 27: Design  Patterns in Groovy

CERNGS-AIS

Template Method Pattern in Java

Page 28: Design  Patterns in Groovy

CERNGS-AIS

Template Method Pattern in Groovy

Page 29: Design  Patterns in Groovy

CERNGS-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

Page 30: Design  Patterns in Groovy

CERNGS-AIS

Strategy PatternDefine a family of algorithms,

encapsulate each one, and make them interchangeable

Strategy lets the algorithm vary independently from the clients that use it

Page 31: Design  Patterns in Groovy

CERNGS-AIS

Strategy Pattern in Java

Page 32: Design  Patterns in Groovy

CERNGS-AIS

Strategy Pattern in Java

Page 33: Design  Patterns in Groovy

CERNGS-AIS

Strategy Pattern in GroovyLet’s use some closure

Page 34: Design  Patterns in Groovy

CERNGS-AIS

Strategy PatternThis pattern is really used to have

first class functions Groovy already has first class functions

Page 35: Design  Patterns in Groovy

CERNGS-AIS

Is-A vs Has-AWe 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

Page 36: Design  Patterns in Groovy

CERNGS-AIS

Grails FrameworkA 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

Page 37: Design  Patterns in Groovy

CERNGS-AIS

ConclusionAll Design Patterns can be coded the

same way in Groovy as in JavaSome Patterns have been designed

because of restriction of the languageGroovy, allowing first class function and

weak typing, highlights similarities of different patterns

Page 38: Design  Patterns in Groovy

CERNGS-AIS

Useful urls

Design patterns in Groovy– http://groovy.codehaus.org/Design+Patterns+with+Groovy

Design patterns in Java– http://sourcemaking.com/design_patterns

Page 39: Design  Patterns in Groovy

CERNGS-AIS

Thank You


Recommended