コード例 #1
0
        public override void Init()
        {
            //委托事件实现
            var heater = new Heater();

            heater.OnBoiled += new AlarmObserver().Alert;
            heater.OnBoiled += new ScreenObserver().Show;
            heater.BoilWater();

            //IObservable<>和IObserver<>实现
            var subject = new Ammeter();
            var alarm   = new MeterAlarm();
            var sms     = new MeterSMS();

            alarm.Subscribe(subject);
            sms.Subscribe(subject);
            subject.Publish();

            alarm.Unsubscribe();
            subject.Publish();

            alarm.Subscribe(subject);
            sms.Unsubscribe();
            subject.Publish();
        }
コード例 #2
0
        static void Main02(string[] args)
        {
            Heater heater = new Heater();
            Alarm  alarm  = new Alarm();

            heater.BoilEvent += alarm.MakeAlert;
            heater.BoilEvent += Display.ShowMsg;

            heater.BoilWater();
        }
コード例 #3
0
        public StudyEvent01()
        {
            InitializeComponent();
            Heater heater = new Heater();
            Alarm  alarm  = new Alarm();

            heater.Boiled += alarm.MakeAlert;
            //heater.Boiled += (new Alarm()).MakeAlert;
            //heater.Boiled += new Heater.BoiledEventHandler(alarm.MakeAlert);
            heater.Boiled += Display.ShowMsg;
            heater.BoilWater();
        }
コード例 #4
0
        private void button2_Click(object sender, EventArgs e)
        {
            Heater heater = new Heater();
            Alarm alarm = new Alarm();

            heater.Boiled += alarm.MakeAlert;	//注册方法
            heater.Boiled += (new Alarm()).MakeAlert;		//给匿名对象注册方法
            heater.Boiled += new Heater.BoiledEventHandler(alarm.MakeAlert);	//也可以这么注册
            heater.Boiled += Display.ShowMsg;		//注册静态方法

            heater.BoilWater();	//烧水,会自动调用注册过对象的方法
        }
コード例 #5
0
        static void Main(string[] args)
        {//将发布者和订阅者串联起来的方法
            //发布者(拥有事件)
            Heater heater = new Heater();
            //订阅者(拥有具体的执行方法)
            Alarm alarm = new Alarm();

            //订阅事件
            heater.BoilEvent += alarm.MakeAlert;
            heater.BoilEvent += Display.ShowMsg;
            heater.BoilWater();
        }
コード例 #6
0
        static void Main(string[] args)
        {
            GreetPeople("sufei", EnglishGreeting);
            GreetPeople("苏飞", ChineseGreeting);

            GreetingDelegate delegate1;

            //注意这里,第一次用的“=”,是赋值的语法;第二次,用的是“+=”,是绑定的语法。如果第一次就使用“+=”,将出现“使用了未赋值的局部变量”的编译错误。
            delegate1  = EnglishGreeting; // 先给委托类型的变量赋值
            delegate1 += ChineseGreeting; // 给此委托变量再绑定一个方法


            GreetPeople("micheal", delegate1);

            delegate1 -= EnglishGreeting; //取消对EnglishGreeting方法的绑定

            GreetPeople("tom", delegate1);

            Heater heater = new Heater();
            Alarm  alarm  = new Alarm();

            heater.BoilEvent += alarm.MakeAlert;         //注册方法
            heater.BoilEvent += (new Alarm()).MakeAlert; //给匿名对象注册方法
            heater.BoilEvent += Display.ShowMsg;         //注册静态方法

            heater.BoilWater();                          //烧水,会自动调用注册过对象的方法

            //ReturnObjectHandler rh1 = getObject;
            //错误 返回类型错误
            //ReturnObjectHandler rh2 = getVoid;

            //错误	在以下方法或属性之间的调用不明确:“_10Delegate.Program.TestReturn.TestReturn(_10Delegate.ReturnObjectHandler)”和“_10Delegate.Program.TestReturn.TestReturn(_10Delegate.ReturnVoidtHandler)”
            //TestReturn rr1 = new TestReturn(getObject);
            //TestReturn rr2 = new TestReturn(getVoid);

            Console.ReadKey();
        }
コード例 #7
0
        static void Main(string[] args)
        {
            #region test_观察者模式

            //XiaoMei xiao_mei = new XiaoMei();
            //XiaoMi xiao_mi = new XiaoMi();
            //XiaoLan xiao_lan = new XiaoLan();

            ////小米和小蓝在小美那里都注册了一下
            //xiao_mei.AddPerson(xiao_mi);
            //xiao_mei.AddPerson(xiao_lan);

            ////小美向小米和小蓝发送消息
            //xiao_mei.NotifyPerson();

            //Console.ReadLine();

            #endregion

            #region test_装饰者模式

            #region 装饰手机demo

            //// 我买了个苹果手机
            //Phone phone = new ApplePhone();

            //// 现在想贴膜了
            //Decorator applePhoneWithSticker = new Sticker(phone);
            //// 扩展贴膜行为
            //applePhoneWithSticker.Print();
            //Console.WriteLine("----------------------\n");

            //// 现在我想有挂件了
            //Decorator applePhoneWithAccessories = new Accessories(phone);
            //// 扩展手机挂件行为
            //applePhoneWithAccessories.Print();
            //Console.WriteLine("----------------------\n");

            //// 现在我同时有贴膜和手机挂件了
            //Sticker sticker = new Sticker(phone);
            //Accessories applePhoneWithAccessoriesAndSticker = new Accessories(sticker);
            //applePhoneWithAccessoriesAndSticker.Print();

            #endregion

            #region  明治demo

            //Food food = new Bread(new Vegetable(new Cream(new Food("香肠"))));
            //Console.WriteLine(food.Make());

            #endregion


            #endregion

            #region test_工厂模式

            ////简单工厂模式
            //Car c = Factory.getCarInstance("Benz");
            //if (c != null)
            //{
            //    c.Run();
            //    c.Stop();
            //}
            //else
            //{
            //    Console.WriteLine("造不了这种汽车。。。");
            //}

            ////工厂方法模式
            //VehicleFactory factory = new BroomFactory();
            //Moveable m = factory.Create();
            //m.Run();

            ////抽象工厂模式
            //AbstractFactory f = new DefaultFactory();
            //Vehicle v = f.CreateVehicle();
            //v.Run();
            //Car car = new Car();
            //car.Run();

            //Weapon w = f.CreateWeapon();
            //w.Shoot();

            #endregion

            #region test_代理模式

            ////静态代理
            //ProxyInterface proxyInterface = new WeddingCompany(new NormalHome());
            //proxyInterface.Marry();

            ////动态代理
            //var proxyMotheds = new Dictionary<string, DynamicAction>();
            //proxyMotheds.Add("Add", new DynamicAction()
            //{
            //    BeforeAction = new Action(() => Console.WriteLine("Before Doing....")),
            //    AfterAction = new Action(() => Console.WriteLine("After Doing...."))
            //});

            //var user = new User();
            //var t = ProxyFactory<User>.Create(user, proxyMotheds);

            //int count = 0;

            ////t.Add("Tom", 28, out count);
            ////t.SayName();

            //Console.WriteLine(count);

            #endregion

            #region 依赖注入

            ////Setter注入
            //IServiceClass serviceA = new ServiceClassA();
            //IServiceClass serviceB = new ServiceClassB();
            //ClientClass client = new ClientClass();

            //client.Set_ServiceImpl(serviceA);
            //client.ShowInfo();
            //client.Set_ServiceImpl(serviceB);
            //client.ShowInfo();

            //构造注入
            //IServiceClass serviceA = new ServiceClassA();
            //IServiceClass serviceB = new ServiceClassB();
            //ClientClass clientA = new ClientClass(serviceA);
            //ClientClass clientB = new ClientClass(serviceB);

            //clientA.ShowInfo();
            //clientB.ShowInfo();

            ////依赖获取
            //IFactory factory = FactoryContainer.factory;
            //IWindow window = factory.MakeWindow();
            //Console.WriteLine("创建 " + window.ShowInfo());
            //IButton button = factory.MakeButton();
            //Console.WriteLine("创建 " + button.ShowInfo());
            //ITextBox textBox = factory.MakeTextBox();
            //Console.WriteLine("创建 " + textBox.ShowInfo());

            #endregion

            #region 委托

            Heater heater = new Heater();
            Alarm  alarm  = new Alarm();
            heater.BoilEvent += alarm.MakeAlert;         //注册方法
            heater.BoilEvent += (new Alarm()).MakeAlert; //给匿名对象注册方法
            heater.BoilEvent += Display.ShowMsg;         //注册静态方法
            heater.BoilWater();                          //烧水,会自动调用注册过对象的方法

            #endregion

            Console.ReadLine();
        }