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

推荐订阅源

罗磊的独立博客
Cisco Talos Blog
Cisco Talos Blog
C
Check Point Blog
博客园_首页
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Martin Fowler
Martin Fowler
Recorded Future
Recorded Future
S
Security @ Cisco Blogs
L
LINUX DO - 最新话题
博客园 - 司徒正美
P
Privacy International News Feed
G
Google Developers Blog
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
K
Kaspersky official blog
I
InfoQ
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Webroot Blog
Webroot Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
D
Docker
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Microsoft Azure Blog
Microsoft Azure Blog
Spread Privacy
Spread Privacy
量子位
H
Hacker News: Front Page
Simon Willison's Weblog
Simon Willison's Weblog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
SecWiki News
SecWiki News
S
Security Affairs
Latest news
Latest news
人人都是产品经理
人人都是产品经理
C
CERT Recently Published Vulnerability Notes
S
Security Archives - TechRepublic
V
Visual Studio Blog
T
Troy Hunt's Blog
S
Secure Thoughts
F
Fortinet All Blogs
V
V2EX
The Register - Security
The Register - Security
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - FrankFei

创采人力资源管理软件--产品简介 创采e-HR荣获江苏省高新技术产品认定 个人经典收藏网址 创采人事档案管理系统(ASP.NET+ExtJS) 创采应用框架开发平台--CFrame 创采人事管理软件--分布式的人力资源管理解决方案 创采e-HR合作经营 创采人事管理软件 创采人力资源管理系统 使用.Net和ExtJS技术开发的人力资源管理系统 ObjectBuilder应用之TypeMappingPolicy、SingletonPolicy ObjectBuilder中IBuilderPolicy和IBuilderStrategy之区别 ObjectBuilder模式浅析 NHibernate连接多数据库字符定义问题 优化GridView的查询、翻页性能 名言警句 折叠内容 [转]NUnit2.0详细使用方法 设计模式--Prototype
ObjectBuilder中WeakRefDictionary使用模式浅析
FrankFei · 2007-08-02 · via 博客园 - FrankFei

FrankFei at 2007/08/02

大家在学习ObjectBuilder的Locator时,可能都知道里面是使用WeakRefDictionary作为它内部的存储结构,以实现对象的缓冲机制。那WeakRefDictionary的是如何实现的呢?其实它是采用对象适配器模式,把一个Dictioary泛型集合对象适配成符合存储弱引用对象的集合。

我们先来回顾一下对象适配器模式,下面是结构图:

目标(Target)角色:这是客户所期待的接口。目标可以是具体的或抽象的类,也可以是接口。
源(Adaptee)角色:需要适配的类。
适配器(Adapter)角色:通过在内部包装(Wrap)一个Adaptee对象,把源接口转换成目标接口。

我们再来看一下WeakRefDictionary的定义:

public class WeakRefDictionary<TKey, TValue>

     
{

         
private Dictionary<TKey, WeakReference> inner = new Dictionary<TKey, WeakReference>();

         
public WeakRefDictionary()

         
{

         }


         
public void Add(TKey key, TValue value)

         
{

              TValue dummy;

 

              
if (TryGet(key, out dummy))

                   
throw new ArgumentException(Properties.Resources.KeyAlreadyPresentInDictionary);

 

              inner.Add(key, 
new WeakReference(EncodeNullObject(value)));

         }


         
public bool Remove(TKey key)

         
{

              
return inner.Remove(key);

         }


         …

     }


上面只是列出一小部分代码,但从中我们可以看出一个简化的对象适配器模式的实现。结构图中的Adapter是继承自Target的,此处的WeakRefDictionary对应Adapter,但WeakRefDictionary却并没有继承任何类,所以我上面说这是一个简化的对象适配器模式的实现。那适配(Adaptee)的是什么对象呢?很显然,就是泛型Dictionary了,只是这个地方适配的方法名称是一样的,比如inner.Add(key, new WeakReference(EncodeNullObject(value)));对应于public void Add(TKey key, TValue value),inner.Remove(key);对应于public bool Remove(TKey key),而不像结构图中Adaptee的方法SpecificRequest()被适配成了Adapter中的方法Request()那么明显。

当然ObjectBuilder中定义这样一个Adapter:WeakRefDictionary不仅仅只是上面提到的那几个适配,我这里只是提出这样一个概念,上面只是我个人的理解,不当之处请指教!