Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

20071201 Eliminare For @JavaDayRoma2 Roma-IT [ITA]

1,234 views

Published on

I remember the looks of amazement when I reached the slide where there were no more for in java :-).
This prsentation was a great success. I had to repeat it twice on Javaday. And on both occasions we were sold out. Is this perhaps because it was sponsored by the Anti-IF Campaign?

Published in: Business
  • Be the first to comment

  • Be the first to like this

20071201 Eliminare For @JavaDayRoma2 Roma-IT [ITA]

  1. 1. Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License Campagna Anti-IF: Java e i linguaggi dinamici, incrementare le potenzialità espressive di Java [“Eliminare” i FOR] Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  2. 2. Campagna Anti-IF Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  3. 3. Obiettivo Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License Più potere a Java “senza” i FOR − Paper su come “eliminare” i FOR Esperienza di design evolutivo − Attraverso il refactoring − Usando i principi di design Far vincere qualche maglietta :) Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  4. 4. Sviluppo Java Web: lento! Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License News Aggregator@xpug.it http://it.groups.yahoo.com/group/extremeprogramming-it/message/4485: http://it.groups.yahoo.com/group/extremeprogramming-it/message/4485: …… > E poi detta tutta java a sviluppo web non è così tanto veloce in una > E poi detta tutta java a sviluppo web non è così tanto veloce in una > giornata non si riesce. > giornata non si riesce. su questo no :-) Direi che una prima iterazione con tutte le funzionalita' su questo no :-) Direi che una prima iterazione con tutte le funzionalita' di base in una giornata con 2 coppie si riesce a fare. di base in una giornata con 2 coppie si riesce a fare. > Con Rails in mezza o 1/4 :) ma non entriamo in guerre di religione. > Con Rails in mezza o 1/4 :) ma non entriamo in guerre di religione. Se non si riesce probabilmente le storie non sono state spezzate in modo Se non si riesce probabilmente le storie non sono state spezzate in modo iterativo-incrementale/verticale ;-) iterativo-incrementale/verticale ;-) …… Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  5. 5. Il problema ////class Department class Department Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License public void printSlips() {{ public void printSlips() Resource resource = null; Resource resource = null; (resources select: inForce) do: printSlip. (resources select: inForce) do: printSlip. for(int i=0; i i< _resources.size();i++) {{ for(int i=0; < _resources.size();i++) resource = (Resource)_resources.get(i); resource = (Resource)_resources.get(i); if(resource.lastContract().deadline().after(new Date())) {{ if(resource.lastContract().deadline().after(new Date())) System.out.println(resource.name()); System.out.println(resource.name()); System.out.println(resource.salary()); System.out.println(resource.salary()); }} }} public void printSlips() {{ public void printSlips() }} for (Resource eachResource: _resources) {{ for (Resource eachResource: _resources) if(eachResource.lastContract().deadline().after(new Date()) )){{ if(eachResource.lastContract().deadline().after(new Date()) System.out.println(eachResource.name()); System.out.println(eachResource.name()); “Our job is to solve problems, not spoonfeed compilers (…) “Our job is to solve problems, not spoonfeed compilers (…) System.out.println(eachResource.salary()); We need clarity soSystem.out.println(eachResource.salary()); We need clarity sowe can communicate using our code. We we can communicate using our code. We }} value conciseness and the ability to express aarequirement in value conciseness and the ability to express requirement in }} code accurately and efficiently”. code accurately and efficiently”. --Dave Thomas --Dave Thomas Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  6. 6. Parliamo di design ////class Department class Department Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License public void printSlips() {{ public void printSlips() Resource resource = null; Resource resource = null; for(int i=0; i i< _resources.size();i++) {{ for(int i=0; < _resources.size();i++) resource = (Resource)_resources.get(i); resource = (Resource)_resources.get(i); if(resource.lastContract().deadline().after(new Date())) {{ if(resource.lastContract().deadline().after(new Date())) System.out.println(resource.name()); System.out.println(resource.name()); System.out.println(resource.salary()); System.out.println(resource.salary()); }} }} }} Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  7. 7. Parliamo di design ////class Department class Department Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License public void printSlips() {{ public void printSlips() Resource resource = null; Resource resource = null; Ciclo?+Navigazione? Ciclo?+Navigazione? Ciclo?+Navigazione? for(int i=0; i i< _resources.size();i++) {{ for(int i=0; < _resources.size();i++) resource = (Resource)_resources.get(i); resource = (Resource)_resources.get(i); Selezione? Selezione? if(resource.lastContract().deadline().after(new Date())) {{ if(resource.lastContract().deadline().after(new Date())) System.out.println(resource.name()); Contenuti? System.out.println(resource.name()); Contenuti? Media? Media? System.out.println(resource.salary()); System.out.println(resource.salary()); }} }} Layout? Layout? }} public void printSlipForEach() { { public void printSlipForEach() Ciclo? Ciclo? for (Resource eachResource: _resources) { { for (Resource eachResource: _resources) if(eachResource.lastContract().deadline().after(new Date()) ) ){ { if(eachResource.lastContract().deadline().after(new Date()) System.out.println(eachResource.name()); System.out.println(eachResource.name()); System.out.println(eachResource.salary()); System.out.println(eachResource.salary()); }} }} }} Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  8. 8. Abbassare la complessità Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License Il Refactoring: − Aumentare la capacità del codice di rivelare le Lightweight intenzioni di design, a qualsiasi membro del team, Lightweight alla prima occhiata − Migliorare la struttura interna del sistema, consentendo alle necessarie astrazioni di emergere “Our job is to solve problems, not spoonfeed compilers (…) “Our job is to solve problems, not spoonfeed compilers (…) We need clarity so we can communicate using our code. We We need clarity so we can communicate using our code. We value conciseness and the ability to express aarequirement in value conciseness and the ability to express requirement in code accurately and efficiently”. code accurately and efficiently”. --Dave Thomas --Dave Thomas Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  9. 9. Step 1: Separare le logiche ////class Department Department ////class class Department Department class Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License public void printSlips() {{ public void printSlips() {{ printSlips() public void public void printSlips() Resource eachResource = null; Resource resource = null; Resource eachResource = null; Resource resource = null; List inforces = new ArrayList(); for(int i=0; i i< _resources.size();i++) {{ List inforces = new ArrayList(); for(int i=0; < _resources.size();i++) resource for(int i=0; i i< _resources.size();i++) {{ resource= (Resource)_resources.get(i); for(int i=0; < _resources.size();i++) = (Resource)_resources.get(i); Selezione? eachResource = (Resource)_resources.get(i); if(resource.lastContract().deadline().after(new Date())) {{ eachResource = (Resource)_resources.get(i); Selezione? if(resource.lastContract().deadline().after(new Date())) if(eachResource.lastContract().deadline().after(new Date()) )){{ System.out.println(resource.name()); Stampa busta? if(eachResource.lastContract().deadline().after(new Date()) System.out.println(resource.name()); Stampa busta? inforces.add(eachResource); System.out.println(resource.salary()); inforces.add(eachResource); System.out.println(resource.salary()); }} }} }} }} }} Resource eachInForce = null; Resource eachInForce = null; for(int i=0; i i< inforces.size();i++) {{ for(int i=0; < inforces.size();i++) eachInForce = (Resource)inforces.get(i); eachInForce = (Resource)inforces.get(i); System.out.println(eachInForce.name()); System.out.println(eachInForce.name()); System.out.println(eachInForce.salary()); System.out.println(eachInForce.salary()); }} Francesco Cirillo – }} francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  10. 10. Step 2: Semplificare il FOR ////class Department class Department Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License public void printSlips() {{ public void printSlips() …… …… Resource eachInForce = null; Resource eachInForce = null; for(int i=0; i i< inforces.size();i++) {{ for(int i=0; < inforces.size();i++) Ciclo ++navigazione Ciclo navigazione eachInForce = (Resource)inforces.get(i); eachInForce = (Resource)inforces.get(i); System.out.println(eachInForce.name()); System.out.println(eachInForce.name()); Logica i-esima i- System.out.println(eachInForce.salary()); Logica i-esima System.out.println(eachInForce.salary()); }} }} new Block() {{ new Block() public void evaluate(Resource each) {{ public void evaluate(Resource each) System.out.println(each.name()); System.out.println(each.name()); System.out.println(each.salary()); System.out.println(each.salary()); }}.evaluate(eachInForce); }}.evaluate(eachInForce); Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  11. 11. Step 2: Semplificare il FOR ////class Department class Department Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License public void printSlips() {{ public void printSlips() …… …… Resource eachInForce = null; Resource eachInForce = null; for(int i=0; i i< inforces.size();i++) {{ for(int i=0; < inforces.size();i++) Ciclo ++navigazione Ciclo navigazione eachInForce = (Resource)inforces.get(i); eachInForce = (Resource)inforces.get(i); new Block() {{ new Block() public void evaluate(Resource each) {{ public void evaluate(Resource each) Logica i-i-esima Logicai-esima System.out.println(each.name()); System.out.println(each.name()); System.out.println(each.salary()); System.out.println(each.salary()); }}.evaluate(eachInForce); }}.evaluate(eachInForce); }} }} Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  12. 12. Step 2: Semplificare il FOR ////class Department class Department Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License public void printSlips() {{ public void printSlips() …… …… Resource eachInForce = null; Resource eachInForce = null; Ciclo ++navigazione for(int i=0; i i< inforces.size();i++) {{ for(int i=0; < inforces.size();i++) Ciclo navigazione eachInForce = (Resource)inforces.get(i); eachInForce = (Resource)inforces.get(i); new PrintSlip().evaluate(eachInForce); Logica i-i-esima new PrintSlip().evaluate(eachInForce); Logicai-esima }} }} public class PrintSlip implements Block {{ public class PrintSlip implements Block public void evaluate(Resource aResource) {{ public void evaluate(Resource aResource) System.out.println(aResource.name()); System.out.println(aResource.name()); System.out.println(aResource.salary()); System.out.println(aResource.salary()); }} }} Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  13. 13. Step 3: Eliminare il FOR ////class Department class Department Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License public void printSlips() {{ public void printSlips() …… …… Resource eachInForce = null; Resource eachInForce = null; Ciclo ++navigazione for(int i=0; i i< inforces.size();i++) {{ for(int i=0; < inforces.size();i++) Ciclo navigazione eachInForce = (Resource)inforces.get(i); eachInForce = (Resource)inforces.get(i); new PrintSlip().evaluate(eachInForce); Logica i-i-esima new PrintSlip().evaluate(eachInForce); Logicai-esima }} }} public class PrintSlip implements Block {{ public class PrintSlip implements Block public void evaluate(Resource aResource) {{ public void evaluate(Resource aResource) System.out.println(aResource.name()); System.out.println(aResource.name()); System.out.println(aResource.salary()); System.out.println(aResource.salary()); }} }} Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  14. 14. Step 3: Eliminare il FOR ////class Department class Department Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License public void printSlips() {{ public void printSlips() …… …… Iterator iterator = inforces.iterator(); Iterator iterator = inforces.iterator(); Ciclo Ciclo while(iterator.hasNext()) {{ while(iterator.hasNext()) new PrintSlip().evaluate(iterator.next()); Logica i-i-esima new PrintSlip().evaluate(iterator.next()); Logicai-esima }} }} public class PrintSlip implements Block {{ public class PrintSlip implements Block public void evaluate(Resource aResource) {{ public void evaluate(Resource aResource) System.out.println(aResource.name()); System.out.println(aResource.name()); System.out.println(aResource.salary()); System.out.println(aResource.salary()); }} }} Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  15. 15. Step 3: Eliminare il FOR ////class Department class Department Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License public void printSlips(…) {{ public void printSlips(…) …… …… Iterator iterator = inforces.iterator(); Iterator iterator = inforces.iterator(); Ciclo Ciclo while(iterator.hasNext()) {{ while(iterator.hasNext()) aBlock.evaluate(iterator.next()); aBlock.evaluate(iterator.next()); Logica i-i-esima Logicai-esima }} }} public class PrintSlip implements Block {{ public class PrintSlip implements Block public void evaluate(Resource aResource) {{ public void evaluate(Resource aResource) System.out.println(aResource.name()); System.out.println(aResource.name()); System.out.println(aResource.salary()); System.out.println(aResource.salary()); }} }} Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  16. 16. Step 3: Eliminare il FOR public class ResourceOrderedCollection {{ public class ResourceOrderedCollection private Collection _items; private Collection _items; Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License public ResourceOrderedCollection(Collection items) {{ public ResourceOrderedCollection(Collection items) Navigazione Navigazione _items ==items; _items items; }} public void forEachDo(Block aBlock) {{ public void forEachDo(Block aBlock) Iterator iterator ==_items.iterator(); Iterator iterator _items.iterator(); Ciclo Ciclo while(iterator.hasNext()) {{ while(iterator.hasNext()) aBlock.evaluate((Resource)iterator.next()); aBlock.evaluate((Resource)iterator.next()); }} }} public void printSlips() {{ public void printSlips() }} Resource eachResource ==null; Resource eachResource null; List inforces ==new ArrayList(); List inforces new ArrayList(); for(int i=0; i i<<_resources.size();i++) {{ for(int i=0; _resources.size();i++) eachResource ==(Resource)_resources.get(i); eachResource (Resource)_resources.get(i); if(eachResource.lastContract().deadline().after(new Date()) ) ){{ if(eachResource.lastContract().deadline().after(new Date()) inforces.add(eachResource); inforces.add(eachResource); }} }} new ResourceOrderedCollection(inforces).forEachDo(new PrintSlip()); new ResourceOrderedCollection(inforces).forEachDo(new PrintSlip()); Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it }} Javaday Roma - 01/12/2007
  17. 17. Step 4: E la selezione public void printSlips() {{ public void printSlips() Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License Resource eachResource ==null; Resource eachResource null; List inforces ==new ArrayList(); List inforces new ArrayList(); Ciclo ++navigazione Ciclo navigazione for(int i=0; i i<<_resources.size();i++) {{ for(int i=0; _resources.size();i++) eachResource ==(Resource)_resources.get(i); eachResource (Resource)_resources.get(i); if(eachResource.lastContract().deadline().after(new Date()) ) ){{ Selezione Selezione Logica i-i-esima Logicai-esima if(eachResource.lastContract().deadline().after(new Date()) inforces.add(eachResource); inforces.add(eachResource); Addizione Addizione }} }} public void printSlips() {{ public void printSlips() new ResourceOrderedCollection(inforces).forEachDo(new PrintSlip()); Resource eachResource ==null; new ResourceOrderedCollection(inforces).forEachDo(new PrintSlip()); Resource eachResource null; }} List inforces ==new ArrayList(); List inforces new ArrayList(); for(int i=0; i i<<_resources.size();i++) {{ for(int i=0; _resources.size();i++) eachResource ==(Resource)_resources.get(i); eachResource (Resource)_resources.get(i); if(new PredicateBlock() {{ if(new PredicateBlock() public boolean is(Resource each) {{ public boolean is(Resource each) return each.lastContract().deadline().after(new Date()); return each.lastContract().deadline().after(new Date()); }}.is(eachResource) {{ }}.is(eachResource) inforces.add(eachResource); inforces.add(eachResource); }} }} new ResourceOrderedCollection(inforces).forEachDo(new PrintSlip()); new ResourceOrderedCollection(inforces).forEachDo(new PrintSlip()); Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it }} Javaday Roma - 01/12/2007
  18. 18. Step 4: E la selezione public void printSlips() {{ public void printSlips() Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License Resource eachResource ==null; Resource eachResource null; List inforces ==new ArrayList(); List inforces new ArrayList(); Ciclo ++navigazione Ciclo navigazione for(int i=0; i i<<_resources.size();i++) {{ for(int i=0; _resources.size();i++) eachResource ==(Resource)_resources.get(i); eachResource (Resource)_resources.get(i); if(new InForcePredicate().is(eachResource)) {{ if(new InForcePredicate().is(eachResource)) inforces.add(eachResource); inforces.add(eachResource); Addizione Addizione }} }} new ResourceOrderedCollection(inforces).forEachDo(new PrintSlip()); new ResourceOrderedCollection(inforces).forEachDo(new PrintSlip()); }} public class InForcePredicate implements PredicateBlock {{ public class InForcePredicate implements PredicateBlock public boolean is(Resource each) {{ public boolean is(Resource each) Selezione Selezione return each.lastContract().deadline().after(new Date()); return each.lastContract().deadline().after(new Date()); }} }} Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  19. 19. Step 4: E la selezione public void printSlips() {{ public void printSlips() Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License Collection inforces ==new ResourceOrderedCollection(_resources).select(new InForce()); Collection inforces new ResourceOrderedCollection(_resources).select(new InForce()); new ResourceOrderedCollection(inforces).forEachDo(new PrintSlip()); new ResourceOrderedCollection(inforces).forEachDo(new PrintSlip()); }} public Collection select(PredicateBlock aBlock) {{ public Collection select(PredicateBlock aBlock) Collection result ==new ArrayList(); Collection result new ArrayList(); Iterator iterator ==_items.iterator(); Iterator iterator _items.iterator(); Navigazione Navigazione while(iterator.hasNext()) {{ Ciclo Ciclo while(iterator.hasNext()) Resource resource ==(Resource)iterator.next(); Resource resource (Resource)iterator.next(); ifif(aBlock.is(resource)) result.add(resource); (aBlock.is(resource)) result.add(resource); Addizione Addizione }} return result; return result; }} public class InForcePredicate implements PredicateBlock {{ public class InForcePredicate implements PredicateBlock public boolean is(Resource each) {{ public boolean is(Resource each) Selezione Selezione return each.lastContract().deadline().after(new Date()); return each.lastContract().deadline().after(new Date()); }} }} Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  20. 20. Step 5: Per leggere meglio public void printSlips() {{ public void printSlips() Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License Collection inforces ==new ResourceOrderedCollection(_resources).select(new InForce()); Collection inforces new ResourceOrderedCollection(_resources).select(new InForce()); new ResourceOrderedCollection(inforces).forEachDo(new PrintSlip()); new ResourceOrderedCollection(inforces).forEachDo(new PrintSlip()); }} public void printSlips() { public void printSlips() { (new ResourceOrderedCollection(_resources).select(new InForce()).forEachDo(new PrintSlip()); (new ResourceOrderedCollection(_resources).select(new InForce()).forEachDo(new PrintSlip()); }} public ResourceOrderedCollection select(PredicateBlock aBlock) { public ResourceOrderedCollection select(PredicateBlock aBlock) { ResourceOrderedCollection result ==new ResourceOrderedCollection(); ResourceOrderedCollection result new ResourceOrderedCollection(); Iterator iterator ==_items.iterator(); Iterator iterator _items.iterator(); while(iterator.hasNext()) {{ while(iterator.hasNext()) Resource resource ==(Resource)iterator.next(); Resource resource (Resource)iterator.next(); ifif(aBlock.is(resource)) result.add(resource); (aBlock.is(resource)) result.add(resource); }} return result; return result; }} public void printSlips() {{ public void printSlips() Predicate isInForce ==new InForce(); Predicate isInForce new InForce(); Block printSlip ==new PrintSlip(); Block printSlip new PrintSlip(); ResourceOrderedCollection resources ==new ResourceOrderedCollection(_resources); ResourceOrderedCollection resources new ResourceOrderedCollection(_resources); resources.select(isInForce).forEachDo(printSlip); resources.select(isInForce).forEachDo(printSlip); }} Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  21. 21. Step 5: Per leggere meglio public void printSlips() {{ public void printSlips() Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License Predicate isInForce ==new InForce(); Predicate isInForce new InForce(); Block printSlip ==new PrintSlip(); Block printSlip new PrintSlip(); ResourceOrderedCollection resources ==new ResourceOrderedCollection(_resources); ResourceOrderedCollection resources new ResourceOrderedCollection(_resources); resources.select(isInForce).forEachDo(printSlip); resources.select(isInForce).forEachDo(printSlip); }} public void printSlips() {{ public void printSlips() printSlipsUsing(new InForce(),new PrintSlip()); printSlipsUsing(new InForce(),new PrintSlip()); }} private void printSlipsUsing(Predicate aCriterium, Block anAction) {{ private void printSlipsUsing(Predicate aCriterium, Block anAction) new ResourceOrderedCollection(_resources).select(aCriterium).forEachDo(anAction); new ResourceOrderedCollection(_resources).select(aCriterium).forEachDo(anAction); }} public void printSlipsUsing(Predicate aCriterium, Block anAction) {{ public void printSlipsUsing(Predicate aCriterium, Block anAction) _resources.select(aCriterium).forEachDo(anAction); _resources.select(aCriterium).forEachDo(anAction); }} private ResourceOrderedCollection _resources; private ResourceOrderedCollection _resources; Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  22. 22. Step 6: Generalizzare new ResourceOrderedCollection(resources).select(isInForce).forEachDo(printSlip); Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License new ResourceOrderedCollection(resources).select(isInForce).forEachDo(printSlip); public class ResourceOrderedCollection { { public class ResourceOrderedCollection …… public void forEachDo(Block aBlock) { { public void forEachDo(Block aBlock) Iterator iterator ==_items.iterator(); Iterator iterator _items.iterator(); while(iterator.hasNext()) { { public class PrintSlip implements Block { { public class PrintSlip implements Block while(iterator.hasNext()) aBlock.evaluate((Resource)iterator.next()); public void evaluate(Resource aResource) { { public void evaluate(Resource aResource) aBlock.evaluate((Resource)iterator.next()); }} System.out.println(aResource.name()); System.out.println(aResource.name()); }} System.out.println(aResource.salary()); System.out.println(aResource.salary()); public ResourceOrderedCollection select(Predicate aBlock) { { }} public ResourceOrderedCollection select(Predicate aBlock) ResourceOrderedCollection result == ResourceOrderedCollection result }} new ResourceOrderedCollection(); new ResourceOrderedCollection(); Iterator iterator ==_items.iterator(); Iterator iterator _items.iterator(); while(iterator.hasNext()) { { while(iterator.hasNext()) Resource resource ==(Resource)iterator.next(); Resource resource (Resource)iterator.next(); ifif(aBlock.is(resource)) result.add(resource); (aBlock.is(resource)) result.add(resource); }} return result; return result; }} public class InForcePredicate implements PredicateBlock { { public class InForcePredicate implements PredicateBlock }} public boolean is(Resource each) { { public boolean is(Resource each) return each.lastContract().deadline().after(new Date()); return each.lastContract().deadline().after(new Date()); }} }} Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  23. 23. Step 6: Generalizzare new ResourceOrderedCollection(resources).select(isInForce).forEachDo(printSlip); Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License new ResourceOrderedCollection(resources).select(isInForce).forEachDo(printSlip); public class ResourceOrderedCollection { { public class ResourceOrderedCollection … public class OrderedCollection { { public class OrderedCollection … …… public void forEachDo(Block aBlock) {class InForcePredicate implements Predicate { public void forEachDo(Block aBlock) {class InForcePredicate implements Predicate { public public public void forEachDo(Block aBlock) { { Iterator iterator ==_items.iterator(); boolean is(Object anObject) { forEachDo(Block aBlock) public void Iterator iterator _items.iterator(); boolean is(Object anObject) { iterator = _items.iterator(); public while(iterator.hasNext()) { { public Iterator iterator = _items.iterator(); Iterator while(iterator.hasNext()) return ((Resource)anObject).lastContract().deadline().after(new Date()); while(iterator.hasNext()) { { return ((Resource)anObject).lastContract().deadline().after(new Date()); aBlock.evaluate((Resource)iterator.next()); aBlock.evaluate((Resource)iterator.next()); while(iterator.hasNext()) }} aBlock.evaluate(iterator.next()); }} aBlock.evaluate(iterator.next()); }} }} }} public class PrintSlip implements Block { public ResourceOrderedCollection Block { public class PrintSlip implements }} public ResourceOrderedCollectionselect(Predicate aBlock) { { public OrderedCollection select(Predicate aBlock) { select(Predicate aBlock) public void evaluate(Resource aResource) { { public void evaluate(Resource aResource) ResourceOrderedCollection result == public OrderedCollection select(Predicate aBlock) { ResourceOrderedCollection result System.out.println(aResource.name()); OrderedCollection result == System.out.println(aResource.name()); new ResourceOrderedCollection(); OrderedCollection result new ResourceOrderedCollection(); System.out.println(aResource.salary()); new OrderedCollection(); Iterator System.out.println(aResource.salary()); Iteratoriterator ==_items.iterator(); iterator _items.iterator(); new OrderedCollection(); }} while(iterator.hasNext()) { { Iterator iterator ==_items.iterator(); Iterator iterator _items.iterator(); }} while(iterator.hasNext()) while(iterator.hasNext()) { { Resource resource ==(Resource)iterator.next(); while(iterator.hasNext()) Resource resource (Resource)iterator.next(); PrintSlip implements Block { = iterator.next(); public class PrintSlip implements object { = iterator.next(); ifif(aBlock.is(resource)) result.add(resource); public class Object Block Object object (aBlock.is(resource)) result.add(resource); }} public void evaluate(Object anObject) { { result.add(object); ifif(aBlock.is(object)) result.add(object); (aBlock.is(object)) public void evaluate(Object anObject) return result; }} System.out.println(((Resource)anObject).name()); return result; System.out.println(((Resource)anObject).name()); return result; }} System.out.println(((Resource)anObject).salary());} return result; System.out.println(((Resource)anObject).salary());} }} }} }} public class InForcePredicate implements PredicateBlock { { } public class InForcePredicate implements PredicateBlock public boolean is(Resource each) { { } public boolean is(Resource each) return each.lastContract().deadline().after(new Date()); return each.lastContract().deadline().after(new Date()); } } Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Francesco }} Javaday Roma - 01/12/2007
  24. 24. Dove siamo ////class Department class Department Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License public void printSlips() {{ public void printSlips() Resource resource = null; Resource resource = null; Navigazione? Navigazione? for(int i=0; i i< _resources.size();i++) {{ for(int i=0; < _resources.size();i++) resource = (Resource)_resources.get(i); resource = (Resource)_resources.get(i); Selezione? Selezione? if(resource.lastContract().deadline().after(new Date())) {{ if(resource.lastContract().deadline().after(new Date())) System.out.println(resource.name()); Contenuti? System.out.println(resource.name()); Contenuti? Media? Media? System.out.println(resource.salary()); System.out.println(resource.salary()); }} }} Layout? Layout? }} _resources.select(onlyWomen).forEachDo(printReport); _resources.select(isInForce).forEachDo(printSlip); _resources.select(onlyWomen).forEachDo(printReport); _resources.select(isInForce).forEachDo(printSlip); Contenuti+ Contenuti+ Navigazione Navigazione Selezione Selezione Media+ Media+ Layout Layout Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  25. 25. ////class Department class Department public void printSlips() { { Review & ToDoList public void printSlips() Resource resource ==null; Resource resource null; for(int i=0; i i<<_resources.size();i++) { { for(int i=0; _resources.size();i++) Navigazione? Navigazione? resource ==(Resource)_resources.get(i); resource (Resource)_resources.get(i); Selezione? Selezione? if(resource.lastContract().deadline().after(new Date())) { { if(resource.lastContract().deadline().after(new Date())) System.out.println(resource.name()); System.out.println(resource.name()); Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License Media? System.out.println(resource.salary()); Contenuti? Contenuti? Media? System.out.println(resource.salary()); }} }} }} Layout? Layout? 10/01-10/11/2007:Contract Andrea:Employee “Andrea”:String :Department Federica:Manager 1/9/2007-10/10/2008:Contract “Federica”:String System.out:PrintStream Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  26. 26. Review & ToDoList public OrderedCollection select(Predicate aBlock) { { public OrderedCollection select(Predicate aBlock) OrderedCollection result ==new publicCreative Commons Attribution-NonCommercial-ShareAlike 2.5 License OrderedCollection result new OrderedCollection(); publicboolean is(Object each) { { boolean is(Object each) OrderedCollection(); _resources.select(isInForce).forEachDo(printSlip); return ((Resource)each).lastContract().deadline().after(new Date()); return ((Resource)each).lastContract().deadline().after(new Date()); _resources.select(isInForce).forEachDo(printSlip); Iterator iterator ==_items.iterator(); Iterator iterator _items.iterator(); }} while(iterator.hasNext()) { { while(iterator.hasNext()) Object object ==iterator.next(); Object object iterator.next(); ifif(aBlock.is(object)) result.add(object); (aBlock.is(object)) result.add(object); }} return result; 10/01-10/11/2007:Contract return result; }} resources:OrderedCollection Andrea:Employee :InForce “Andrea”:String :Department inForce:OrderedCollection Federica:Manager 1/9/2007-10/10/2008:Contract :PrintSlip “Federica”:String System.out:PrintStream Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  27. 27. Review & ToDoList public OrderedCollection select(Predicate aBlock) { { public class PrintSlip implements Block { { aBlock) public OrderedCollection select(Predicate OrderedCollection result ==new public class PrintSlip implements Block public void forEachDo(Block aBlock) { { public void forEachDo(Block aBlock) public void evaluate(Objectnew OrderedCollection result anObject) { Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License OrderedCollection(); public void evaluate(Object anObject) { Iterator iterator ==_items.iterator(); Iterator iterator _items.iterator(); OrderedCollection(); IteratorSystem.out.println(((Resource)anObject).name()); Iteratoriterator ==_items.iterator(); System.out.println(((Resource)anObject).name()); iterator _items.iterator(); while(iterator.hasNext()) { { while(iterator.hasNext()) System.out.println(((Resource)anObject).salary()); while(iterator.hasNext()) { { System.out.println(((Resource)anObject).salary()); while(iterator.hasNext()) aBlock.evaluate(iterator.next()); aBlock.evaluate(iterator.next()); } } Object object ==iterator.next(); Object object iterator.next(); }} }} ifif(aBlock.is(object)) result.add(object); }} (aBlock.is(object)) result.add(object); }} return result; return result; 10/01-10/11/2007:Contract }} resources:OrderedCollection Andrea:Employee :InForce “Andrea”:String :Department inForce:OrderedCollection Federica:Manager 1/9/2007-10/10/2008:Contract :PrintSlip “Federica”:String System.out:PrintStream Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  28. 28. Riferimenti blocchi e chiusure Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License Blocchi in Java − http://www.c2.com/cgi/wiki?BlocksInJava Blocchi e chiusure in Ruby − http://www.artima.com/intv/closuresP.html Chiusure nel linguaggio Java − http://www.javac.info/closures-v03.html Esempi di librerie − http://jaggregate.sourceforge.net Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  29. 29. Segui la Campagna Anti-IF Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007
  30. 30. Domande Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License Risorse: http://www.xplabs.it/risorse.html http://www.metodiagili.it/campagna-anti-if.html Tecnica del Pomodoro: http://www.tecnicadelpomodoro.it EasyTracking: http://www.myagileprojects.com Gruppo XPLabs’ Friends- Italia: http://it.groups.yahoo.com/group/xplabs-friends/ Extreme Programming User Group – Italia: http://www.xpug.it/ Francesco Cirillo – francesco.cirillo@metodiagili.it- MetodiAgili.it Javaday Roma - 01/12/2007

×