esempio open closed

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    esempio open closed - Presentation Transcript

    1. Open Closed T. software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification
    2. Esempio
      • Una particolare classe semplice (Naturali, ovvero numeri interi positivi)
    3. Esempio
      • Una particolare classe semplice (Naturali, ovvero numeri interi positivi)
      • Ipotizziamo alcune soluzioni
    4. Esempio
      • Una particolare classe semplice (Naturali, ovvero numeri interi positivi)
      • Ipotizziamo alcune soluzioni
      • Ipotizziamo una estensione (del dominio)
    5. Esempio
      • Una particolare classe semplice (Naturali, ovvero numeri interi positivi)
      • Ipotizziamo alcune soluzioni
      • Ipotizziamo una estensione (del dominio)
      • Quale soluzione tra quelle proposte è “Open Closed” (ovvero consente l'estensione senza necessitare di modifiche)?
    6. Dominio?
      • gestisco denaro contante (quindi solo positivi)
    7. @Pre(&quot;@value>=0&quot;) // dbc framework Contract4j public Natural(int value) { this.value=value; } public Natural(int value) { if (value<0) { throw new InitException(&quot;non negativo: &quot;+value); } this.value=value; } oppure: Costruttore
    8. Aspettative @Test public void zeroEqualsZero() { Natural zero = new Natural(0); Natural anotherZero = new Natural(0); Assert.assertEquals(zero,anotherZero); }
    9. Soluzione 1 @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass () != o. getClass ()) return false; Natural natural = (Natural) o; if (value != natural.value) return false; return true; } @Override public int hashCode() { return value; }
    10. Soluzione 2 @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Natural)) return false; Natural natural = (Natural) o; if (value != natural.value) return false; return true; } @Override public int hashCode() { return value; }
    11. Aspettative @Test public void zeroEqualsZero() { Natural zero = new Natural(0); Natural anotherZero = new Natural(0); Assert.assertEquals(zero,anotherZero); } Soluzione 1. equals con getClass() OK Soluzione 2. equals con instanceof OK
    12. Estensione
      • Valori negativi
    13. “sì alle estensioni! no alle modifiche!” public class Relative extends Natural { @Pre // precondizione rilassata public Relative(int value) { this.value = value; } .... }
    14. “sì alle estensioni! no alle modifiche!” public class Relative extends Natural { @Pre // precondizione rilassata public Relative(int value) { this.value = value; } .... } La precondizione è rilassata. Preconditions cannot be strengthened in a subtype. (Postconditions cannot be weakened in a subtype.)
    15. Correlazioni-osservazioni
      • Natural super classe di Relative
    16. Correlazioni-osservazioni
      • Natural super classe di Relative
      • Natural sotto insieme di Relative (o, che è lo stesso, è isomorfo ad un suo sottoinsieme)
    17. Correlazioni-osservazioni
      • Natural super classe di Relative
      • Natural sotto insieme di Relative (o, che è quasi lo stesso, è isomorfo ad un suo sottoinsieme)
      • Morfismo : (astrazione di un processo che trasforma una struttura astratta in un'altra mantenendo alcune caratteristiche &quot;strutturali&quot; della prima (wikipedia))
    18. Correlazioni-osservazioni
      • Natural super classe di Relative
      • Natural sotto insieme di Relative (o, che è quasi lo stesso, è isomorfo ad un suo sottoinsieme)
      • Morfismo : (astrazione di un processo che trasforma una struttura astratta in un'altra mantenendo alcune caratteristiche &quot;strutturali&quot; della prima (wikipedia))
      • “Is A” != “sottoClasse di” (ricordate “Quadrato-Rettangolo”?)
    19. Concretamente @Test public void testMixEquals() throws Exception { Natural unoNat = new Natural(1); Relative unoRel = new Relative(1); Assert.assertEquals(unoNat,unoRel); } Soluzione 1. equals con getClass() fallisce Soluzione 2. equals con instanceof va a buon fine
    20. Concretamente @Test public void testMixEquals() throws Exception { Natural unoNat = new Natural(1); Relative unoRel = new Relative(1); Assert.assertEquals(unoNat,unoRel); } Sto sostituendo all'1 di Nat, l'1 di Rel (Liskov?)
    21. if S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program What is wanted here is something like the following substitution property: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T. Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T. L.S.P. Varie definizioni
    22. Nuova estensione public class ColoredRelative extends Relative { public static String NEUTRO=&quot;neutro&quot;; public static String GRIGIO=&quot;grigio&quot;; public static String NERO=&quot;nero&quot;; private static String DEFAULT=NEUTRO; protected String colore=DEFAULT; public ColoredRelative(int value) { super(value); } ...
    23. Violazione transitività @Test public void testTransViolation() throws Exception { Relative due = new Relative(2); ColoredRelative dueNeuter = new ColoredRelative(2); ColoredRelative dueNero = new ColoredRelative(2,ColoredRelative.NERO); Assert.assertFalse(dueNero.equals(dueNeuter)); Assert.assertEquals(due,dueNeuter); Assert.assertEquals(due,dueNero); }
    24. Soluzione 3
      • Equals...
      • http://www.angelikalanger.com/Articles/JavaSolutions/SecretsOfEquals/Equals.html
    25. Conclusione
      • Un costruttore non vuoto protected
      • Equals “non standard” (a slices)
    26. Evoluzione su (L.S.P)
      • Sostituibilità come “uguale comportamento” (e niente questioni di “equals”)
      • Distinzione tra metodi osservativi e metodi modificativi (tramite annotations)
      • Sollecitare l'oggetto della classe padre sui suoi metodi che ne modificano lo stato
      • Verificare che la stessa sollecitazione su oggetti di sottoclasse verifichi lo stesso comportamento “visibile” (observers)
    27. Evoluzione (equals)
      • Equivalenza (uguaglianza) cross-classing in senso più esteso (esempio: tra implementazioni differenti ma “uguali” di numeri complessi: forma polare e forma cartesiana)
      nz
    28. Links
      • www.objectmentor.com/resources/articles/ocp.pdf
      • http://www.artima.com/weblogs/viewpost.jsp?thread=4744
      • http://www.artima.com/intv/bloch17.html
      • http://www.objectmentor.com/resources/articles/lsp.pdf
      • http://www.angelikalanger.com/Articles/JavaSolutions/SecretsOfEquals/Equals-2.html

    + tonyxtonyx, 2 weeks ago

    custom

    122 views, 0 favs, 1 embeds more stats

    Un semplice esempio di open closed principle, con r more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 122
      • 120 on SlideShare
      • 2 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 1
    Most viewed embeds
    • 2 views on http://blog.excogitanet.com

    more

    All embeds
    • 2 views on http://blog.excogitanet.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories