C#之—委託

(1)定義委託:(百度百科樣例,只有寫了纔有收穫)html

namespace Entrust
{
    public delegate void GreetingDelegate(string name); //定義委託
    class Class1
    {
        public void EnglishGreeting(string name)
        {
            Console.WriteLine("Good Morning." + name);
        }

        public void ChineseGreeting(string name)
        {
            Console.WriteLine("早上好." +name);
        }

        public void GreetPeople(string name, GreetingDelegate MakeGreeting)
        {
            MakeGreeting(name);
        }
    }
}
View Code

測試類:設計模式

namespace Entrust
{
    class TestMain
    {
        static void Main(string[] args)
        {
            Class1 c = new Class1();

            //c.GreetPeople("San Zhang",c.EnglishGreeting);
            //c.GreetPeople("張三", c.ChineseGreeting);

            //Console.WriteLine("委託變量!");
            //GreetingDelegate delegate1 = c.EnglishGreeting;
            //GreetingDelegate delegate2 = c.ChineseGreeting;
            //c.GreetPeople("張三", delegate2);
            //c.GreetPeople("San Zhang", delegate1);

            //Console.WriteLine("多個方法綁定同一個委託!");
            //GreetingDelegate delegate1;
            //delegate1 = c.EnglishGreeting;
            //delegate1 += c.ChineseGreeting;
            //c.GreetPeople("李四",delegate1);

            //Console.WriteLine("直接調用委託!");
            //GreetingDelegate delegate1;
            //delegate1 = c.EnglishGreeting;
            //delegate1 += c.ChineseGreeting;
            //delegate1("小張");

            //GreetingDelegate delegate1 = new GreetingDelegate(c.EnglishGreeting);
            //delegate1 += c.ChineseGreeting;
            //delegate1("小李");

            GreetingDelegate delegate1 = c.EnglishGreeting;
            delegate1 += c.ChineseGreeting; //綁定委託
            delegate1 -= c.EnglishGreeting; //解除委託
            delegate1("小張");
        }
    }
}
View Code

(2)定義委託事件(封裝屬性和方法)ide

namespace Entrust
{
    public delegate void GreetingDelegate1(string name);

    class GreetingManage
    {
        public event GreetingDelegate1 MakeGreet;//這裏聲明一個事件

        public GreetingDelegate1 delegate1;
        public void GreetPeople(string name)
        {
            //if (delegate1 != null)
            //    delegate1(name);
            MakeGreet(name);
        }
    }
}
View Code

執行的方法類測試

namespace Entrust
{
    class GreetingLanguage
    {
        public void EnglishGreeting(string name)
        {
            Console.WriteLine("Morning, " + name);
        }

        public void ChineseGreeting(string name) 
        {
            Console.WriteLine("早上好, " + name);
        }
    }
}
View Code

測試類:this

namespace Entrust
{
    class TestMain
    {
        static void Main(string[] args)
        {
            GreetingLanguage gl = new GreetingLanguage();
            GreetingManage gm = new GreetingManage();
            //gm.delegate1 = gl.EnglishGreeting;
            gm.MakeGreet += gl.ChineseGreeting;
            gm.GreetPeople("小張");
            Console.ReadKey();
        }
    }
}
View Code

(3)委託彌補觀察者模式的不足(若觀察者不存在)詳見(設計模式之—觀察者模式
通知者接口類(Subject)spa

namespace Observer_Pattern.CLASS
{
    interface Subject
    {
        void Notify(); //通知
        string SubjectState
        {
            get;
            set;
        }
    }
}
View Code

祕書類(secreter)實現通知者接口設計

namespace Observer_Pattern.CLASS
{
    public delegate void EventHandler(); //聲明一個委託
    class Secretary:Subject
    {
        public event EventHandler Update; //聲明一事件Update,類型爲委託EventHandler
        private string action;

        /// <summary>
        /// 通知
        /// </summary>
        public void Notify() 
        {
            Update();
        }

        public string SubjectState
        {
            get { return action; }
            set { action = value; }
        }
    }
}
View Code

 炒股工做者(stockWorker)3d

namespace Observer_Pattern.CLASS
{
    class StockObserver
    {
        private string name;
        private Subject sub;
        public StockObserver(string name, Subject sub)
        {
            this.name = name;
            this.sub = sub;
        }
        public void StockUpdate()
        {
            Console.WriteLine("{0} ,{1} 關閉股票軟件,繼續工做!",sub.SubjectState,name);
        }
    }
}
View Code

看NBA工做者(NBAWorker)code

namespace Observer_Pattern.CLASS
{
    class NBAObserver
    {
        private string name;
        private Subject sub;
        public NBAObserver(string name, Subject sub)
        {
            this.name = name;
            this.sub = sub;
        }

        public void NBAUpdate()
        {
            Console.WriteLine("{0} ,{1} 關閉NBA視頻,繼續工做!",sub.SubjectState,name);
        }
    }
}
View Code

測試類(TestMain)視頻

namespace ObserverPattern_Entrust_
{
    class TestMain
    {
        static void Main(string[] args)
        {
            Secretary mishu = new Secretary();
            StockObserver worker1 = new StockObserver("小張", mishu);
            NBAObserver worker2 = new NBAObserver("小李", mishu);

            mishu.Update += worker1.StockUpdate; //綁定委託
            mishu.Update += worker2.NBAUpdate; //綁定委託

            mishu.SubjectState = "老闆回來了";
            mishu.Notify(); //調用委託

            Console.ReadLine();
        }
    }
}
View Code

測試結果:

相關文章
相關標籤/搜索