Home > C# .NET > Design Pattern: Singleton in C#

Design Pattern: Singleton in C#

เรามรู้จัก design pattern กันดีกว่า เขาว่า ถ้าได้ทำความเข้าใจ ท่านจะเปลี่ยนวิถีการ ออกแบบ โปรแกรมของท่านตลอด ไป นอกจากนั้น ท่านผู้รู้ยังกล่าวว่า หากต้องการ ออกแบบโปรแกรมของท่านให้มี สถาปัตยกรรม ที่ ดีมีความยืดหยุ่นต่อการนำปใช้ และการบำรุงรักษาแล้วละก็ ต้องเรียนรู้เรื่อง Design pattern ซะ ….

นี่ก็เป็นสาเหตุหนึ่งให้ เราต้องหันมาพัฒนาตนเองต่อไป ผมก็เลยจะนำเอาเรื่องราวต่าง ๆ ที่นอกจากการ ที่เราจะเน้นหนักไปทางการเขียนแล้ว เราก็จะมองในเรื่อง ของ สถาปัตยกรรมของ ระบบด้วย ครับ นั่นเป็นสิ่งหนึ่งที่จทำให้เราส้รางโปรแกรมที่มี คุณภาพได้ สำหรับ บทความนี้ผมก็จะพูดเรื่อง design pattern ซึ่งก็คงจะพูดเป็น เรื่อง ๆ เนื่องจาก design patterns นั้นมีอยู่มากมาย และหลากหลายการใช้งาน ครับ สำหรับในบทนี้เราก็จะพูดถึง desgin pattern ที่ชื่อว่า  “singleton”

สำหรับ คำจำกัดความของ Singleton pattern นั้น ท่านผู้รู้ก็กล่าวว่า ในบางครั้งเราต้องการให้ class มีเพียง หนึ่ง instance และให้มี มีช่องทางเข้าถึง เป็น global point  access ครับ

sigleton pattern นี้เรามักจะใช้กับส่วนของระบบที่ต้องการให้ มี instance เดียวในระหว่างการทำงาน เช่น file system หรือ window manager ครับ … 🙂

รองมาดูส่วนที่เป็น code ของ class นี้ดูนะครับ


namespace DesignPatterns
{
    /// <summary>
    /// Singleton class implements that simplest version of the Singleton
    /// design pattern.
    /// </summary>
    public sealed class Singleton
    {
        private static readonly Singleton _instance = null;
        // make the default constructor private, so that no can directly create it.
        private Singleton()
        {
        }

        // public property that can only get the single instance of this class.
        public static Singleton Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new Singleton();
                return _instance

            }
        }
    }
}

จาก code จะเห็นว่า constructor เราจะกำหนดให้เป็น private เพื่อให้ ไม่ให้ส่วนอื่นในโปรแกรมสามารถที่จะสร้าง instance ของ class นี้ได้ ซึ่งเป็นการ บังคับให้เป็นไปตามข้อกำหนด ของ pattern นี้

เราจะเห็นว่าเราได้ประกาศ ตัวแปร static  ชื่อว่า _instance  เป็น member variable  ซึ่งมี datatype เป็นชนิดเดียวกับ class และ access ผ่านทาง property ชื่อ Instance  (ตัวแปร static นั้นจะถูกสร้างไว้ครั้งเดียว ครั้งแรกที่ถูกเรียกใช้)     และจะเห็นว่าตัวแปรนี้ไม่สามารถ แก้ไขได้เนื่องจากว่าถูกกำหนดให้เป็น readonly นะครับ

สุดท้ายดังที่กล่าวไป ครับ public  Instance property นั้นถูก define ไว้เพื่อสำหรับส่งค่า ของตัวแปร _instance ให้กับผู้เรียกใช้ เท่านั้น  ซึ่งถูกำหนดให้เป็น static      property นี้มีไว้เพื่อให้เป็นที่ หลักในการ เข้าถึง instance ของ class ซึ่งเพื่อให้เป็นไปตามข้อกำหนด ของ pattern นี้

สุดท้ายจริง เรารองมาดูที่ code ของการเรียกใช้ class นี้กันนะครับ

 

static void Main(stirng [] args)
{
       Singleton obj1 = Singleton.Instance;
       Singleton obj2 = Singleton.Instance;
       obj1.ToString();
       obj2.ToString();   
}

คงพอที่จะได้ idea บ้างนะครับ มีอะไรไม่เข้าใจ ก็ เมล์มาคุยได้นะครับ หรือ จะศึกษา singleton เพิ่มได้อีกตาม link เลยครับ

ธีระพงษ์ สนธยามาลย์   Senior Programmer Soft Speed Solution    s_teerapong2000@yahoo.com

Categories: C# .NET
  1. May 3, 2014 at 12:12 pm

    อธิบายได้เข้าใจง่ายและเห็นภาพเลยครับ (*__*)

    • Teerapong Sontayaman
      May 4, 2014 at 1:34 pm

      ขอบคุณครับ

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: