Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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. Matteo Vaccari
    [email protected]
    [email protected]
    (cc) Alcuni diritti riservati
    L’arte perduta di
    pensare a oggetti

    View full-size slide

  2. Che obiettivo ci diamo?

    View full-size slide

  3. Costo del cambiamento
    Kent Beck, Extreme Programming Explained

    View full-size slide

  4. Può essere così?
    Kent Beck, Extreme Programming Explained

    View full-size slide

  5. OCP, DIP, SRP, LSP,...
    Decorator, Mediator,
    Bridge, Adapter, Composite,
    Visitor, Flyweight...

    View full-size slide

  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/

    View full-size slide

  7. Minimo ma flessibile
    Dafydd Rees

    View full-size slide

  8. 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?

    View full-size slide

  9. Che cos’è un oggetto?

    View full-size slide

  10. Che cos’è un oggetto?
    Data
    Proc1
    Proc2 Proc3
    Proc4
    Proc5
    David West, Object Thinking

    View full-size slide

  11. OOP è un paradigma nuovo
    OOP decompone il software in maniera diversa

    View full-size slide

  12. Solve
    Problem
    Read
    Input
    Compute
    Answer
    Write
    Output
    Setup
    Printer
    Iterate
    over
    things
    Clo
    Prin
    Read
    hings
    Validate
    Things
    Store
    Things Foo Bar Baz

    View full-size slide

  13. Pryce e Freeman, Growing Object-Oriented Software

    View full-size slide

  14. Pensiero a oggetti Pensiero formale
    vs.

    View full-size slide

  15. Kent Beck, Ward Cunningham http://c2.com/doc/oopsla89/paper.html
    Class, Responsibilities, Collaborators
    Vs The UML formal meta-model

    View full-size slide

  16. Vs. documenti di specifica
    Cards + conversations

    View full-size slide

  17. Eiffel
    Vs
    Smalltalk

    View full-size slide

  18. Un esempio:
    http access log report

    View full-size slide

  19. 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

    View full-size slide

  20. Usa la forza degli
    oggetti, Luke!

    View full-size slide

  21. Access Calendar
    Day Report
    AccessLog

    View full-size slide

  22. Usa la forza del
    TDD, Luke!

    View full-size slide

  23. First test: a report with one row and one column

    View full-size slide

  24. Second test: choose to develop rows? or to develop
    columns?

    View full-size slide

  25. “Semplici strutture dati”

    View full-size slide

  26. 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

    View full-size slide

  27. Robert Martin, Clean Coders episode VII

    View full-size slide


  28. First Name

    Last Name

    Email



    View full-size slide

  29. 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 ...

    View full-size slide

  30. 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());
    }
    }
    }

    View full-size slide

  31. 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();
    }
    }

    View full-size slide

  32. The basic principle of recursive
    design is to make the parts have the
    same power as the whole.
    -- Alan Kay

    View full-size slide

  33. 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);
    }
    }

    View full-size slide

  34. 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;
    }

    View full-size slide

  35. public class UserView extends CrudView {
    private HtmlForm form;
    public void buildForm(CompositeDataItem user) {
    form = new HtmlForm(HttpMethod.POST);
    user.displayDataOn(form);
    this.add(form);
    }
    }

    View full-size slide

  36. 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()));
    }

    View full-size slide

  37. 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...

    View full-size slide

  38. OOP è umanesimo

    View full-size slide

  39. Per saperne di più:

    View full-size slide

  40. 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

    View full-size slide

  41. 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)

    View full-size slide

  42. Want to know more?
    http://matteo.vaccari.name/blog/
    [email protected]
    twitter: @xpmatteo
    This presentation can be downloaded from
    http://slideshare.net/xpmatteo

    View full-size slide

  43. Grazie dell’attenzione!
    Extreme Programming:
    development & mentoring

    View full-size slide