設計模式 之 觀察者模式

來源:互聯網
上載者:User
前段時間,看了下網上講的設計模式的 觀察者模式,呵呵,記得有一個很爽的例子,今天閑著沒事,就寫寫來看看.

高中的時候,班上班長是一個女的,n漂亮,身材n好,哪個時候,不知道是多少男生暗戀的對象.
呵呵,可惜,當時一個二個男的都找不到很好的方法,以至,都沒追到,如果說現在回到以前,呵呵,用設計模式來解說 這個追美女的過程,至少來說,如果我要獲得美女今天在哪個位置,呵呵,我就不用在頭疼了,呵呵,你可以說,給她的手機裝個移動的什麼位置的業務,呵呵,那就可以隨時獲得她的位置,但是,你如果都到了能夠搞到她的手機的地步,那應該就不用追了,直接就是你的了.

呵呵,由代碼來說吧.

首先,美女

 public class 美女
    {
        public event LocationDelegate 美女位置改變;
        public 美女()
        {
 
        }
        public void Move(string mLocation)
        {
            this.location = mLocation;
            OnMove(location);
           
        }
        protected void OnMove(string mLocation)
        {
            if (this.美女位置改變 != null)
            {
                美女位置改變(this, new LocationArg(mLocation));
            }
        }
        private string location;
        public string Location
        {
            get
            {
                return this.location;
            }
        }

        private string name;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public override string ToString()
        {
            return this.name.ToString();
        }
    }

  public class LocationArg:EventArgs
    {
        private string location;
        public LocationArg(string mLocation)
        {
            this.location=mLocation;
        }
        public string Location
        {
            get
            {
                return location;
            }
        }
    }
    public delegate void LocationDelegate(object sender, LocationArg mLocationArg);

某個男生的觀察美女位置的變化:
 

 

using System;
using System.Collections.Generic;
using System.Text;

namespace observer
{
   public interface 男生
    {
       string Name
       {
           get;
           set;
       }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace observer
{
    public class 某個男生:男生
    {
        private 美女 美女班長;
        public 某個男生(美女 班長)
        {
            美女班長 = 班長;
            美女班長.美女位置改變 += new LocationDelegate(美女班長位置改變);
        }

        void 美女班長位置改變(object sender, LocationArg mLocationArg)
        {
            string location = mLocationArg.Location.ToString();
        }

        private string mName;
        public string Name
        {
            get
            {
                return mName;
            }
            set
            {
                mName = value;
            }
        }

        public override string ToString()
        {
            return this.Name.ToString();
        }

      
    }
}

 public partial class Form1 : Form
    {
        美女 美女1 = new 美女();
        public Form1()
        {
            InitializeComponent();
            男生 男生1 = new 某個男生(美女1);
        }

    

        private void button1_Click(object sender, EventArgs e)
        {
            美女1.Move("美女在解放公園");
        }
      
    }
}

呵呵,按照這樣的設計,以後男生要知道美女班長某個時候在哪兒,就不用在自己打電話問她了,呵呵,只要美女班長位置一改變,男生就可以獲得美女班長的位置了,哈哈,多爽.....美女,等我...呵呵.

看了別人一篇文章:
http://www.cnblogs.com/ruochen/archive/2007/12/16/996706.html
有點困惑,
呵呵.
及到底是在 被觀察者處註冊觀察者的資訊呢?還是由在觀察者處保持一個被觀察者的資訊.

呵呵,可以看看上面那篇文章,疑問也提到哪兒了.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.