Università degli Studi di Roma Tor Vergata Facoltà di Ingegneria Corso di Laurea in Ingegneria Informatica Programmazione orientata agli Oggetti OOP L 06b 1
L06b: Metamorfosi 2
Metamorfosi? Vuol dire che un oggetto può cambiare l insieme degli stati e/o comportamenti possibili durante la sua vita. In La metamorfosi di F. Kafka un uomo, il personaggi principale, diventa uno scarafaggio. Nell asino d oro di Apuleio (II sec.), Lucio, per la curiosità nei confronti delle pratiche magiche, viene accidentalmente trasformato in asino, invece che in uccello come avrebbe desiderato. In Le Metamorfosi di Ovidio, il matrimonio, trasforma i due sposi, le loro attese e desideri in un insieme di esperienze, di gioie e dolori condivisi. 3
What is Metamorphosis? Metamorphosis 1. a change in form, structure, or function; specifically the physical change undergone by some animals, as of the tadpole to the frog. 2. any marked change, as in character, appearance, or condition. ~ Webster s New World Dictionary, Simon & Schuster, Inc., 1979
Metamorfosi vs. OOP Le metamorfosi esistono nel mondo reale. Q1: È una delle trasformazioni elementari previste dalla OOP? Q1.1: Se no come modellarla? Per i linguaggi OOP basati su classi oggi disponibili la risposta a Q1 è NEGATIVA. 5
Example: Metamorphosis In the university, there are full time students and part time students Full time students have an expected graduation date but part time students do not Part time students may take a maximum of 3 courses where there is no maximum for full time students ParttimeStudent name address studentid maxnumcourses FulltimeStudent name address studentid graddate OOAD Using the UML - Architectural Design, v 4.2 Copyright 1998-1999 Rational Software, all rights reserved G.Calavaro
What happens if a part-time time student becomes a full-time student? OOAD Using the UML - Architectural Design, v 4.2 Copyright 1998-1999 Rational Software, all rights reserved G.Calavaro
Modeling Metamorphosis Inheritance may be used to model common structure, behavior and/or relationships to the changing parts OOAD Using the UML - Architectural Design, v 4.2 Copyright 1998-1999 Rational Software, all rights reserved G.Calavaro
Modeling Metamorphosis: One Approach A generalization relationship may be created Student name address studentid ParttimeStudent maxnumcourses FulltimeStudent graddate OOAD Using the UML - Architectural Design, v 4.2 Copyright 1998-1999 Rational Software, all rights reserved G.Calavaro 9
Modeling Metamorphosis: One Approach? : Student Manager s : Parttime Classification temp : Fulltime Classification Create ((Student(s:ParttimeClassification, ) setremaining( ) s=temp Delete(s) 10
Modeling Metamorphosis: One Approach Student name address studentid ParttimeStudentFulltimeStudent maxnumcoursesgraddate : Student Manager s : Parttime Classification temp : Fullti Classificatio Create ((Student(s:ParttimeClassification, setremaining( ) s=temp delete 11
Modeling Metamorphosis: One Approach C è di meglio! Student name address studentid ParttimeStudentFulltimeStudent maxnumcoursesgraddate : Student Manager s : Parttime Classification temp : Fullti Classificatio Create ((Student(s:ParttimeClassification, setremaining( ) s=temp delete 11
Modeling Metamorphosis: Another Approach Inheritance may be used to model common structure, behavior and/or relationships to the changing parts Student name address studentid Student name address studentid 1 1 Classification ParttimeStudent maxnumcourses FulltimeStudent graddate FulltimeClassification graddate ParttimeClassification maxnumcourses OOAD Using the UML - Architectural Design, v 4.2 Copyright 1998-1999 Rational Software, all rights reserved G.Calavaro 12
Modeling Metamorphosis: Another Approach (contd) Metamorphosis is accomplished by the object talking to the changing parts : Student Manager : Student : Parttime : Fulltime Classification Classification change to full time delete create OOAD Using the UML - Architectural Design, v 4.2 Copyright 1998-1999 Rational Software, all rights reserved G.Calavaro 13
Metamorphosis and Flexibility This 2 nd technique also adds to the flexibility of the model ResidentInformation dorm room roomkeyid 0..1 1 Student name address studentid 1 1 Classification A student may also live on campus FulltimeClassification graddate ParttimeClassification maxnumcourses OOAD Using the UML - Architectural Design, v 4.2 Copyright 1998-1999 Rational Software, all rights reserved G.Calavaro 14
Esercizio Fra i Raneidi, Il Girino - larva acquatica a respirazione branchiale e, peraltro, muta - dopo 30 gg. dalla nascita (*) si trasforma nell anfibio Rana capace, peraltro, di camminare e gracchiare, oltre che a suo modo - nuotare. Modellare la crescita di un Girino, la sua trasformazione in Rana, e scrivere un programma di test. (*) Dato tutto da verificare!!! 15
Esercizio Di una classe Persona, siano Bambino e Ragazzo due classi derivate. Persona possegga il metodo giornodicompleanno(), il quale provochi l incremento dell età dell oggetto destinatario. Un Bambino, a partire dal suo 14 o anno d età, acquisisca il comportamento boolean haiilmotorino() che restituisce true se e solo se il destinatario possiede tale mezzo di locomozione. Modellare la crescita di una Persona e scrivere un programma di test. 16