首页 > 其他分享 >CSharp:Observer Pattern

CSharp:Observer Pattern

时间:2022-09-30 21:33:53浏览次数:39  
标签:Observer Pattern void private CSharp new message public

 

    /// <summary>
    /// Summary description for Observer.
    /// Observer Pattern 观察者
    ///20220918
    /// geovindu,Geovin Du,涂聚文
    /// </summary>
    public interface Observer
    {
        void sendNotify(string message);
    }

  

    /// <summary>
    /// Summary description for Subject.
    /// Observer Pattern 观察者
    ///20220918
    /// geovindu,Geovin Du,涂聚文
    /// </summary>
    public interface Subject
    {
        void registerInterest(Observer obs);
    }

  

   /// <summary>
    /// Observer Pattern 观察者
    ///20220918
    /// geovindu,Geovin Du,涂聚文
    /// </summary>
    public partial class ListObsForm : Form, Observer
    {

        /// <summary>
        /// 
        /// </summary>
        public ListObsForm(Subject subj)
        {
            InitializeComponent();
            init(subj);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="subj"></param>
        public void init(Subject subj)
        {
            subj.registerInterest(this);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        public void sendNotify(string message)
        {
            lsColors.Items.Add(message);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListObsForm_Load(object sender, EventArgs e)
        {

        }
    }

  

   /// <summary>
    /// Observer Pattern 观察者
    ///20220918
    /// geovindu,Geovin Du,涂聚文
    /// </summary>
    public partial class ColObserverForm : Form, Observer 
    {
        private Brush bBrush;
        // private System.Windows.Forms.PictureBox pic;
        private Font fnt;
        private Hashtable colors;
        private string colName;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="subj"></param>
        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 = "";
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        public void sendNotify(string message)
        {
            colName = message;
            message = message.ToLower();
            Color col = (Color)colors[message];
            pic.BackColor = col;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void paintHandler(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.DrawString(colName, fnt, bBrush, 20, 40);
        }

        /// <summary>
        /// 
        /// </summary>
        public ColObserverForm(Subject subj)
        {
            InitializeComponent();
            init(subj);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ColObserverForm_Load(object sender, EventArgs e)
        {

        }
    }

  

调用:

   /// <summary>
    /// Observer Pattern 观察者
    ///20220918
    /// geovindu,Geovin Du,涂聚文
    /// </summary>
    public partial class ObserverPatternForm : Form, Subject
    {
        private ArrayList observers;

        /// <summary>
        /// 
        /// </summary>
        private void init()
        {
            EventHandler evh = new EventHandler(opButton_Click);
            opRed.Click += evh;
            opBlue.Click += evh;
            opGreen.Click += evh;
            observers = new ArrayList();
            ListObsForm lobs = new ListObsForm(this);
            lobs.Show();
            ColObserverForm colObs = new ColObserverForm(this);
            colObs.Show();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obs"></param>
        public void registerInterest(Observer obs)
        {
            observers.Add(obs);
        }
        /// <summary>
        /// 
        /// </summary>
        public ObserverPatternForm()
        {
            InitializeComponent();
            init();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ObserverPatternForm_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void opButton_Click(object sender, System.EventArgs e)
        {
            RadioButton but = (RadioButton)sender;
            for (int i = 0; i < observers.Count; i++)
            {
                Observer obs = (Observer)observers[i];
                obs.sendNotify(but.Text);
            }
        }
    }

  

输出:

 

标签:Observer,Pattern,void,private,CSharp,new,message,public
From: https://www.cnblogs.com/geovindu/p/16746309.html

相关文章

  • python正则(?:pattern)、(?=pattern)、(?!pattern)、(?<=pattern)和(?<!pattern)
    (?!pattern)、(?<=pattern)和(?<!pattern)importrestr="23456win56"str_suffix="23456win56"#先找到win,匹配到win的后面为56|12的winprint(re.search("win(?=......
  • CSharp: Mediator Pattern
     ///<summary>///SummarydescriptionforRectangle.///MediatorPattern中介者模式///20220918///geovindu,GeovinDu,涂聚文///<......
  • Java: State Pattern
    /***版权所有2022涂聚文有限公司*许可信息查看:*描述:*状态模式StatePattern*历史版本:JDK14.02*2022-09-12创建者geovindu*2022-09-12添加L......
  • Pointcut is not well-formed: expecting 'name pattern' at character position 53
    xml方式配置spring事务,出现的报错报错内容:org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'datasource'definedinclass......
  • [答疑]analysis patterns里为什么不是 父 0..1 子0..*
    ​​软件方法(下)分析和设计第8章连载[20210723更新]>>​​尘语<xn****qq.com>2016/4/2420:24:39请教一下analysispatterns里为什么不是父0..1子0..*潘加宇(3504847)......
  • CSharp: Command Pattern
     ///<summary>///definesCommandinterface///CommandPatterns命令模式///20220918///geovindu,GeovinDu,涂聚文///</summary>......
  • Java: Memento Pattern
     /***版权所有2022涂聚文有限公司*许可信息查看:*描述:*备忘录模式MementoPattern/SnapshotPattern/TokenPattern*历史版本:JDK14.02*2022-09-1......
  • Java: Command Patterns
     /***版权所有2022涂聚文有限公司*许可信息查看:*描述:*命令模式CommandPatterns*历史版本:JDK14.02*2022-09-12创建者geovindu*2022-09-12......
  • 面向机器学习工程师的 Python 设计模式:Observer
    Photoby杰森梁on不飞溅面向机器学习工程师的Python设计模式:Observer了解如何通过采用设计模式来构建代码:基于Instagram的示例介绍模式描述了一个经常出现的......
  • Java: Iterator/Cursor Patterns
    /***版权所有2022涂聚文有限公司*许可信息查看:*描述:*迭代器模式Iterator/CursorPatterns*历史版本:JDK14.02*2022-09-12创建者geovindu*2022......