L'arte perduta di pensare ad oggetti

3,337 views
3,278 views

Published on

La mia presentazione al Codemotion 2012

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,337
On SlideShare
0
From Embeds
0
Number of Embeds
1,270
Actions
Shares
0
Downloads
18
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

L'arte perduta di pensare ad oggetti

  1. 1. L’arte perduta dipensare a oggetti Matteo Vaccari vaccari@pobox.com matteo.vaccari@xpeppers.com (cc) Alcuni diritti riservati
  2. 2. Che obiettivo ci diamo?
  3. 3. Costo del cambiamento Kent Beck, Extreme Programming Explained
  4. 4. Può essere così? Kent Beck, Extreme Programming Explained
  5. 5. OCP, DIP, SRP, LSP,...Decorator, Mediator,Bridge, Adapter, Composite, Visitor, Flyweight...
  6. 6. Not so many people can:• start with some simple, concrete user stories and build a minimal yet flexible object model, Dafydd Rees, Responsibility Driven Design References http://www.dafydd.net/archive/2010/
  7. 7. Minimo ma flessibile Dafydd Rees
  8. 8. Una storia Finalmente ho capito come collaborare!1982 Commodore Vic20 APPASSIONATO1991 Laurea in Scienze dellInformazione HOTSHOT1998 Dottorato in Informatica FORMALISTA2003 Extreme Programming! XPer2007 Team Orione in Sourcesense → XPeppers2009 Workshop su Design Emergente FC2011 Object Thinking Non tutti i progetti fatti con XP vanno bene. Alcuni hanno problemi di qualità del codice... Forse che ho trascurato il design?
  9. 9. Che cos’è un oggetto?
  10. 10. Che cos’è un oggetto? Proc1 Proc5Proc2 Data Proc3 Proc4 David West, Object Thinking
  11. 11. OOP è un paradigma nuovoOOP decompone il software in maniera diversa
  12. 12. Solve Problem Read Compute Write Input Answer Output IterateRead Validate Store Setup Clo Foo Bar Baz overhings Things Things Printer Prin things
  13. 13. Pryce e Freeman, Growing Object-Oriented Software
  14. 14. Pensiero a oggetti vs. Pensiero formale
  15. 15. Class, Responsibilities, Collaborators Vs The UML formal meta-model Kent Beck, Ward Cunningham http://c2.com/doc/oopsla89/paper.html
  16. 16. Vs
  17. 17. Cards + conversations Vs. documenti di specifica
  18. 18. Smalltalk Vs Eiffel
  19. 19. Un esempio:http access log report
  20. 20. Access Log ReportEsercizio - estrarre un report da un file di accessi di ApacheSi vuole scrivere un programma batch che legge in input un file di accessi di Apachee produce in output un report.Il nostro input è un file di questo tipo:192.168.20.192 - - [29/Jul/2011:14:19:48 +0200] "GET / HTTP/1.1" 200 11870192.168.20.192 - - [29/Jul/2011:14:19:49 +0200] "GET /tomcat.png HTTP/1.1" 304 -192.168.20.192 - - [29/Jul/2011:14:20:39 +0200] "POST /phoenix-0.0/phoenixServlet HTTP/1.1" 200 264192.168.20.192 - - [29/Jul/2011:14:21:23 +0200] "GET /phoenix-0.0/tranpipe-demo/demo.html HTTP/1.1"192.168.20.191 - - [29/Jul/2011:15:48:04 +0200] "POST /phoenix-0.0/phoenixServlet HTTP/1.1" 200 263192.168.100.184 - - [01/Aug/2011:13:20:49 +0200] "GET /manager/status?XML=true HTTP/1.1" 401 2486Il nostro obiettivo è estrarre un report di questa formadata risultati-2xx risultati-3xx risultati-4xx risultati-5xx29/Jul/2011 1223 23 456 1201/Aug/2011 1212 24 11 123
  21. 21. Usa la forza degli oggetti, Luke!
  22. 22. AccessLog Access Calendar Day Report
  23. 23. Usa la forza del TDD, Luke!
  24. 24. First test: a report with one row and one column
  25. 25. Second test: choose to develop rows? or to develop columns?
  26. 26. “Semplici strutture dati”
  27. 27. Mature programmers know that the idea that everything is an object isa myth. Sometimes you really do want simple data structures with procedures operating on them. Robert Martin, Clean Code
  28. 28. Robert Martin, Clean Coders episode VII
  29. 29. <form> <label for="firstName">First Name</label> <input id="firstName" name="firstName" value="${firstName}" /> <label for="lastName">Last Name</label> <input id="lastName" name="lastName" value="${lastName}" /> <label for="email">Email</label> <input id="email" name="email" value="${email}" /> <!-- and so on and so forth for 100 other fields --></form>
  30. 30. class RegistrationRequest { private String firstName; private String lastName; private String email; // and so on and so forth ... public void setFirstName(String firstName) { this.firstName = firstName; } public String getFirstName() { return firstName; } // and so on and so forth ... public ErrorList validate() { ErrorList errors = new ErrorList(); if (isEmpty(firstName)) { errors.add("First Name is required"); } if (isEmpty(lastName)) { errors.add("Last Name is required"); } // and so on and so forth .... return errors; } }
  31. 31. class RegistrationRequestController { public void handleRequest( HttpServletRequest request, HttpServletRequest response ) { RegistrationRequest rr = new RegistrationRequest(); rr.setFirstName(request.getParameter("firstName")); rr.setLastName(request.getParameter("lastName")); rr.setEmail(request.getParameter("email")); // etc etc ... if (rr.isEmpty()) { registrationDao.save(rr); response.redirectTo(THANKYOU_PAGE); } else { registrationView.setErrors(errors); response.getWriter().write(registrationView.toHtml()); } }}
  32. 32. class RegistrationDao { public void save(RegistrationRequest rr) { String sql = "insert into registration_requests" + " (firstName, lastName, email, ...) " + // etc. " values (?,?,?,....)"; // etc. PreparedStatement statement = connection.prepareStatement(sql); statement.setObject(1, rr.getFirstName()); statement.setObject(2, rr.getLastName()); // ... arrrgh! statement.execute(); }}
  33. 33. The basic principle of recursivedesign is to make the parts have thesame power as the whole. -- Alan Kay
  34. 34. public class User extends CompositeDataItem { private PrimaryKeyDataItem userId = new PrimaryKeyDataItem("UserId"); private StringDataItem username = new NonEditableStringDataItem("Username"); private StringDataItem firstName = new StringDataItem("FirstName"); private StringDataItem lastName = new StringDataItem("LastName"); private PasswordDataItem password = new PasswordDataItem("Password", "HashedPassword"); private StringDataItem email = new StringDataItem("Email"); private BooleanDataItem enabled = new BooleanDataItem("Enabled"); public User(Row row) { for (DataItem item : items.values()) { item.setFrom(row); } } public User(ParameterMap parameterMap) { for (DataItem item : items.values()) { item.setFrom(parameterMap); } }
  35. 35. public void displayDataOn(HtmlForm form) { for (DataItem item : items.values()) { item.displayDataOn(form); form.addRow(); }}public Row getDatabaseRow() { Row row = new Row(); for (DataItem item : items.values()) { item.putIn(row); } return row;}
  36. 36. public class UserView extends CrudView { private HtmlForm form; public void buildForm(CompositeDataItem user) { form = new HtmlForm(HttpMethod.POST); user.displayDataOn(form); this.add(form); }}
  37. 37. public class UserInsertController { // ... public void handleRequest(WebRequest request, WebResponse response) { if (request.isPost()) { User user = new User(request.parameterMap()); ErrorList errorList = user.validate(); if (errorList.isNotEmpty()) { view.setError(errorList.toHtmlElement()); return; } registry.insertUser(user); response.redirectTo(user.url()); } view.buildForm(new User(request.parameterMap())); }
  38. 38. Una storia...1982 Commodore Vic20 APPASSIONATO1991 Laurea in Scienze dellInformazione HOTSHOT1998 Dottorato in Informatica FORMALISTA2003 Extreme Programming! XPer2007 Team Orione in Sourcesense → XPeppers2009 Workshop su Design Emergente FC2011 Object Thinking2012 Success! L’ultimo progetto consegnato dal “mio” team XP: 1400 giornate su nove mesi, messo in produzione ...che prosegue... con pochissimi difetti e nei tempi previsti!
  39. 39. OOP è umanesimo
  40. 40. Per saperne di più:
  41. 41. Per saperne di più:http://www.cleancoders.com/codecast/clean-code-episode-7/show Architecture: the lost years http://www.youtube.com/watch?v=WpkDN78P884
  42. 42. Per saperne di più: Francesco Cirillo Kent Beck ha già scritto un libro sul design a oggetti -- è il libro sul TDD (Workshop Design Emergente 2009)
  43. 43. Want to know more? http://matteo.vaccari.name/blog/ vaccari@pobox.com twitter: @xpmatteoThis presentation can be downloaded from http://slideshare.net/xpmatteo
  44. 44. Grazie dell’attenzione! Extreme Programming:development & mentoring

×