- Fotók, videók mobillal
- Samsung Galaxy S25 FE - fenséges, felejthető vagy felesleges?
- Xiaomi 17 Ultra - jó az optikája
- Brutális akkumulátort kaphat a Honor X80 GT
- iPhone topik
- Xiaomi 14T - nem baj, hogy nem Pro
- Itt a Galaxy S26 széria: az Ultra fejlődött, a másik kettő alig
- Íme az új Android Auto!
- KUGOO S1 Pro elektromos roller teszt
- Xiaomi 13 - felnőni nehéz
Új hozzászólás Aktív témák
-
Sk8erPeter
nagyúr
válasz
Peter Kiss
#9919
üzenetére
Találtam egy jó összefoglalót arról, hogy miért is NEM feltétlenül indokolt a Singletonok használata (amiről tulajdonképpen Te is beszélsz):
http://gooh.posterous.com/singletons-in-php
"Singletons are not unique snowflakes
In languages where objects live in an application server, Singletons can be used to keep memory usage low. Instead of creating two objects, you reference an existing instance from the globally shared application memory. In PHP there is no such application memory. A Singleton created in one Request lives for exactly that request. A Singleton created in another Request done at the same time will still be a completely different instance. And it will occupy it's own memory. Those instances are not linked to each other. They are completely isolated, because PHP is a Shared-Nothing architecture. You do not have one single unique instance, but many similar instances in parallel processes. Thus, one of the two main purposes of a Singleton is not applicable.Don't construct twice, it's all right
Advocates of the Singleton in PHP often argue it's still useful to be able to limit an instance within a single request. The aforementioned database classes being the most prominent example. But the much easier solution would be simply not to instantiate a second instance. If anyone can make sure there is just one instance, it's the developer. If you need to have the same instance in many classes, use Dependency Injection. Just create one, inject everywhere. That will also save you the hassle of deconstructing your Singleton once you notice you need a second instance of it all of a sudden.
Another example where Singletons are often applied but don't make sense is classes like FrontControllers. While conceptually it makes sense to say "There may be only one FrontController", it is superfluous to ensure it from an architectural viewpoint. A FrontController is usually instantiated only once in your application's control flow anyway. If you don't write a new Foo; anywhere else, you already made sure there is just one instance. So you ain't gonna need the Singleton here. Don't express concepts in your code that are never used.Don't shoot yourself in the foot
The Singleton's other purpose (to have a global access point to the instance) is undesirable in PHP. The desire for that usually stems from having an architecture where objects pull in their dependencies. Like any globals and statics, the Singleton's getInstance() method creates coupling to the global scope. This makes Unit-Testing harder. There is ways to mitigate this, but in general, the cost to mitigate is higher than to simply avoid the Singleton in favor of Dependency Injection. This is especially true in those situations, where the Singleton is applied but never instantiated twice anyway."Itt van egy hosszabb témázás magyarul erről:
http://weblabor.hu/blog/20100727/php-egyke-ososztaly============
Hadd mondjak ellenérvet is (már úgyis megszokhattátok, hogy ez a rész is mindig jön a hsz.-eimben
):
[link]
Pont ez jutott nekem is eszembe elsőként, amit itt írnak az elfogadott válaszban, hogy pl. egy Logging class esetén tipikusan jól használható egy Singleton osztály, mert ott felesleges tesztelésekről beszélni (valszeg nem a naplózásért felelős osztály a legfontosabb, amit tesztelni kellene), plusz teljesen elfogadható lehet ennek a mintának az alkalmazása, mert más módon szépen összehozni a rendszerrel feleslegesen macerás lehet.Saját példa az, hogy bizonyos esetekben csak nagyon macerás módon, adott esetben a teljes rendszer újratervezésével lehetne normálisan beépíteni a rendszerbe egy több helyen szükséges változó Singleton nélküli használatát - tipikusan olyan rendszerekre gondolok, ahol jelenleg még nem elsősorban az OOP-szemléletet követik, hanem egyelőre inkább globális függvényekét (bár van elmozdulás az OOP irányába): mint a Drupal.
Más megoldás nyilván a globális változók használata, ami tulajdonképpen hasonló lehet a Singletonokhoz, de kicsit mégis más. Pl. Drupalnál szükségem volt már ilyenre: [link].A másik: csak hogy egy kicsit pontosítsunk, a Singleton-osztályok használata elsősorban PHP-nél felesleges. Aztán más nyelveknél vannak bőven kivételek, mint pl. egy ablakkezelő objektum használata.
Ettől függetlenül tényleg fennáll, hogy a lehető legritkább esetekben szabad használni, alapvetően a már említett szempontok miatt kerülendő. Főleg PHP-nél (lásd az idézett cikket).
===
DE további ajánlott linkek:
http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons
Új hozzászólás Aktív témák
- Manli RTX 3070 8GB LHR / Csavarmatricás / Beszámítás OK! / Akciós ár!
- Apple Watch 4 44mm szürke színben. 74% akkumulátor.
- Canon EF-S 24mm f/2.8 STM objektív
- Dell Inspiron 16 Plus 7640 Ultra 7 / RTX 4060 16gb DDR5 1TB SSD/2.5K 120Hz Garancia
- Canon 60D fényképezőgép + 50mm objektív + 16GB memóriakártya
- HIBÁTLAN iPhone 17 Pro Max 256GB Deep Blue-1 ÉV GARANCIA - Kártyafüggetlen, 100% Akksi
- ÁRGARANCIA!Épített KomPhone Ryzen 7 5700X 16/32/64GB RAM RTX 5060 Ti 16GB GAMER termékbeszámítással
- HIBÁTLAN iPhone 14 Plus 128GB Midnight-1 ÉV GARANCIA - Kártyafüggetlen, MS4472
- Honor Magic6 Pro / 12/512GB / Kártyafüggetlen / 12Hó Garancia
- Telefon felvásárlás!! Apple iPhone SE (2016), Apple iPhone SE2 (2020), Apple iPhone SE3 (2022)
Állásajánlatok
Cég: Laptopműhely Bt.
Város: Budapest

):
