เมนูนำทาง
ซิงเกิลตันแพตเทิร์น ตัวอย่างโค้ดโค้ดตัวอย่างการใช้คลาสชิงเกิลตันในภาษาซีชาร์ปด้วยวิธีดอตเน็ต
sealed class Singleton { private Singleton() {} public static readonly Singleton instance = new Singleton();}
ซอร์สโค้ดคลาสซิงเกิลตันในภาษาจาวา
public class Singleton { private static Singleton instance = null; private Singleton() {} public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } public void doWork() { // do something }}
การเรียกใช้งาน
Singleton.getInstance().doWork();
คลาสซิงเกิลตันแบบปลอดภัยในการทำงานแบบ multi-threading โดย Bill Pugh
public class ThreadSafeSingleton { private ThreadSafeSingleton() { } private static class SingletonHolder { private static ThreadSafeSingleton instance = new ThreadSafeSingleton(); } public static ThreadSafeSingleton getInstance() { return SingletonHolder.instance; }}
<?phpclass Singleton{ private static $single; private function __construct() { } public static function getInstance() { if (self::$single === null) { self::$single = new Singleton(); } return self::$single; } public function doWork() { // do something } public function __wakeUp(){ } public function __clone(){ }}?>
การเรียกใช้งาน
Singleton::getInstance()->doWork();
เมนูนำทาง
ซิงเกิลตันแพตเทิร์น ตัวอย่างโค้ดใกล้เคียง
ซิงเกิลเลดีส์ (พุตอะริงออนอิต) ซิงเกิลประจำตัวละครในเค-อง! ก๊วนดนตรีแป๋วแหวว ซิงเกิล ซิงเกิลนิวคลีโอไทด์โพลีมอร์ฟิซึม ซิงเกิลเลดี้ เพราะเคยมีแฟน ซิงเกิลตัน ซิงเกิลสุดท้าย ซิงเกอร์ คอร์ปอเรชั่น ซิงเกิลคอยล์ ซิงเกิลตันแพตเทิร์นแหล่งที่มา
WikiPedia: ซิงเกิลตันแพตเทิร์น http://www.javaworld.com/javaworld/jw-04-2003/jw-0... http://msdn2.microsoft.com/en-us/library/ms954629....