zoukankan      html  css  js  c++  java
  • .net中的观察者模式(用delegate/event实现)

    using System;

    namespace ConsoleApplication1
    {


        
    //用户界面(观察者1)
        public class SomeKindOfUI
        
    {
            
    public void Show(object anObject)
            
    {
                
    if (anObject is SomeData)
                
    {
                    ImpShow((SomeData)anObject);
                }

            }


            
    public void ImpShow(SomeData data)
            
    {
                Console.WriteLine(
    "Observe1. The new ask price is: " + data.AskPrice);
            }

        }


        
    //用户界面(观察者2)
        public class AnotherKindOfUI
        
    {
            
    public void Show(object anObject)
            
    {
                
    if (anObject is SomeData)
                
    {
                    ImpShow((SomeData)anObject);
                }

            }


            
    public void ImpShow(SomeData data)
            
    {
                Console.WriteLine(
    "Observe2. The new ask price is: " + data.AskPrice);
            }

        }



        
    //业务数据(被观察对象)
        public class SomeData 
        
    {

            
    public delegate void UpdateHandler(object sender);
            
    public event UpdateHandler UpdateEvent;

            
    //被观察者中的数据
            float _askPrice;

            
    //改变数据的属性
            public float AskPrice 
            
    {
                
    set 
                
    {
                    _askPrice 
    = value;
                    
    if (UpdateEvent != null)
                        UpdateEvent(
    this);
                }

                
    get
                
    {
                    
    return _askPrice;
                }

            }

        }
     


        
    /// <summary>
        
    /// Summary description for Class1.
        
    /// </summary>

        class Class1
        
    {
            
    /// <summary>
            
    /// The main entry point for the application.
            
    /// </summary>

            [STAThread]
            
    static void Main(string[] args)
            
    {
                SomeKindOfUI ui 
    = new SomeKindOfUI();
                AnotherKindOfUI anoth 
    = new AnotherKindOfUI();
                SomeData data 
    = new SomeData();
                data.UpdateEvent 
    += new SomeData.UpdateHandler(ui.Show);//observer1
                data.UpdateEvent += new SomeData.UpdateHandler(anoth.Show);//observer2
                data.AskPrice = 6789.2f;
            }

        }

    }

  • 相关阅读:
    red hat linux下安装mysql
    oracle数据库sys与system默认密码
    eclipse 创建maven web项目
    mysql-5.7.17-winx64的安装配置
    有趣的浏览器地址栏JavaScript代码
    jsp自定义标签Tag
    exp:CollectionSecurity must be empty, but is not; jsp自定义标签异常
    java中&与&&的区别
    解决springmvc在multipart/form-data方式提交请求在过滤器Filter中获取不到参数的问题
    java itextpdf使用HTML模板生成pdf文件,并设置table
  • 原文地址:https://www.cnblogs.com/silva/p/266502.html
Copyright © 2011-2022 走看看