lezione 02 - delegation
TRANSCRIPT
![Page 1: Lezione 02 - Delegation](https://reader038.vdocuments.pub/reader038/viewer/2022110121/558950e8d8b42a12638b4658/html5/thumbnails/1.jpg)
Ingegneria del Software
![Page 2: Lezione 02 - Delegation](https://reader038.vdocuments.pub/reader038/viewer/2022110121/558950e8d8b42a12638b4658/html5/thumbnails/2.jpg)
Introduzione al pattern…
![Page 3: Lezione 02 - Delegation](https://reader038.vdocuments.pub/reader038/viewer/2022110121/558950e8d8b42a12638b4658/html5/thumbnails/3.jpg)
Ingegneria del Software - A.A. 2003/2004
Problema introduttivo (1/3)
Cosa accade alle persone che “giocano più ruoli”?Cosa accade alle persone che “giocano più ruoli”?
![Page 4: Lezione 02 - Delegation](https://reader038.vdocuments.pub/reader038/viewer/2022110121/558950e8d8b42a12638b4658/html5/thumbnails/4.jpg)
Ingegneria del Software - A.A. 2003/2004
Problema introduttivo (2/3)
L’ereditarietà non risolve il problema! L’ereditarietà non risolve il problema!
![Page 5: Lezione 02 - Delegation](https://reader038.vdocuments.pub/reader038/viewer/2022110121/558950e8d8b42a12638b4658/html5/thumbnails/5.jpg)
PatternDelegation
![Page 6: Lezione 02 - Delegation](https://reader038.vdocuments.pub/reader038/viewer/2022110121/558950e8d8b42a12638b4658/html5/thumbnails/6.jpg)
Ingegneria del Software - A.A. 2003/2004
Il pattern Delegation (1/8)
• Nome Delegation (When not to use Inheritance)[Grand98]
• Synopsis La delega è un modo per “estendere” e riusare le funzionalità di una classe senza ricorrere all’ereditarietà. La nuova classe simula l’ereditarietà utilizzando un’istanza della classe originale per fornire le funzionalità originali. A queste ne aggiunge di nuove.
![Page 7: Lezione 02 - Delegation](https://reader038.vdocuments.pub/reader038/viewer/2022110121/558950e8d8b42a12638b4658/html5/thumbnails/7.jpg)
Ingegneria del Software - A.A. 2003/2004
Il pattern Delegation (2/8)
• Context - Delegation è più generale dell’ereditarietà
- L’ereditarietà è utile per relazioni del tipo “is-a-kind-of” perchè sono intrinsecamente statiche
- In relazioni tra classi più dinamiche, tipo “is-a-role-played-by”, l’ereditarietà crea problemi
- Esempio:Sistema per la prenotazione di biglietti aerei che prevede passeggeri, personale di linea, agente di vendita biglietti aerei, ecc.
![Page 8: Lezione 02 - Delegation](https://reader038.vdocuments.pub/reader038/viewer/2022110121/558950e8d8b42a12638b4658/html5/thumbnails/8.jpg)
Ingegneria del Software - A.A. 2003/2004
Il pattern Delegation (3/8)
![Page 9: Lezione 02 - Delegation](https://reader038.vdocuments.pub/reader038/viewer/2022110121/558950e8d8b42a12638b4658/html5/thumbnails/9.jpg)
Ingegneria del Software - A.A. 2003/2004
Il pattern Delegation (4/8)
• Forces– L’ereditarietà è una relazione statica che non cambia nel
tempo– Se una classe tenta di nascondere un metodo o una
variabile ereditata da una superclasse, allora quella superclasse non dovrebbe essere estesa
– Dichiarare un classe come sottoclasse di una classe utility può essere rischioso• Estendendo una classe come Vector, quest’ultima potrebbe
cambiare nel tempo perdendo la compatibilità• I client potrebbero presupporre la superclasse
(es. cast per passaggio di parametri)• I client potrebbero richiamare i metodi pubblici della
superclasse
![Page 10: Lezione 02 - Delegation](https://reader038.vdocuments.pub/reader038/viewer/2022110121/558950e8d8b42a12638b4658/html5/thumbnails/10.jpg)
Ingegneria del Software - A.A. 2003/2004
Il pattern Delegation (5/8)
Da Context…
… a Solution
• Solution - Gli “attori” delpattern Delegation sono:
– Delegator
– Delegate
![Page 11: Lezione 02 - Delegation](https://reader038.vdocuments.pub/reader038/viewer/2022110121/558950e8d8b42a12638b4658/html5/thumbnails/11.jpg)
Ingegneria del Software - A.A. 2003/2004
Il pattern Delegation (6/8)
• Consequences- Vantaggi
-Aggira i problemi dell’ereditarietà-Facile cambiare il comportamento di un oggetto a run-time
- Svantaggi- Meno strutturata rispetto all’ereditarietà:
relazioni tra le classi meno ovvie• Implementation
- Implementazione banale• Java API usage
- Le API Java presentano molti esempi di delegation pattern.
- Alla base del modello ad eventi Java.
![Page 12: Lezione 02 - Delegation](https://reader038.vdocuments.pub/reader038/viewer/2022110121/558950e8d8b42a12638b4658/html5/thumbnails/12.jpg)
Ingegneria del Software - A.A. 2003/2004
Il pattern Delegation (7/8)
• Code example
FlightSegment FlightSegment non estende laclasse LuggageCompartment!
![Page 13: Lezione 02 - Delegation](https://reader038.vdocuments.pub/reader038/viewer/2022110121/558950e8d8b42a12638b4658/html5/thumbnails/13.jpg)
Ingegneria del Software - A.A. 2003/2004
Il pattern Delegation (8/8)
• Related Patterns - Molti pattern usano il principio di delegaEs. Decorator, Proxy, Observer.
LuggageCompartment LuggageCompartmentnon estende Vector!
![Page 14: Lezione 02 - Delegation](https://reader038.vdocuments.pub/reader038/viewer/2022110121/558950e8d8b42a12638b4658/html5/thumbnails/14.jpg)
Ingegneria del Software - A.A. 2003/2004
Domande?