deligate (๋๋ฆฌ์)
delegate๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ์๋๋ฅผ ๋ณ์์ฒ๋ผ ๋ค๋ฃฐ ์ ์๋ค. ์ฆ, ๋ณ์์ฒ๋ผ delegate์ ๋ฉ์๋๋ฅผ ํ ๋นํ ์ ์๋ค.
์๋ฅผ ๋ค์ด, ์๋์ ๊ฐ์ delegate๋
delegate int MathOperation(int a, int b);
int ๋ ๊ฐ๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋ฐ๊ณ , ์ ์๋ฅผ ๋ฐํํ๋ ๋ชจ๋ ํจ์๋ฅผ ํ ๋นํ ์ ์๋ค.
int Add(int a, int b)
{
return a + b;
}
int Subtract(int a, int b)
{
return a - b;
}
Add์ Substract ๋ฉ์๋๋ ๋ค๋ฅด์ง๋ง, ๋ ๋ค ๋งค๊ฐ๋ณ์๋ฅผ intํ 2๊ฐ๋ฅผ ๋ฐ๊ณ , intํ์ ๋ฐํํ๊ธฐ ๋๋ฌธ์ delegate MathOperation์ ํ ๋นํ ์ ์๋ค.
delegate๋ ์ ์ธํ๊ณ ์ ์ธํ ๋งค๊ฐ๋ณ์์ ๋ฉ์๋๋ฅผ ํ ๋นํ๋ฉด ๋๋ค.
MathOperation operation = Add;
๋ง์ฝ ์์ Add์ Substract ๋ฉ์๋๊ฐ Calculate ์ด๋ผ๋ ํด๋์ค์ ๋ค์ด๊ฐ์๋ค๊ณ ๊ฐ์ ํ๋ฉด, ์๋์ ๊ฐ์ด ํ ๋นํ ์๋ ์๋ค.
Calculate cal = new Calculate();
MathOperation a;
a = new MathOperation(cal.Add); // a์ Add ๋ฉ์๋ ํ ๋น
a = new MathOperation(cal.Substract); // b์ Substract ๋ฉ์๋ ํ ๋น
degelate์ ์ฌ์ฉํ๋ ์ด์
์ ์ง์ ํธ์ถํ์ง ์๊ณ ๋๋ฆฌ์๋ฅผ ์ฌ์ฉํ ๊น?
๋๋ฆฌ์๋ ๋ฉ์๋์ ๋ํ ์ฐธ์กฐ์ด๊ธฐ ๋๋ฌธ์, ๋ฉ์๋ ์์ฒด๋ฅผ ๋๊ฒจ์ค ๋ ์ฌ์ฉํ๋ค. ๋๋ฆฌ์๋ฅผ ์ฌ์ฉํด์ ๋ค๋ฅธ ๋ฉ์๋๋ฅผ ๋์ผํ ๋ฐฉ์์ผ๋ก ํธ์ถํ ์ ์๊ธฐ ๋๋ฌธ์ ์ผ๋ฐํ๋ ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋ค.
๋๋ฆฌ์๋ ๋น์ทํ ๊ธฐ๋ฅ์ ์ฌ๋ฌ ๊ณณ์์ ์ฌ์ฉํ ๋, ๋น๊ต ๋ก์ง์ ํ ๋ฒ ์ ์ํ๊ณ ์ฌ๋ฌ ๊ณณ์์ ์ฌ์ฌ์ฉํ ์ ์๊ธฐ ๋๋ฌธ์ ์ฝ๋์ ์ค๋ณต์ ์ค์ผ ์ ์๋ค. ๋ํ, ๋๋ฆฌ์๋ฅผ ์ฌ์ฉํ์ฌ ์ต๋ช ํจ์๋ ๋๋ค์์ ํ์ฉํ ์ ์๋ค.
์ผ๋ฐํ ๋๋ฆฌ์
๋๋ฆฌ์๋ ๋ง์ฐฌ๊ฐ์ง๋ก ์ผ๋ฐํ ๋ฉ์๋๋ ์ฐธ์กฐํ ์ ์๋ค.
delegate int Method<T>(T a, T b);
๋๋ฆฌ์ ์ฒด์ธ
๋๋ฆฌ์๋ ํ๋์ ์ฌ๋ฌ ๊ฐ์ ๋ฉ์๋๋ฅผ ์ด์ ์๋ ์๋ค. ์ด์ ๋๋ ๊ฐํธํ๊ฒ ์ฐ์ฐ๊ธฐํธ๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
namespace DelegateChains
{
delegate void Notify(string message);
class Notifier
{
public Notify EventOccured;
}
class EventListener
{
private string name;
public EventListener(string name)
{
this.name = name;
}
public void SomethingHappened(string message)
{
Console.WriteLine($"{name}.SomethingHappended : {message}");
}
}
class MainApp
{
static void Main(string[] args)
{
Notifier notifier = new Notifier();
EventListener listener1 = new EventListener("Listener1");
EventListener listener2 = new EventListener("Listener2");
EventListener listener3 = new EventListener("Listener3");
// += ๋ก ์๊ธฐ
notifier.EventOccured += listener1.SomethingHappened;
notifier.EventOccured += listener2.SomethingHappened;
notifier.EventOccured += listener3.SomethingHappened;
notifier.EventOccured("You've got mail.");
Console.WriteLine();
// -= ๋ก ๋๊ธฐ
notifier.EventOccured -= listener2.SomethingHappened;
notifier.EventOccured("Download complete.");
Console.WriteLine();
// Delegate.Combine์ผ๋ก ์๊ธฐ
Notify notify1 = new Notify(listener1.SomethingHappened);
Notify notify2 = new Notify(listener2.SomethingHappened);
notifier.EventOccured = (Notify)Delegate.Combine(notify1, notify2);
notifier.EventOccured("Fire!!");
Console.WriteLine();
// Delegate.Remove๋ก ๋๊ธฐ
notifier.EventOccured = (Notify)Delegate.Remove(notifier.EventOccured, notify1);
notifier.EventOccured("ASAP!!");
Console.WriteLine();
}
}
}
์ด๋ฒคํธ
์ด๋ฒคํธ๋ ๋๋ฆฌ์์ ์ ๋ฐ์ ์ผ๋ก ๊ฐ์ ์ญํ ์ ํ์ง๋ง, ๋๋ฆฌ์๋ณด๋ค ์์ ํ ๋ฒ์ ์ผ๋ก ๋ณผ ์ ์๋ค.
using System;
public class Button
{
public event EventHandler Click;
public void OnClick()
{
// Click ์ด๋ฒคํธ๊ฐ null์ด ์๋ ๊ฒฝ์ฐ, ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ฅผ ํธ์ถ
Click?.Invoke(this, EventArgs.Empty);
}
}
public class Program
{
public static void Main(string[] args)
{
Button button = new Button();
button.Click += Button_Click;
button.OnClick();
Console.ReadLine();
}
private static void Button_Click(object sender, EventArgs e)
{
Console.WriteLine("๋ฒํผ์ด ํด๋ฆญ๋์์ต๋๋ค.");
}
}
'๐ฅ๏ธ > C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ด๊ฒ์ด C#์ด๋ค] Chapter15: ์ฐ์ต๋ฌธ์ (LINQ) (0) | 2023.10.05 |
---|---|
[C#] ๋๋ค์๊ณผ Func, Action ๋๋ฆฌ์ (0) | 2023.09.18 |
[์ด๊ฒ์ด C#์ด๋ค] Chapter12: ์ฐ์ต๋ฌธ์ (0) | 2023.08.25 |
[C#] try catch์ throw๋ฌธ/์ (0) | 2023.08.25 |
[์ด๊ฒ์ด C#์ด๋ค] Chapter11: ์ฐ์ต๋ฌธ์ (0) | 2023.08.22 |