SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Downloaden Sie, um offline zu lesen
TPA 2015/2016
Creational patterns
Factory method, singleton
Renato Grappone
Sergiy Shevchenko
PRIMA DI PARTIRE
PRIMA DI PARTIRE
Qual'è il problema nell'utilizzare un costruttore al momento
opportuno?
Interfaccia
Classe concreta

implementa interfaccia "Pizza"
IDENTIFICHIAMO IL PROBLEMA
IDENTIFICHIAMO IL PROBLEMA
cambia anche la ricetta
il tipo di pizza può cambiare
IDENTIFICHIAMO IL PROBLEMA
parte del codice che varia
nel tempo
la preparazione non cambia
INCAPSULAMENTO
viene spostato
dall'orderPizza()
SimplePizzaFactory
e diventa una Factory
CREANDO SIMPLE PIZZA FACTORY
La classe SimplePizzaFactory ha l'unico scopo - creare le pizze
codice spostato dall'ordinaPizza()
PAUSA DI RIFLESSIONE
• Quali sono i vantaggi? Non abbiamo
semplicemente spostato il problema dal metodo
orderPizza() nel createPizza()?



Ora SimpleFactory può essere utilizzata dove c'è
bisogno della istanziazione di una pizza. Un esempio
può essere PizzaShopMenu oppure HomeDelivery.

PAUSA DI RIFLESSIONE
• Il metodo createPizza() può essere dichiarato
statico?



Si, si chiama "static factory" ed il vantaggio è ben
evidente, non c'è bisogno di istanziare la
SimplePizzaFactory. Ma ha anche degli svantaggi: non
è possibile estendere la fabbrica per cambiare il
comportamento del metodo
COSTRUENDO LA PIZZERIA
Pizzeria prende come

parametro del
costruttore
una fabbrica
Creazione della pizza
ora avviene nella fabbrica
"fabbrica"
interfaccia
"cliente"
varie implementazioni
ABBIAMO
DEFINITO UNA
FABBRICA
ASTRATTA?
No! SimplePizzaFactory è
solamente una idioma, una
parte del design pattern
Abstract Factory.Arriveremo
al quadro completo tra poco...
Si
CAMBIO DEI REQUISITI
La pizzeria, prepara gli oggetti (pizze) davvero ottime, quindi il
proprietario decide di espandere la sua rete, creando una
catena di pizzerie.
Pizzeria
Le pizze vengono

preparate nello stesso modo
SalernoFactory
MilanoFactory
CAMBIO DEI REQUISITI
Fabbrica tipica milanese
Il procedimento di
preparazione è sempre
LO STESSO
Lo stesso procedimento
porta ai vincoli nella
composizione!
FACTORY METHOD

PATTERN
... come altri pattern creazionali, esso indirizza il problema della
creazione di oggetti senza specificarne l'esatta classe. Questo pattern
raggiunge il suo scopo fornendo un'interfaccia per creare un oggetto,
ma lascia che le sottoclassi decidano quale oggetto istanziare.
FACTORY METHOD
class diagramm
FACTORY METHOD
• Dobbiamo dare la
possibilità alle sottoclassi
della classe "Pizzeria"
decidere la preparazione
delle pizze.
creaPizza è un metodo
astratto, quindi deve
essere per forza
implementato dalle
sottoclassi
RITORNANDO ALLA PIZZERIA
la sottoclasse decide

come creare la pizza
creazione avviene

nel metodo astratto

dell'istanza
FACTORY METHOD
RITORNANDO ALLA PIZZERIA
"override" del metodo
di creazione della pizza
Per un'altra pizzeria
le pizze possono
essere completamente

diverse
COSA ABBIAMO FATTO?
Istanziamo la pizzeria
Facciamo un ordine
pizzeriaSa, nel metodo ordinePizza invoca creaPizza
Metodi di preparazione sono definiti nei factory methods,
quindi sono sovrascritti dalle sottoclassi
DOMANDE?
ESEMPIO IN
SINGLETON PATTERN
SINGLETON CLASS DIAGRAMM
l'istanza statica

della classe stessa
costruttore privato
metodo statico
restituisce singleton
SCOPO ED IMPLEMENTAZIONE
Il singleton è un design pattern creazionale che ha lo scopo di
garantire che di una determinata classe venga creata una e una
sola istanza, e di fornire un punto di accesso globale a tale istanza.
L'implementazione più semplice di
questo pattern prevede che la classe
singleton abbia un unico costruttore
privato, in modo da impedire
l'istanziazione diretta della classe. La
classe fornisce inoltre un metodo
"getter" statico che restituisce una
istanza della classe (sempre la stessa),
creandola preventivamente o alla prima
chiamata del metodo, e
memorizzandone il riferimento in un
attributo privato anch'esso statico.
PYTHONIC SINGLETON
Q&A

Weitere ähnliche Inhalte

Empfohlen

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Design patterns: Creational patterns