zoukankan      html  css  js  c++  java
  • 观察者设计模式代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace _04PublishPatten
    {
    
        public partial class MainFrm : Form
        {
            public ChildFrm1 ChildFrm1 { get; set; }
            public MainFrm()
            {
                InitializeComponent();
            }
    
            private void btnOpenFrms_Click(object sender, EventArgs e)
            {
                ChildFrm1 frm1 =new ChildFrm1();
    
                this.ChildFrm1 = frm1;
    
                frm1.Show();
    
    
                ChildFrm2 frm2 = new ChildFrm2();
                //第一种:用接口的形式注册 观察者。
                frm1.MessageOnList.Add(frm2);//把观察者放到  观察者的集合中。
    
                //第二种:用户委托的形式注册观察者
                //观察者的 方法注册到  form1窗体的委托中去。
                //frm1.SendMsgDelInstance += frm2.SetMsgText;
    
                //第三种:事件的形式注册观察者。
                frm1.SendMsgDelEvent += frm2.SetMsgTextEvent;
    
                frm2.Show();
    
    
                ChildFrm3 frm3 = new ChildFrm3();
                //frm1.MessageOnList.Add(frm3);
                frm1.SendMsgDelInstance += frm3.SetMsgText;
    
                frm1.SendMsgDelEvent += frm3.SetMsgTextEvent;
                frm3.Show();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                this.ChildFrm1.SendMsgDelInstance("ssssss");
                //this.ChildFrm1.SendMsgDelEvent("sssss");
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _04PublishPatten
    {
        public interface IMessageON
        {
            void RecieveMsg(string str);
        }
    }
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace _04PublishPatten
    {
        public partial class ChildFrm3 : Form,IMessageON
        {
            public ChildFrm3()
            {
                InitializeComponent();
            }
    
            #region 接口实现观察者模式的方法
            public void RecieveMsg(string str)
            {
                this.txtMsg.Text = str + "     " + DateTime.Now.ToString();
            } 
            #endregion
    
    
            #region 委托实现的观察者的方法
    
            public void SetMsgText(string str)
            {
                this.txtMsg.Text ="委托:"+ str;
            }
            #endregion
    
    
            #region 事件使用的方法
    
            public void SetMsgTextEvent(string str)
            {
                this.txtMsg.Text = " 事件:" + str;
            }
            #endregion
        }
    }
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace _04PublishPatten
    {
        public partial class ChildFrm2 : Form,IMessageON
        {
            public ChildFrm2()
            {
                InitializeComponent();
            }
    
            #region 接口实现观察者模式的方法
    
            public void RecieveMsg(string str)
            {
                this.txtMessage.Text = str + "    " + DateTime.Now.ToString();
            }
            
            #endregion
    
            #region 委托使用的方法
    
            public void SetMsgText(string str)
            {
                this.txtMessage.Text = " 委托:" + str;
            }
            #endregion
    
    
            #region 事件使用的方法
    
            public void SetMsgTextEvent(string str)
            {
                this.txtMessage.Text = " 事件:" + str;
            }
            #endregion
        }
    }
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace _04PublishPatten
    {
        public delegate void SendMsgDel(string strMsg);
    
        public partial class ChildFrm1 : Form
        {
            public ChildFrm1()
            {
                InitializeComponent();
                MessageOnList = new List<IMessageON>();
            }
    
            #region 委托实现观察者模式
            public SendMsgDel SendMsgDelInstance { get; set; }
            private void btnDelSendMsg_Click(object sender, EventArgs e)
            {
                SendMsgDelInstance(DateTime.Now.ToString());
            }
            #endregion
    
            #region 用事件实现观察者模式
    
            public event SendMsgDel SendMsgDelEvent;
    
            private void btnEvent_Click(object sender, EventArgs e)
            {
                SendMsgDelEvent(DateTime.Now.ToString() + "   ---");
            }
    
            #endregion
    
            #region C#代码实现观察者模式
            //这个集合放我们的观察者。
            public List<IMessageON> MessageOnList { get; set; }
    
            private void btnMessage_Click(object sender, EventArgs e)
            {
                foreach (var messageOn in MessageOnList)
                {
                    messageOn.RecieveMsg("------");
                }
            }
            #endregion
    
      
       
        }
    }
  • 相关阅读:
    单例模式
    工厂模式
    代理模式
    网络问题
    java中System.getProperty()方法详解
    配置logback.xml文件来实现日志文件输出
    Spring MVC 文件上传与下载快速学习
    SpringMVC中的视图和视图解析器
    JSON的概述和简单的操作
    BeanValidate中的注解
  • 原文地址:https://www.cnblogs.com/Jason1019/p/8511284.html
Copyright © 2011-2022 走看看