SlideShare a Scribd company logo
1 of 28
Open Closed T. software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification
Esempio ,[object Object]
Esempio ,[object Object]
Ipotizziamo alcune soluzioni
Esempio ,[object Object]
Ipotizziamo alcune soluzioni
Ipotizziamo una estensione (del dominio)
Esempio ,[object Object]
Ipotizziamo alcune soluzioni
Ipotizziamo una estensione (del dominio)
Quale soluzione tra quelle proposte è “Open Closed” (ovvero consente l'estensione senza necessitare di modifiche)?
Dominio? ,[object Object]
@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
Aspettative @Test public void zeroEqualsZero() { Natural zero = new Natural(0); Natural anotherZero = new Natural(0); Assert.assertEquals(zero,anotherZero); }
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; }
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; }
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
Estensione ,[object Object]
“sì alle estensioni!  no alle modifiche!” public class Relative  extends  Natural { @Pre // precondizione rilassata public Relative(int value) { this.value = value; } .... }
“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.)
Correlazioni-osservazioni ,[object Object]
Correlazioni-osservazioni ,[object Object]
Natural  sotto insieme di Relative (o, che è lo stesso, è  isomorfo  ad un suo sottoinsieme)
Correlazioni-osservazioni ,[object Object]
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))
Correlazioni-osservazioni ,[object Object]
Natural  sotto insieme di Relative (o, che è  quasi  lo stesso, è  isomorfo  ad un suo sottoinsieme)

More Related Content

Viewers also liked

авиаперевозки Clients 2009
авиаперевозки Clients 2009авиаперевозки Clients 2009
авиаперевозки Clients 2009Pavel Red'ko
 
Social Media SEO for Business
Social Media SEO for BusinessSocial Media SEO for Business
Social Media SEO for BusinessHeidi Allen
 
Business Continuity from PAETEC
Business Continuity from PAETECBusiness Continuity from PAETEC
Business Continuity from PAETECTracey Gaffney
 
Trapper School Grad P P
Trapper  School  Grad  P PTrapper  School  Grad  P P
Trapper School Grad P Pnsbsd
 
Socialroi altimiter group
Socialroi altimiter groupSocialroi altimiter group
Socialroi altimiter groupTimothy Hooker
 
Presentation delivery tips and techniques
Presentation delivery tips and techniquesPresentation delivery tips and techniques
Presentation delivery tips and techniquesLee Bandy
 
Resumen de la pag. 1 a la 32 "Todo es teoría"
Resumen de la pag. 1 a la 32  "Todo es teoría"Resumen de la pag. 1 a la 32  "Todo es teoría"
Resumen de la pag. 1 a la 32 "Todo es teoría"laura68
 
Blogger & Facebook for FoL
Blogger & Facebook for FoLBlogger & Facebook for FoL
Blogger & Facebook for FoLjulid
 
Networking And Network Etiquette
Networking And Network EtiquetteNetworking And Network Etiquette
Networking And Network EtiquetteJMULLINMBA
 
Conception To 8 Weeks
Conception To 8 WeeksConception To 8 Weeks
Conception To 8 Weekscdbb
 
Vakfotografie Sonny Lips
Vakfotografie Sonny LipsVakfotografie Sonny Lips
Vakfotografie Sonny LipsSonny1967
 
Interagency Coordination of Biometrics and Forensic RDT&E
Interagency Coordination of Biometrics and Forensic RDT&EInteragency Coordination of Biometrics and Forensic RDT&E
Interagency Coordination of Biometrics and Forensic RDT&EDuane Blackburn
 
2010.05 dil publication
2010.05 dil publication2010.05 dil publication
2010.05 dil publicationPavel Red'ko
 

Viewers also liked (20)

Meet yolanda
Meet yolandaMeet yolanda
Meet yolanda
 
авиаперевозки Clients 2009
авиаперевозки Clients 2009авиаперевозки Clients 2009
авиаперевозки Clients 2009
 
Social Media SEO for Business
Social Media SEO for BusinessSocial Media SEO for Business
Social Media SEO for Business
 
Diplomas 2009 - 2010
Diplomas 2009 - 2010Diplomas 2009 - 2010
Diplomas 2009 - 2010
 
Business Continuity from PAETEC
Business Continuity from PAETECBusiness Continuity from PAETEC
Business Continuity from PAETEC
 
Intrumens medivals
Intrumens medivalsIntrumens medivals
Intrumens medivals
 
Trapper School Grad P P
Trapper  School  Grad  P PTrapper  School  Grad  P P
Trapper School Grad P P
 
Socialroi altimiter group
Socialroi altimiter groupSocialroi altimiter group
Socialroi altimiter group
 
Presentation delivery tips and techniques
Presentation delivery tips and techniquesPresentation delivery tips and techniques
Presentation delivery tips and techniques
 
Resumen de la pag. 1 a la 32 "Todo es teoría"
Resumen de la pag. 1 a la 32  "Todo es teoría"Resumen de la pag. 1 a la 32  "Todo es teoría"
Resumen de la pag. 1 a la 32 "Todo es teoría"
 
Blogger & Facebook for FoL
Blogger & Facebook for FoLBlogger & Facebook for FoL
Blogger & Facebook for FoL
 
参与感 00序言 jason_laipmp
参与感 00序言 jason_laipmp参与感 00序言 jason_laipmp
参与感 00序言 jason_laipmp
 
Networking And Network Etiquette
Networking And Network EtiquetteNetworking And Network Etiquette
Networking And Network Etiquette
 
What’s new on f15
What’s new on f15What’s new on f15
What’s new on f15
 
Conception To 8 Weeks
Conception To 8 WeeksConception To 8 Weeks
Conception To 8 Weeks
 
Vakfotografie Sonny Lips
Vakfotografie Sonny LipsVakfotografie Sonny Lips
Vakfotografie Sonny Lips
 
Ageing consumers
Ageing consumersAgeing consumers
Ageing consumers
 
Interagency Coordination of Biometrics and Forensic RDT&E
Interagency Coordination of Biometrics and Forensic RDT&EInteragency Coordination of Biometrics and Forensic RDT&E
Interagency Coordination of Biometrics and Forensic RDT&E
 
2010.05 dil publication
2010.05 dil publication2010.05 dil publication
2010.05 dil publication
 
02 portfolio
02 portfolio02 portfolio
02 portfolio
 

esempio open closed

  • 1. Open Closed T. software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification
  • 2.
  • 3.
  • 5.
  • 8.
  • 10. Ipotizziamo una estensione (del dominio)
  • 11. Quale soluzione tra quelle proposte è “Open Closed” (ovvero consente l'estensione senza necessitare di modifiche)?
  • 12.
  • 13. @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
  • 14. Aspettative @Test public void zeroEqualsZero() { Natural zero = new Natural(0); Natural anotherZero = new Natural(0); Assert.assertEquals(zero,anotherZero); }
  • 15. 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; }
  • 16. 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; }
  • 17. 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
  • 18.
  • 19. “sì alle estensioni! no alle modifiche!” public class Relative extends Natural { @Pre // precondizione rilassata public Relative(int value) { this.value = value; } .... }
  • 20. “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.)
  • 21.
  • 22.
  • 23. Natural sotto insieme di Relative (o, che è lo stesso, è isomorfo ad un suo sottoinsieme)
  • 24.
  • 25. Natural sotto insieme di Relative (o, che è quasi lo stesso, è isomorfo ad un suo sottoinsieme)
  • 26. Morfismo : (astrazione di un processo che trasforma una struttura astratta in un'altra mantenendo alcune caratteristiche &quot;strutturali&quot; della prima (wikipedia))
  • 27.
  • 28. Natural sotto insieme di Relative (o, che è quasi lo stesso, è isomorfo ad un suo sottoinsieme)
  • 29. Morfismo : (astrazione di un processo che trasforma una struttura astratta in un'altra mantenendo alcune caratteristiche &quot;strutturali&quot; della prima (wikipedia))
  • 30. “Is A” != “sottoClasse di” (ricordate “Quadrato-Rettangolo”?)
  • 31. 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
  • 32. 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?)
  • 33. 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
  • 34. 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); } ...
  • 35. 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); }
  • 36.
  • 38.
  • 40.
  • 41. Distinzione tra metodi osservativi e metodi modificativi (tramite annotations)
  • 42. Sollecitare l'oggetto della classe padre sui suoi metodi che ne modificano lo stato
  • 43. Verificare che la stessa sollecitazione su oggetti di sottoclasse verifichi lo stesso comportamento “visibile” (observers)
  • 44.
  • 45.