专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 设计模式

观察者模式,越详细越好.200分送上.大家看分的份上啊该如何处理

发布时间:2011-06-19 13:22:50 文章来源:www.iduyao.cn 采编人员:星星草
观察者模式,越详细越好.200分送上.大家看分的份上啊。
观察者模式,越详细越好.200分送上.大家看分的份上啊。

------解决方案--------------------
C# code
//Subject.cs
using System;

namespace Observer
{
    /// <summary>
    /// Summary description for Subject.
    /// </summary>
    public interface Subject     {
         void registerInterest(Observer obs);
    }
}

//ListObs.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Observer
{
    /// <summary>
    /// Summary description for ListObs.
    /// </summary>
    public class ListObs : System.Windows.Forms.Form, Observer
    {
        private System.Windows.Forms.ListBox lsColors;
        /// <summary>
        /// Adds text of color to list box
        /// </summary>
        private System.ComponentModel.Container components = null;

        public ListObs(Subject subj)         {
            InitializeComponent();
            init(subj);
        }
        //------
        public void init(Subject subj) {
            subj.registerInterest (this);
        }
        //------
        public void sendNotify(string message){
            lsColors.Items.Add(message);
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.lsColors = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            // 
            // lsColors
            // 
            this.lsColors.Location = new System.Drawing.Point(16, 16);
            this.lsColors.Name = "lsColors";
            this.lsColors.Size = new System.Drawing.Size(216, 173);
            this.lsColors.TabIndex = 0;
            // 
            // ListObs
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(264, 213);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.lsColors});
            this.Name = "ListObs";
            this.Text = "List observer";
            this.ResumeLayout(false);

        }
        #endregion
    }
}


//Observer.cs
using System;

namespace Observer
{
    /// <summary>
    /// Summary description for Observer.
    /// </summary>
    public interface Observer     {
        void sendNotify(string message);
    }
}


//ColObserver.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Observer
{
    /// <summary>
    /// Summary description for ColObserver.
    /// </summary>
    public class ColObserver : System.Windows.Forms.Form, Observer     {
        private System.ComponentModel.Container components = null;
        private Brush bBrush;
        private System.Windows.Forms.PictureBox pic;
        private Font fnt;
        private Hashtable colors;
        private string colName;
        //-----
        public ColObserver(Subject subj)         {
            InitializeComponent();
            init(subj);
        }
        //-----
        private void init(Subject subj) {
            subj.registerInterest (this);
            fnt = new Font("arial", 18, FontStyle.Bold);
            bBrush = new SolidBrush(Color.Black);
            pic.Paint+= new PaintEventHandler (paintHandler);
            colors = new Hashtable ();
            colors.Add("red", Color.Red );
            colors.Add ("blue", Color.Blue );
            colors.Add ("green", Color.Green );
            colName = "";
        }
        //-----
        public void sendNotify(string message) {
            colName = message;
            message = message.ToLower ();
            Color col = (Color)colors[message];
            pic.BackColor = col;
        }
        //-----
        private void paintHandler(object sender, PaintEventArgs e) {
             Graphics g = e.Graphics ;
             g.DrawString(colName, fnt, bBrush, 20, 40);
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.pic = new System.Windows.Forms.PictureBox();
            this.SuspendLayout();
            // 
            // pic
            // 
            this.pic.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.pic.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.pic.Location = new System.Drawing.Point(24, 24);
            this.pic.Name = "pic";
            this.pic.Size = new System.Drawing.Size(184, 152);
            this.pic.TabIndex = 0;
            this.pic.TabStop = false;
            // 
            // ColObserver
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(248, 213);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.pic});
            this.Name = "ColObserver";
            this.Text = "Color observer";
            this.ResumeLayout(false);

        }
        #endregion
    }
}
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

  • 关于后台服务器架构有关问题

    关于后台服务器架构问题 最近小弟在优化后台服务器的工作,因为以前的服务器是采用单进程,单线程,并没有涉及到多台服务器的交互问题...

  • 请问个模式的应用

    请教个模式的应用 要做个公司的权限管理,有三个角色 公司管理员,具有权限M1(a),M1(a,b) 部门经理,具有权限M1(a,b),M2() 部门管理...

  • 继承的优缺点解决方法

    继承的优缺点 请问大家如何理解继承,如何使用好继承? ------解决方案-------------------- 关于这个问题,我的确做了一些思考,推荐...

  • 急设计程序删除文件夹中的已损坏pdf文件解决办法

    急!急!急!设计程序删除文件夹中的已损坏pdf文件 文件夹中总共的pdf文件数量有三十几万,怎么才能删除这些pdf文件中已损坏的pdf文件呢? ...

  • 关于Singleton模式继承的有关问题

    关于Singleton模式继承的问题? 定义一个Singleton类,一般都是要被其他实际的类继承,使这个实际的类具有Singleton功能。 现在看到二...

  • 怎么分析需求以决定用什么设计模式

    如何分析需求以决定用什么设计模式? 最近在看设计模式解析,基本上是陷到里面无法自拔了,我的问题是如何分析需求以决定用什么设计模...

  • 设计模式的应用解决方案

    设计模式的应用 我不知道什么时候该用什么样的设计模式 有号得例子或者教学视频吗 ------解决方案-------------------- 这个...

  • visio2002创建UML类图解决思路

    visio2002创建UML类图 使用visio2002画UML类图,文件-新建-选择绘图类型-UML模型图,就会报下面的错误 Runtime Error Program c:\.....

  • 蜂窝模式?该怎么解决

    蜂窝模式? 啥意思啊 ------解决方案-------------------- 是设计模式中的一个 161592169@qq.com 你给我发歌邮件 我回...

  • [HoWo03]是什么书,在google和百度上都找不到,该怎么解决

    [HoWo03]是什么书,在google和百度上都找不到 [HoWo03]是什么书,在google和百度上都找不到 ------解决方案-------------------- H...

热门推荐: