CERN European Organization for Nuclear Research GS Department
- Slides: 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
Design Patterns l Someone has already solved your problem! l A design pattern is a general repeatable solution to a commonly occurring problem in software design CERN GS-AIS
Design Patterns l Creational patterns – – – l Structural patterns – – – – CERN Abstract Factory Design Pattern Builder Design Pattern Factory Method Design Pattern Object Pool Design Pattern Prototype Design Pattern Singleton Design Pattern 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 l 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 Square. Object fit the Round. Hole? CERN GS-AIS
Adapter Pattern l Allows objects satisfying one interface to be used where another type of interface is expected l The Square. Object should be used where we expect a Round. Object 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 object. Fits 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 l Two flavours of the pattern: – The delegation flavour – The inheritance flavour l 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 l 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? l Due to syntax simplification, the Null Object Pattern may be less useful in Groovy than in Java for simple cases l 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 l Attach additional responsibilities to an object dynamically l Provide a flexible alternative to subclassing for extending functionality l Doesn’t modify source code l Decorators can be combined in flexible ways l 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? l Using interceptor as decorator looks like the observer pattern l Except that the observation code is part of its own class (no need to change the observed class) l You can use a Multi. Interceptor. Proxy. Meta. Class to pass multiple observer – http: //groovy. codehaus. org/JN 3515 -Interception CERN GS-AIS
Problem I want to reuse my algorithm with two other encodings CERN GS-AIS
Template Method Pattern l 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 l We still have to create two classes – Reversing. Rot 13 extends Two. Phase. Encryption – Base 64 Reversing extends Two. Phase. Encryption l What if we want to inverse the execution of the phases? – We have to create two other classes • Rot 13 Reversing extends Two. Phase. Encryption • Reversion. Base 64 extends Two. Phase. Encryption l What if we want to use Base 64 with Rot 13 ? – new classes, etc l Is there an other way to write Two. Phase. Encryption? – Extract the parts which are changing : the encryptions CERN GS-AIS
Strategy Pattern l Define a family of algorithms, encapsulate each one, and make them interchangeable l 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 l This pattern is really used to have first class functions l Groovy already has first class functions CERN GS-AIS
Is-A vs Has-A l We have seen two ways of implementing a solution using either inheritance (Is-A) or delegation (Has-A) l One should prefer the Has-A version of an algorithm which will be more resistant to changes l Remember the @Delegate CERN GS-AIS
Grails Framework l. A lot of the Design Patterns are getting useless with usage of complex Frameworks like Spring or Grails l These Frameworks already implement a lot of these Design Patterns to simplify coding l Examples: Template, Strategy, Proxy, Builder, Abstract Factory, Singleton, Observer, etc CERN GS-AIS
Conclusion l All Design Patterns can be coded the same way in Groovy as in Java l Some Patterns have been designed because of restriction of the language l Groovy, allowing first class function and weak typing, highlights similarities of different patterns CERN GS-AIS
Useful urls l Design patterns in Groovy – http: //groovy. codehaus. org/Design+Patterns+with+Groovy l Design patterns in Java – http: //sourcemaking. com/design_patterns CERN GS-AIS
Thank You CERN GS-AIS
- Lesson 15 nuclear quest nuclear reactions
- Fisión nuclear vs fision nuclear
- Cern beams department
- Cern te department
- Cern openlab
- Intel vp weekly compute projectian
- Edh cern
- European landowners organization
- Hse organization chart
- Hseline
- Hse org chart
- Organizational chart of banquet
- Hse advisor adalah
- Tdgxlbnnob4 -site:youtube.com
- Mse org chart
- Hse department organization chart
- Hse department organization chart
- Hse advisor adalah
- Process organization in computer organization
- Block arrangement essay
- European strategy forum on research infrastructures
- European commission community research
- Iso 22301 utbildning
- Typiska drag för en novell
- Nationell inriktning för artificiell intelligens
- Ekologiskt fotavtryck
- Varför kallas perioden 1918-1939 för mellankrigstiden
- En lathund för arbete med kontinuitetshantering
- Underlag för särskild löneskatt på pensionskostnader
- Tidbok för yrkesförare
- A gastrica
- Förklara densitet för barn
- Datorkunskap för nybörjare
- Stig kerman
- Debatt artikel mall
- Autokratiskt ledarskap
- Nyckelkompetenser för livslångt lärande
- Påbyggnader för flakfordon
- Lufttryck formel
- Publik sektor