Archive

Posts Tagged ‘Delegates’

Delegates and Events ตอนที่ 3

สวัสดีครับ คิดว่าคงมี สักคนที่ได้มีโอกาสอ่าน blog ผม ก็เลยสวัสดีไปงั้นแหละ สำหรับตอนนี้เป็น การพูดถึง delegates ต่อจากตอนที่แล้ว ครับ ซึ่งเราคงจะได้เข้าใจการใช้งาน delegates ไม่มากก็น้อย สำหรับในตอนนี้ จะพูดถึงเรื่อง Multicast delegates ครับ  delegates สามารถที่จะอ้างอิง function หรือ method ได้มาก กว่าหนึ่งนะครับนี่ นั่นก็คือ delegates object หนึ่งตัวสามารถที่จะอ้างอิง ไปยัง หลาย ๆ  method และเมื่อเรียก delegates นั้นก็ทำให้ method ที่มันอ้างอิงอยู่ ทำงานในคราวเดียวกัน ครับ

ครับจากที่ได้กล่าวมา ข้างต้น  delegates นั้นที่จริงแล้วเป็น multicast ก็หมายถึงโดยปกติแล้วมันสามารถที่จะชี้ไปยัง function หรือ method ได้หลาย method หรือ function ในคราวเดียวกัน  Base case ของ multicast delegates ก็คือ System.MulticastDelegate ครับ   ดังนั้น การเรียก delegates object ครั้งเดียวก็สามารถที่จะสั่งให้ หลาย method ทำงานได้ แต่ต้องไม่ลืมนะครับว่า Method เหล่านั้นต้องเป็นไปตามข้อกำหนดของ delegates นะครับ นี้สำคัญ นะครับ 🙂

<pre>using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
public class MyClass
{
// Declare a delegate that takes a single string parameter
// and has no return type.
public delegate void LogHandler(string message);

// The use of the delegate is just like calling a function directly,
// though we need to add a check to see if the delegate is null
// (that is, not pointing to a function) before calling the function.
public void Process(LogHandler logHandler)
{
if (logHandler != null)
{
logHandler("Process() begin");
}

if (logHandler != null)
{
logHandler("Process() end");
}
}
}

// The FileLogger class merely encapsulates the file I/O
public class FileLogger
{
FileStream fileStream;
StreamWriter streamWriter;

// Constructor
public FileLogger(string filename)
{
fileStream = new FileStream(filename, FileMode.Create);
streamWriter = new StreamWriter(fileStream);
}

// Member Function which is used in the Delegate
public void Logger(string s)
{
streamWriter.WriteLine(s);
}

public void Close()
{
streamWriter.Close();
fileStream.Close();
}
}

// Test Application which calls both Delegates
public class TestApplication
{
// Static Function which is used in the Delegate
static void Logger(string s)
{
Console.WriteLine(s);
}

static void Main(string[] args)
{
FileLogger fl = new FileLogger("process.log");

MyClass myClass = new MyClass();

// Crate an instance of the delegates, pointing to the static
// Logger() function defined in the TestApplication class and
// then to member function on the fl instance of a FileLogger.
MyClass.LogHandler myLogger = null;
myLogger += new MyClass.LogHandler(Logger);
myLogger += new MyClass.LogHandler(fl.Logger);

myClass.Process(myLogger);
fl.Close();
}
}

}

ครับ จากตัวอย่าง ใน class MyClass นั้นยังคงเดิมนะครับ จริง ๆ แล้วส่วนใหญ่คงเดิมเพียงแต่เราเพิ่ม Method  ใน class TestApplication ชื่อ Logger(string s) ครับซึ่งประกาศสอดคล้อง กับ delegates ของเรานะครับ    ส่วนใน Class FileLogger นั้น มี method ชื่อเดียวกัน Logger(string s) แต่จะเขียน string ลงไฟล์นะครับ ไม่ต้อง งงนะครับเพราะ ต่าง class กันสามารถมี method ชื่อเดียวกันได้

มาดูที่การนำไปใช้ดีกว่านะครับ ใน Main method นั้น การที่เรียกใช้ Logger ใน FileLogger class จะต้องทำผ่าน object ใช่ไหมครับ ดังนั้นก็จะต้อง สร้าง object ของ FileLogger ขึ้นมานะครับ

FileLogger fl = new FileLogger(“process.log”);

สำหรับการ instantiate จะต้องกำหนด ชื่อ file ให้กับ constructor ด้วยเรากำหนด ให้เป็นชื่อ process.log นะครับหลังจากนี้เราก็สามารถเข้าถึง Logger ของ FileLogger Object ได้แล้วโดยอ้างผ่านทางตัวแปร object  แบบนี้นะครับ  fl.Logger (ทุกคนคงรู้แล้ว) แต่การอ้างอิง ไปยัง Logger method ใน TestApplication class นั้นไม่ต้องผ่าน object ใช่ไหมครับเพราะว่ามันเป็น อะไร นะครับ  ครับใช่แล้ว มันเป็น static  method เราก็อ้างอิงผ่านชื่อ class ได้เลย แต่ในคราวนี้ function Main กับ Logger นั้นอยู่ใน class เดียวกันเราก็ไม่ต้อง ใช้ชื่อ class นำหน้า นะครับ

มาดูที่ ตัวแปร delegates ดีกว่านะครับ เนื่องจากว่า delegates ของเราประกาศไว้ใน class ที่ชื่อว่า  MyClass ดังนั้นการอ้าอิง type ก็ต้องอ้างผ่าน ชื่อ Class นะครับ อย่าง code ที่เราใช้ประกาศ ตัวแปร delegates ก็เป็นดังนี้

MyClass.LogHandler myLogger = null;

เป็นการ declare ไว้ก่อนนะครับยังไม่ได้ชี้ไปที่ไหนเลย หลังจากนั้นจึงค่อย ๆ เพิ่ม method เข้าไปที่ละตัวดังนี้

myLogger += new MyClass.LogHandler(Logger);
myLogger += new MyClass.LogHandler(fl.Logger);

ค่อนข้างจะ make sense นะครับ คล้าย ๆ กับ การใช้ operator ในการเพิ่มค่าแต่ว่าไม่ได้เป็นการเพิ่มค่านะครับเป็นการเพิ่ม address เข้าไปยัง address list ที่ ตัวแปร delegates หรือ delegate object ชี้อยู่นั่นเอง แล้วหลังจากนั้น เราก็ส่ง myLogger ให้เป็น argument ของ fl.Process()

fl.Process(myLogger);

ทำให้การทำเรียก myLogger ครั้งนี้สั่งให้ method Logger และ fl.Logger ทำงานครับ เป็นไงครับ  Multicast delegates พอจะได้ไหมครับ

เพิ่มเติมอีกนิดนะครับ นอกจากเราจะใช้ Operator += แล้วเรายังสามารถใช้ Operator -= ได้ด้วยนะครับ ใช้อย่างไรนะเหรอครับ แบบนี้ครับ

myLogger -= new MyClass.LogHandler(Logger);

แล้วลอง เรียก fl.Process(myLogger) ดูนะครับว่าผลมันจะเป็นอย่างไร :))

เอาละครับ คงเอาไว้แค่นี้ก่อนะครับ มันยาวมากไปก็ดี ผมก็ขยันมาก คนอ่านก็คงขยันพอ ๆ กับผมใช่ไหมครับ  ฮะ ฮะ ฮะ ต่อไปจะพูดเรื่อง Event นะครับ

Delegates and Events ตอนที่ 1

Class ต่าง ๆ เป็น Reference type  หมายถึง ตัวแปรของ Obeject ของ Class นั้น ไม่ได้อ้างอิงไปยัง Object โดยตรง แต่ชี้ไปยังที่ตำแหน่งใน Address ของ Object แทน ซึ่งทำให้เราสามารถใช้งาน Object ในรูปแบบพิเศษได้มากมาย  Delegates ก็เป็นตัวแปรอีกชนิดหนึ่งที่เป็นลักษณะ Reference type  Deletgates นั้น สามารถที่จะชี้ไปยัง Method หรือ Reference ไปยัง Method ได้ในขณะ runtime  นั่นก็หมายถึงเราสามารถ ที่จะเลือกได้ว่า เราจะ สั่ง Method ไหนทำงานก็ได้ ในขณะ runtime  ขึ้นอยู่กับ ความต้องการในขณะนั้น

ในส่วนนี้ผมจะพูดถึงเรื่อง การสร้าง Deletgates  การทำ multicast delegates และ วิธีการใช้ event กับ delegates

Deletgates เป็นตัวแปร แบบ Reference type สามารถที่จะอ้างอิงไปยัง method ใด ๆ ก็ได้  delegate ใน C# คล้ายกับ function pointer ใน C++   เพื่อให้เกิดความเข้าใจ พิจารณาจาก code ต่อไปนี้

// Delegate declaration
public delegate void MyDelegate(string ArgValue);

public void DelegateFunction(string PassValue)
{
// Method implement here
}
public void UseMethod()
{
// Delegate Instantiation
MyDelegate DelegateObject = new MyDelegate(DelegateFunction);

}

จาก code ด้านบน บรรทัดแรกคือการประกาศ delegate สังเกตุรูปแบบของการประกาศเป็นไปตาม syntax นี้
delegate <return type> <delegate-name> <parameter list>
จาก code ตัวอย่าง delegate ชื่อ MyDelegate ไม่ return ค่าและมี parameter 1 ตัวคือ PassValue ซึ่งมีชนิดเป็น string สำหรับ function ที่เราจะทำ delegate ก็จะต้อง มีการกำหนด signature ให้ตรงกับ delegate ที่ประกาศไว้ จากตัวอย่าง คือ function ถัดมา ชื่อว่า DelegateFunction ซึ่งได้มีการประกาศ signature ไว้ตรงกับ delegate ด้านบนทุกประการ หลังจากนั้น การใช้งานเราจำทำการ instantiate Delegate object คล้ายกับการ Instantiat object ของ Class  เพียงแต่ว่า นี้เป็น object ที่เป็นdelegate โดยจะส่งชื่อ function ที่ต้องการเป็น Parameter ของ delegate constructure

Mydelegate DelegatObject = new MyDelegate(DelegateFunction);

หลังจากนั้น การใช้งานเราสามารถที่จะเรียก DelegateObject แทนชื่อ function ได้เลยและเป็นลักษณะ reference สามารถที่จะส่งผ่าน DelegateObject เป็น argument ของfunctionใด ๆ ที่ต้องการใช้ function เป็น parameter ไ้ด้เลย ซึ่งจะไ้้้่ด้เห็นการใช้งานในรูปแบบต่าง ๆต่อไป

การใช้งาน Delegate

เพื่อให้เห็น ภาพมากขึ้นจะขอยกตัวอย่างการเรียกใช้ function ปกติขึ้นมาพิจารณาเพื่อนำไปสู่การใช้งาน ขอให้พิจารณาจากตัวอย่างนี้นะครับ


namespace MyNameSpace.NoDelegate
{
    public class MyClass
    {
        public void Process()
        {
            Console.WriteLine("Process() begin");
            Console.WriteLine("Process() end");
        }
    }
    public class Test
    {
        static void Main(string[] args)
        {
            MyClass myClass = new MyClass();
            myClass.Process();
        }
    }
}

จาก ตัวอย่าง เป็นการใช้งานทั่วไปสำหรับการเรียกใช้ function สมมุติว่าถ้าหากเราไม่ต้องการเรียกใช้ function ตรง ๆ หล่ะหากเราต้องการที่จะส่งผ่าน function นี้ให้กับ function อื่นเรียกใช้อีกที โดยเฉพาะการทำงานในลักษณะ event-driven เรามาดูการ ใช้ delegate จากตัวอย่างเดียวกัน

Very basic delegate


namespace ConsoleApplication1
{
    public class MyClass
    {
        public delegate void WriteHandler(string message);
        public void Process(WriteHandler writeh)
        {
            writeh("Process() begin");
            writeh("Process() end");
        }
        public void writer(string message)
        {
            Console.WriteLine(message);
        }
    }
    public class Program
    {
        static void Main(string[] args)
        {
            MyClass myclass = new MyClass();
            MyClass.WriteHandler writehander = new MyClass.WriteHandler(myclass.writer);
            myclass.Process(writehander);
        }
    }
}

ผลของการ ทำงาน จะได้เช่นเดียวกับโปรแกรมแรกนะครับ แต่โปรแกรมนี้เราใช้คุณลักษณะของ delegate พิจารณา class Myclass จะเห็นว่ามีการ ประกาศ Delegate  ชื่อ ว่า WriteHandler มี parameter 1 ตัวคือ message และไม่มีการ return ค่าดังนั้น function ที่เราต้องการทำ delegation คือ writer สังเกตุว่า มี signature เหมือนกัน

หลังจากนั้น เราต้องการส่งผ่านฟังก์ชันนี้เขาไปทำงานใน method ชื่อ Process นะครับ สังเกตุว่า Parameter ของ Process นั้นเป็นชนิดเดียวกับ delegate

แล้วสุดท้ายเ้รามาดูที่ class program  ที่ Main เราจะเห็นว่า จะมีการ instantiate delegate object ชื่อ writehander แล้วส่ง method ชื่อ writer เป็น argument ผลก็คือได้ writerhandler ที่อ้างอิงไปยัง writer เราก็โยน writerhandler เข้าไปให้ Process เป็นการส่งผ่าน parameter แบบ reference นะครับ  จะเห็นว่าการทำงานครั้งนี้ เราส่งผ่าน function หรือ method เป็น parameter นะครับ นี่แหละการทำงานของ delegate ครับ จะเห็นว่าการทำงานของ delegate นั้นมีขั้นตอนดังนี้นะครับ

  • Delclaration
  • Instantiation
  • Invocation

จากตัวอย่างที่แล้ว หลายท่านที่เป็น มือใหม่อาจจะยังมึน ๆ ขอตบท้ายด้วย ตัวอย่าง ง่าย ๆ อีกหนึ่งตัวอย่างนะครับ

using System;

namespace Mynamespace.BasicDelegate
{
    // #1 Declaration
    public delegate void SimpleDelegate();

    class TestDelegate
    {
        public static void MyFunc()
        {
            Console.WriteLine("I was called by delegate ...");
        }
        public static void Main()
        {
            // #2 Instantiation
            SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);
            // #3 Invocation
            simpleDelegate();
        }
    }
}

เป็นงั้ยครับ พอจะได้ concept ใหม่ครับ ลอง run code ดูนะครับแล้วจะเข้าใจ สำหรับ delegate ขอพอแค่นี้ก่อนนะครับ ขอให้ติดตาม ในตอน ต่อ ๆไปนะครับเนื่องจากมีการใช้งานอีก หลายลักษณะ  ขอให้มีความสุขกับการเรียนรู้นะครับ

Categories: C# .NET Tags: