Upload
frank-sons
View
527
Download
0
Tags:
Embed Size (px)
Citation preview
The Singleton Dilemma
2
What is a Singleton?
What is the problem?
What is the solution to this dilemma?
Singleton? Tell me more…
4
Probably best known design pattern
Only one instance of a class at any time
Global access to that single instance
Singleton and PHP are no friends
8
Singleton: unique instance for application
PHP: Shared-Nothing architecture
Dilemma: instance only unique for request
Singleton has even more problems
9
Dependency hiding
Globals are bad, singleton is a global
No Unit Testing possible
Solution: Dependency Injection
12
Create one instance and „inject“ it
Objects should be loosely coupled
Don‘t create objects in regular classes
„Ask, don‘t look“
Solution: Factory
14
Encapsulates object creation
Different factories for different lifetimes
Handles object lifetimes
Solution: PHP application lifetime
16
Cache or Session for „application“ lifetime
Factory can handle Cache or Session
Benefits!
18
Unit Testing is possible!
Easy object creation!
Better lifetime handling!
Easy to change dependencies!
Find us on
Bigpoint GmbHFrank Sons
Senior Software Engineer CTD
Drehbahn 47-4820354 Hamburg
Germany
Tel +49 40.88 14 13 - 0Fax +49 40.88 14 13 - 11
23