惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
V
Vulnerabilities – Threatpost
A
Arctic Wolf
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
Hugging Face - Blog
Hugging Face - Blog
H
Hacker News: Front Page
D
Docker
人人都是产品经理
人人都是产品经理
Attack and Defense Labs
Attack and Defense Labs
Forbes - Security
Forbes - Security
I
InfoQ
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
aimingoo的专栏
aimingoo的专栏
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Simon Willison's Weblog
Simon Willison's Weblog
腾讯CDC
WordPress大学
WordPress大学
T
Tenable Blog
P
Proofpoint News Feed
月光博客
月光博客
T
Tor Project blog
The Cloudflare Blog
罗磊的独立博客
S
Secure Thoughts
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Hacker News
The Hacker News
P
Palo Alto Networks Blog
I
Intezer
小众软件
小众软件
N
News | PayPal Newsroom
V
Visual Studio Blog
L
LINUX DO - 最新话题
W
WeLiveSecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Troy Hunt's Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园_首页
D
DataBreaches.Net
P
Privacy International News Feed
博客园 - 三生石上(FineUI控件)
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Security Affairs
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
阮一峰的网络日志
阮一峰的网络日志

博客园 - Sherrys

Content-Type 参数 Sql 2000 中行转列的查询方法 DotNet Framework 小技巧 使用 .NET 2.0 SecureString 类保护敏感数据 API参数说明符前缀详解 C#中调用Windows API的要点 - Sherrys - 博客园 SQL 处理简单的 Xml Special Considerations When Using Query Notifications 利用.NET Framework使用RSS feed SQL远程连接 SQL 事务的隔离 SELECT 语句收藏(2000 & 2005) 自动处理 SQL 2005 表格数据 SQL事务的使用 用JS让文章内容指定的关键字加亮 - Sherrys - 博客园 查询SQL连接数的方法 对象的继承方案 使用 ICallbackEventHandler aspx页面中的DropDownList 的 SelectValue 出现中文导致不回调方法的问题 如何利用SQL Server 2005数据库快照形成报表
通过 INotifyPropertyChanged 实现观察者模式
Sherrys · 2007-06-29 · via 博客园 - Sherrys

普通观察者模式存在的问题

我们都知道观察者模式的优点,可以在属性发生改变时,来监听一个实现好了的事件,这样可以帮助来异步处理许多的事情,可是在观察者模式中,一般都是针对于实体来进行封装操作,可大多数的监听过程就需要写在实体的内部了,这样大大影响了实体封装的原子性,如果想实现什么监听事件,都需要在实体层进行更改,这另人太不悦了```

不过,可以通过代理的方式,让实体里面监听的时候执行这个代理就好了,代理的功能由外部仍进来,不过这样也是破坏了实体本身,需要由外部来传递参数,这样对于底层开发人员来说,也不是很愉快的事情。

实现 INotifyPropertyChanged

怎么样才能既实现了监听,也不破坏实体的特性,让监听的事件外露呢?所以通过实现 INotifyPropertyChanged来解决这一问题。

原型:event PropertyChangedEventHandler PropertyChanged

下面这个例子将详细说明用法:

using System;

using System.Collections.Generic;

using System.Text;

using System.ComponentModel;

namespace Demo

{

class Program

{

public static void Main(string[] args)

{

// 创建实例

DemoCustomer demoCustomer = DemoCustomer.CreateNewCustomer();

// 实现事件触发需要处理的事情

demoCustomer.PropertyChanged += new PropertyChangedEventHandler(demoCustomer_PropertyChanged);

// 更新值 引发事件

demoCustomer.PhoneNumber = "100";

// 等待

Console.ReadLine();

}

static void demoCustomer_PropertyChanged(object sender, PropertyChangedEventArgs e)

{

// 获取被更改的属性名

Console.WriteLine(e.PropertyName);

// 获取最新更新的值

Console.WriteLine(((DemoCustomer)sender).PhoneNumber);

}

}

// 实现 INotifyPropertyChanged 接口 进行监听

public class DemoCustomer : INotifyPropertyChanged

{

// 默认的私有属性

private Guid idValue = Guid.NewGuid();

private string customerName = String.Empty;

private string companyNameValue = String.Empty;

private string phoneNumberValue = String.Empty;

/// <summary>

/// 在更改属性时引发的事件。(这个事件将被外露。)

/// </summary>

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String info)

{

if (PropertyChanged != null)

{

PropertyChanged(this, new PropertyChangedEventArgs(info));

}

}

// 默认的构造

private DemoCustomer()

{

customerName = "no data";

companyNameValue = "no data";

phoneNumberValue = "no data";

}

// 简单的工厂模式

public static DemoCustomer CreateNewCustomer()

{

return new DemoCustomer();

}

public Guid ID

{

get

{

return this.idValue;

}

}

public string CompanyName

{

get

{

return this.companyNameValue;

}

set

{

if (value != this.companyNameValue)

{

this.companyNameValue = value;

// 当发生改变时,那么就触发事件,传入触发的属性名

NotifyPropertyChanged("CompanyName");

}

}

}

public string PhoneNumber

{

get

{

return this.phoneNumberValue;

}

set

{

if (value != this.phoneNumberValue)

{

this.phoneNumberValue = value;

// 当发生改变时,那么就触发事件,传入触发的属性名

NotifyPropertyChanged("PhoneNumber");

}

}

}

}

}