• CSharp:Observer Pattern


        /// <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);
                }
            }
        }
    

      

    输出:

  • 相关阅读:
    将图片制作数据拒
    python中PIL.Image,OpenCV,Numpy图像格式相互转换
    9、【Hive】初始化元数据库时失败,遇到org.apache.hadoop.hive.metastore.HiveMetaException: Schema initialization FAILED! Metastore state would be inconsistent
    8、【Hive Mysql】MySQL8 提示Public Key Retrieval is not allowed错误解决方法
    7.【Hive】hive启动出现权限错误 /tmp/hive on HDFS should be writable
    一、Zookeeper简明笔记
    6、System times on machines may be out of sync. Check system time and time zones
    5、利用搭建的hadoop集群计算wordcount时出现内存问题
    4、利用hadoop自带的mr 示例测试运行任务失败: Could not find or load main class org.apache.hadoop.mapreduce.v2.app.MRAppMaster
    3、hadoop运行jar包报错 : "/bin/bash: /bin/java: No such file or directory"
  • 原文地址:https://www.cnblogs.com/geovindu/p/16746309.html
Copyright © 2020-2023  润新知