Hirdetés
- TCL LCD és LED TV-k
- Intel Core i5 / i7 / i9 "Alder Lake-Raptor Lake/Refresh" (LGA1700)
- OLED monitor topic
- Maduro szerint venezuelai gyerekek gyártják majd az NVIDIA chipjeit
- VR topik
- Projektor topic
- HiFi műszaki szemmel - sztereó hangrendszerek
- AMD Ryzen 9 / 7 / 5 7***(X) "Zen 4" (AM5)
- AMD vs. INTEL vs. NVIDIA
- NVIDIA GeForce RTX 5080 / 5090 (GB203 / 202)
Ú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
- Big in Japan - japán játékokról, kötetlenül
- TCL LCD és LED TV-k
- Tőzsde és gazdaság
- Parfüm topik
- Intel Core i5 / i7 / i9 "Alder Lake-Raptor Lake/Refresh" (LGA1700)
- GoodSpeed: Te hány éves vagy?
- Hálózati / IP kamera
- Sega, Nintendo - retro konzolok
- Villanyszerelés
- CES 2026: Nem folyt ki a szemünk a TCL papírképernyőit bámulva
- További aktív témák...
- G15 5530 15.6" FHD IPS i7-13650HX RTX 4060 16GB 512GB NVMe új akku gar
- Eladó Jamo SW 410E
- Telefon tokok, telefon fóliák kedvezményes áron!
- Dell latitude 5330 i5-12.gen/8gb ddr4/250gb ssd/magyar Bill./új akku (Esztétikai hibás)
- Erőmű Magyar! Lenovo P16 ( Core I9 12950HX 32Gb DDR4 512Gb SSD Nvidia RTX 4Gb video) laptop!
- Eladó Apple iPhone 13 Mini 128GB / 12 hó jótállás
- GYÖNYÖRŰ iPhone 12 Mini 128GB Green-1 ÉV GARANCIA -Kártyafüggetlen, MS4169, 100% Akksi
- Targus DOCK423A - USB-C Dual HDMI 4K HUB - 2 x HDMI (120Hz)
- HIBÁTLAN iPhone 13 Pro 128GB Alphine Green -1 ÉV GARANCIA - Kártyafüggetlen, MS3024
- ÁRGARANCIA!Épített KomPhone Ryzen 7 9800X3D 32/64GB RAM RTX 5070Ti 16GB GAMER PC termékbeszámítással
Állásajánlatok
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest
Cég: Laptopszaki Kft.
Város: Budapest
):

