$30 off During Our Annual Pro Sale. View Details »

L'arte perduta di pensare ad oggetti

L'arte perduta di pensare ad oggetti

La mia presentazione a Codemotion Roma 2012

Matteo Vaccari

March 24, 2012
Tweet

More Decks by Matteo Vaccari

Other Decks in Technology

Transcript

  1. 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/
  2. Finalmente ho capito come collaborare! 1982 Commodore Vic20 1991 Laurea

    in Scienze dell'Informazione 1998 Dottorato in Informatica 2003 Extreme Programming! 2007 Team Orione in Sourcesense → XPeppers 2009 Workshop su Design Emergente FC 2011 Object Thinking APPASSIONATO HOTSHOT FORMALISTA XPer Una storia Non tutti i progetti fatti con XP vanno bene. Alcuni hanno problemi di qualità del codice... Forse che ho trascurato il design?
  3. Solve Problem Read Input Compute Answer Write Output Setup Printer

    Iterate over things Clo Prin Read hings Validate Things Store Things Foo Bar Baz
  4. Vs

  5. Access Log Report Esercizio - estrarre un report da un

    file di accessi di Apache Si vuole scrivere un programma batch che legge in input un file di accessi di Apache e 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 11870 192.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 264 192.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 263 192.168.100.184 - - [01/Aug/2011:13:20:49 +0200] "GET /manager/status?XML=true HTTP/1.1" 401 2486 Il nostro obiettivo è estrarre un report di questa forma data risultati-2xx risultati-3xx risultati-4xx risultati-5xx 29/Jul/2011 1223 23 456 12 01/Aug/2011 1212 24 11 123
  6. Mature programmers know that the idea that everything is an

    object is a myth. Sometimes you really do want simple data structures with procedures operating on them. Robert Martin, Clean Code
  7. <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>
  8. 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; } } 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 ...
  9. 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()); } } }
  10. 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(); } }
  11. The basic principle of recursive design is to make the

    parts have the same power as the whole. -- Alan Kay
  12. 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); } }
  13. 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; }
  14. public class UserView extends CrudView { private HtmlForm form; public

    void buildForm(CompositeDataItem user) { form = new HtmlForm(HttpMethod.POST); user.displayDataOn(form); this.add(form); } }
  15. 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())); }
  16. 1982 Commodore Vic20 1991 Laurea in Scienze dell'Informazione 1998 Dottorato

    in Informatica 2003 Extreme Programming! 2007 Team Orione in Sourcesense → XPeppers 2009 Workshop su Design Emergente FC 2011 Object Thinking 2012 Success! APPASSIONATO HOTSHOT FORMALISTA XPer Una storia... L’ultimo progetto consegnato dal “mio” team XP: 1400 giornate su nove mesi, messo in produzione con pochissimi difetti e nei tempi previsti! ...che prosegue...
  17. 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)