.net observer design pattern, different forms of traditional values, entrusted to implement the non-commissioned

1 What is the Observer design pattern

  • Observer pattern is a software design pattern defines a relationship between the subject an-many, the coupling of the code can be reduced. As used herein, the observer model to achieve different forms of traditional values.

2. Observer design pattern to achieve non-commissioned

Figure ideas:
Here Insert Picture Description
Use Interface
First create an interface file, IMessageON.cs, which has a RecieveMsg method;

  namespace WindowsFormsApplication2
{
    public interface IMessageON
    {
        void RecieveMsg(string str);
    }
}

In the Main window and open the main mediating role in all forms.
Code:

 namespace WindowsFormsApplication2
{
    public partial class Main : Form
    {
        public info_send info_send1 { get; set; } 
        public Main()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            info_send s1 = new info_send();
            this.info_send1 = s1;
            s1.Show();

            info_receive1 r1 = new info_receive1();
           // 用接口的形式注册 观察者。
            s1.MessageOnList.Add(r1);
            r1.Show();

            info_receive2 r2 = new info_receive2();
           // 用接口的形式注册 观察者。
            s1.MessageOnList.Add(r2);
            r2.Show();
        }

info_send form, create and transmit a data collection interface type
codes

 namespace WindowsFormsApplication2
{
    public partial class info_send : Form
    {
        public info_send()
        {
            InitializeComponent();
            MessageOnList = new List<IMessageON>(); //集合
          
        }
     
          //这个集合放我们的观察者
        public List<IMessageON> MessageOnList { get; set; }
        private void button1_Click(object sender, EventArgs e)
        {
            foreach (var messageOn in MessageOnList)
            {
            
                messageOn.RecieveMsg(textBox1.Text);
            }
        }
    }
}

info_receive1 forms, implement the interface, (info_receive2 the same form)

namespace WindowsFormsApplication2
{
    public partial class info_receive1 : Form,IMessageON //使用接口
    {
        public info_receive1()
        {
            InitializeComponent();
          
        }

        public void RecieveMsg(string str)  //实现接口
        {
            this.textBox1.Text = str;

        }

Renderings:
Here Insert Picture Description

2. Observer design pattern commissioned achieve

There are also events using delegates
in the Main form mainly acts as an intermediary and open all the form, the code is as follows:

    namespace WindowsFormsApplication2
{
    public partial class Main : Form
    {
        public info_send info_send1 { get; set; }
        public Main()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            info_send s1 = new info_send();
            this.info_send1 = s1;
            s1.Show();

            info_receive1 r1 = new info_receive1();
            //事件的形式注册观察者。
            s1.Messevent+=r1.SetMsgTextEvent;
            //用户委托的形式注册观察者
            s1.Message += r1.SetMsgText;
            r1.Show();

            info_receive2 r2 = new info_receive2();
            //事件的形式注册观察者。
            s1.Messevent += r2.SetMsgTextEvent;
            //用户委托的形式注册观察者
            s1.Message += r2.SetMsgText;
            r2.Show();
        }
    }
 }

info_send form,

namespace WindowsFormsApplication2
{
    public partial class info_send : Form
    {
        public delegate void Mess(string str); //声明委托 

        public info_send()
        {
            InitializeComponent();  
        }
        
        #region 用户委托
        public Mess Message { get; set; }  //委托
        private void button1_Click(object sender, EventArgs e)
        {
            Message(textBox1.Text);
        } 
        #endregion

        #region 使用Event事件
        //事件
        public event Mess Messevent;
        private void button2_Click(object sender, EventArgs e)
        {
            Messevent(textBox1.Text);
        } 
        #endregion
    }
}

nfo_receive1 form, (info_receive2 the same form)

namespace WindowsFormsApplication2
{
    public partial class info_receive1 : Form
    {
        public info_receive1()
        {
            InitializeComponent();   
        }

        public void SetMsgTextEvent(string str)
        {
            this.textBox1.Text = " 事件委托" + str;
        }
        public void SetMsgText(string str)
        {
            this.textBox1.Text = " 用户委托" + str;
        }
    }
}

:( renderings as two effects)
Here Insert Picture Description

Published 16 original articles · won praise 2 · Views 204

Guess you like

Origin blog.csdn.net/weixin_43482965/article/details/104312758