ZengCode.Com (The Thai Php Framework)  


Home   Download   Manual   About us    

Facebook   


MAIN MENU
News
Php Tips
Android Programming
Design Pattern By PHP
C# using Linq น่าใช้จริงๆ
C# Tips & Technique
C# Design Pattern
Linux Quick Tips
Java & JavaScript Tips
Database & SQL
ZengCode Framework Guide
Zeng Code Code
Programming
IPhone (Tips and Trick)

Download เอกสารที่น่าสนใจ

     Singleton Pattern   (2010-03-15)

ทำความรู้จักกับ Design Pattern

Gang of Four (GOF) patterns นั้นถือว่าเป็นพื้นฐานของ design patterns อื่น ๆ ทั้งหมด  ได้มีการแยกแยะออกเป็น กลุ่ม ได้ 3 กลุ่ม คือ   Creational  Structural และ Behavioral ซึ่งผมจะกล่าวถึง design pattern หมดในแต่ละกลุ่มโดยละเอียด – ดังนี้

Creational Patterns
- Abstract Factory  
- Builder  
- Factory Method  
- Prototype  
- Singleton  
Structural Patterns
- Adapter  
- Bridge  
- Composite  
- Decorator  
- Facade  
- Flyweight  
- Proxy  
Behavioral Patterns
- Chain of Resp.  
- Command  
- Interpreter  
- Iterator  
- Mediator  
- Memento  
- Observer  
- State  
- Strategy  
- Template Method  
- Visitor

Singleton Pattern คือ การจำกัดจำนวน object ที่สร้างจาก class บางตัวเพียงหนึ่งตัวหรือตามจำนวนที่ต้องการ
เพื่อเป็นการจำกัดหรือควบควม resource บางอย่างของระบบ

มีวิธีการหลักๆ ดังนี้ครับ

1. Class Constructor ต้องเป็นมี accessiblility เป็น private

  class TestSingleTon
    {
        private static TestSingleTon instant;

        private TestSingleTon() {}

....................................................................

 2.เมื่อเราจะสร้าง Constructor เป็นแบบ private การสร้าง Object โดย new จะใช้ไม่ได้จะต้องมี Method ในการสร้าง instant ขึ้นมาเช่น
และต้องมีตัวแปรที่เป็น private เพื่อควบคุมการสร้าง object เช่นถ้ามีการสร้างแล้วไม่ให้สร้างอีก หรือควบคุมจำนวน object ในตัวอย่างนี้ให้มีการสร้าง
object ได้ครั้งละ 1 object เท่านั้น

  class TestSingleTon
    {
        private static TestSingleTon instant; //อาจจะเป็น couter นับจำนวน object ที่จะให้สร้างได้พร้อมกัน

        private TestSingleTon() {}

        public static TestSingleTon GetTestSingleTon()
        {
            if (instant == null)
            {
                instant = new TestSingleTon();
                return instant;
            }
            return null;
        }

.................................................

 3.อาจจะมีตัว method ในการคืน resouce เพื่อให้สามารถสร้าง object ใหม่ได้อีกจากคลากนี้ จากตัวอย่างผมสร้าง method

Release ในการปลดปล่อย

  public void Release(ref TestSingleTon obj)
        {
            instant = null;
            obj = null;
        }

 ตัวอย่าง Class ทังหมด

  class TestSingleTon
    {
        private static TestSingleTon instant;

        private TestSingleTon() {}

        public static TestSingleTon GetTestSingleTon()
        {
            if (instant == null)
            {
                instant = new TestSingleTon();
                return instant;
            }
            return null;
        }

        public String SayHello()
        {
            return "Hello World";
        }

        public void Release(ref TestSingleTon obj)
        {
            instant = null;
            obj = null;
        }

    }//class

 วิธีการเรียกใช้นะครับ

 TestSingleTon sg = TestSingleTon.GetTestSingleTon();
            if (sg != null)
            {
                MessageBox.Show("1."+sg.SayHello());
                sg.Release(ref sg);
            }

            sg = TestSingleTon.GetTestSingleTon();
            if (sg != null)
                MessageBox.Show("2." + sg.SayHello());

            sg = TestSingleTon.GetTestSingleTon();
            if (sg != null)
                MessageBox.Show("3." + sg.SayHello());

 จากตัวอย่าง Message ที่ 1 และ 2 เท่านั้นที่จะแสดงผล

ลองทำความเข้าใจและเอาไปประยุกต์ใช้ในการทำ Software Architecture ของท่านดูนะครับ รับรองว่า ไฮโซ ขึ้นเยอะ หุหุ


Comment
Name
Comment
Security CodeCAPTCHA Image

easy tracking
avis car rental discount code

This page took 0.137944 seconds to load.