{"id":8981,"date":"2020-08-31T11:00:46","date_gmt":"2020-08-31T02:00:46","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=8981"},"modified":"2020-08-28T20:41:05","modified_gmt":"2020-08-28T11:41:05","slug":"singleton-design-pattern","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/8981","title":{"rendered":"Singleton Design Pattern"},"content":{"rendered":"\n<p>Singleton is one of the creational design pattern that allows you to ensure that a class has only one instance, while providing a global access point to this instance.<\/p>\n\n\n\n<p>Make the single instance object class responsible for creation, initialization, access, and execution. Declare the instance as a member of private static data. Provides a public static member function that encapsulates all initialization code and provides access to the instance.<\/p>\n\n\n\n<p>The client calls the access function (using the class name and the scope resolution operator) whenever a reference to the single instance is required.<\/p>\n\n\n\n<p>The singleton should be considered only if the following three criteria are met:<\/p>\n\n\n\n<p>Single instance ownership cannot reasonably be assigned<br>Lazy initialization is desirable<br>Global access is not provided otherwise<\/p>\n\n\n\n<p>Lets take a look at code samples<\/p>\n\n\n\n<p>PHP code sample<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n\/**\n * Singleton class\n *\/\nclass LogSingleton {\n    \/**\n     * I kept this attribute and constructor as private so that other sub class can't instantiate\n     * it can also be protected if you want to allow sub classes to be instantiated\n     *\/\n    private static $intance = null;\n\n    private function __construct() {\n    }\n\n    \/**\n     * I will assign the singleton instance if $instance is null.\n     * otherwise will return directly.\n     *\/\n    public static function log() {\n      if (self::$intance == null) {\n         self::$intance = new LogSingleton();\n      }\n      return self::$intance;\n    }\n\n    \/\/can also declare other public functions below.\n    public function debugLog()\n    {\n      return \"debug log\";\n    }\n  }\n\n\/\/client code\n$client = LogSingleton::log(); \/\/ this is the singleton instance.\nvar_dump($client->debugLog());\n<\/code><\/pre>\n\n\n\n<p>C# sample code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\n\nnamespace HelloWorld\n{\n    class LogSingleton\n    {\n        \/**\n         * I kept this attributes as private so that other sub class can't instantiate\n         * it can also be protected if you want to allow sub classes to be instantiated\n         *\/\n        private static LogSingleton instance;\n\n        \/**\n         * for the first use for this class, s_lock will have a object.\n         * This will later use along with lock function to protect from multi thread base.\n         *\/\n        private static readonly object s_lock = new object();\n\n        public static LogSingleton log()\n        {\n            if (instance == null)\n            {\n                \/**\n                 * The client who has s_lock will proceed this condition.\n                 *\/\n                lock (s_lock)\n                {\n                    \/**\n                     * I will assign the singleton instance if instance is null.\n                     * otherwise will return directly.\n                     *\/\n                    if (instance == null)\n                    {\n                        instance = new LogSingleton();\n                    }\n                }\n            }\n            return instance;\n        }\n\n        \/\/can also declare other public functions below.\n        public string debugLog()\n        {\n            return \"debug log\";\n        }\n\n        \/\/client code\n        class Program\n        {\n            static void Main(string[] args)\n            {\n                var client = LogSingleton.log(); \/\/ this is the singleton instance.\n                Console.WriteLine(client.debugLog());\n\n            }\n        }\n    }\n\n}\n<\/code><\/pre>\n\n\n\n<p>By Yuuma<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/8981\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/8981\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Singleton Design Pattern\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/8981\" data-text=\"Singleton Design Pattern\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/8981\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/8981\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Singleton is one of the creational design pattern that allows you to ensure that a class has only one instance [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/8981"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=8981"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/8981\/revisions"}],"predecessor-version":[{"id":8996,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/8981\/revisions\/8996"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=8981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=8981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=8981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}