0323 대리자를 이용해 다중 메서드 호출 Multicast delegate/delegate chaining

2021. 3. 23. 17:31C#/수업내용

public class CarDriver 
    {
        public CarDriver() 
        { }
        public static void GoLeft() 
        {
            Console.WriteLine("GoLeft");
        }
        public static void GoFoward()
        {
            Console.WriteLine("GoFoward");
        }
    }

    public class App
    {
        //대리자 선언
        public delegate void GoHome();
        public delegate void Say();
        public App()
        {
            //대리자 변수 선언 및 초기화
            GoHome go = new GoHome(CarDriver.GoLeft);
            //대리자 객체 추가
            go += new GoHome(CarDriver.GoFoward);
            go -= new GoHome(CarDriver.GoLeft);
            //대리자 호출
            go();

            Say say;
            say = new Say(Hi);
            say += new Say(() =>
            {
                Console.WriteLine("Hello");
            });

            say();
            say -= new Say(Hi);
        }
        private void Hi() 
        {
            Console.WriteLine("Hi");
        }

    }
public class App
    {
        //대리자 선언 
        public delegate void SendMessage(string message);

        //생성자 
        public App() {
            Console.WriteLine("App");

            //대리자 변수 선언및 초기화 
            SendMessage del1 = this.Hello;
            SendMessage del2 = this.Hi;

            //대리자 호출 
            del1("홍길동");
            del2("임꺽정");

            Console.WriteLine("---------------------------");

            //대리자 변수 선언 및 초기화 (멀티캐스트) 
            SendMessage del = del1 + del2;
            del("장길산");

            Console.WriteLine("---------------------------");

            SendMessage del3 = (name) => {
                Console.WriteLine("안녕하세요, {0}님", name);
            };

            del += del3;
            del("고길동");

            Console.WriteLine("---------------------------");

            del = del - del2;
            del("둘리");

            Console.WriteLine("---------------------------");

            del -= del1;
            del("도우너");
        }

        public void Hello(string name) {
            Console.WriteLine("Hello, {0}", name);
        }

        public void Hi(string name) {
            Console.WriteLine("Hi, {0}", name);
        }
    }
public class App
    {
        //대리자 선언
        public delegate void ThereIsAFire(string location);
        public App()
        {
            ThereIsAFire fire = new ThereIsAFire(Call119);
            fire += new ThereIsAFire(ShoutOut);
            fire += new ThereIsAFire(Escape);

            fire("우리 집");
        }
        public void Call119(string location) 
        {
            Console.WriteLine("소방서죠? 불났어요! 주소는 {0}", location);
        }
        public void ShoutOut(string location) 
        {
            Console.WriteLine("피하세요! {0}에 불났어요.", location);
        }
        public void Escape(string location) 
        {
            Console.WriteLine("{0}에서 나갑니다", location);
        }
    }

위의 것과 아랫 것은 같음

(ThereIsAFire)로 형식 변환

서로 다른 객체에게 해당 메서드를 알릴 때 사용한다.

public class App
    {
        //대리자 선언
        public delegate void ThereIsAFire(string location);
        public App()
        {
            ThereIsAFire fire=(ThereIsAFire)Delegate.Combine(new ThereIsAFire(Call119),
                new ThereIsAFire(ShoutOut),
                new ThereIsAFire(Escape));
            //ThereIsAFire fire = new ThereIsAFire(Call119);
            //fire += new ThereIsAFire(ShoutOut);
            //fire += new ThereIsAFire(Escape);

            fire("우리 집");
        }
        public void Call119(string location) 
        {
            Console.WriteLine("소방서죠? 불났어요! 주소는 {0}", location);
        }
        public void ShoutOut(string location) 
        {
            Console.WriteLine("피하세요! {0}에 불났어요.", location);
        }
        public void Escape(string location) 
        {
            Console.WriteLine("{0}에서 나갑니다", location);
        }
    }

캐릭터가 

'C# > 수업내용' 카테고리의 다른 글

0324 File 클래스 (외부 파일 불러오기)  (0) 2021.03.24
0324 복습  (0) 2021.03.24
0323 익명 함수  (0) 2021.03.23
0322 대리자 delegate★★  (0) 2021.03.22
0321 기초 복습!  (0) 2021.03.19