Hirdetés
- Vivo X300 Ultra - tárcsázz, ha van rá keret!
- One mobilszolgáltatások
- Íme az új Android Auto!
- Yettel topik
- MWC 2026: Bajnoki címre pályázik a Xiaomi Watch 5
- Samsung Galaxy S24 Ultra - ha működik, ne változtass!
- Milyen okostelefont vegyek?
- Samsung Galaxy S26 Ultra - fontossági sorrend
- Android alkalmazások - szoftver kibeszélő topik
- Xiaomi 15T Pro - a téma nincs lezárva
Ú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
- Idén télen jön a most bemutatott Metro 2039
- GTA V
- Vivo X300 Ultra - tárcsázz, ha van rá keret!
- Battlefield 6
- The Division 2 (PC, XO, PS4)
- Linux kezdőknek
- Automata kávégépek
- Intel Core Ultra 3, Core Ultra 5, Ultra 7, Ultra 9 "Arrow Lake" LGA 1851
- LG LCD és LED TV-k
- Kávé kezdőknek - amatőr koffeinisták anonim klubja
- További aktív témák...
- Lenovo Thinkpad P50 15,6" FHD IPS kijelző, I7-6820HQ CPU, 16GB DDR4, 512GB NVMe SSD, 4GB Quadro VGA,
- HP 15,6",Core i7 1165G7(8x4,8Ghz)IRIS XE VGA,FullHD IPS,16-48GB RAM/256-512SSD,szép állapot
- MSI GTX 560 Ti Twin Frozr II OC 1 GB GDDR5 VGA
- RYZEN 7 5800X +hűtött VRM-es A520M/B550/X570 lap +16GB hűtőbordás DDR4 kit! GAR/SZÁMLA (Te nevedre)!
- ASUS ROG Strix Z790-E Gaming WiFi II garanciás, hibátlan, full doboz
- HIBÁTLAN iPhone 15 Plus 128GB Blue -1 ÉV GARANCIA - Kártyafüggetlen, MS4504
- ÁRGARANCIA! Épített KomPhone i5 12400F 16/32/64GB RAM RX 9060 XT 16GB GAMER PC termékbeszámítással
- HP ProBook 445 G10 14" Ryzen 5 7530U, 16GB RAM, 512GB SSD, jó akku, számla, 6 hó gar
- AKCIÓ! Sapphire Nitro+ AMD RX 7900 XTX 24GB videokártya garanciával hibátlan működéssel
- Logitech MX Master 3S for MAC
Állásajánlatok
Cég: Laptopműhely Bt.
Város: Budapest
):
