弱引用或工厂模式

Weakreference or Factory Pattern

我阅读了一些有关 C# 中的弱引用 Class 的内容,但我不确定它的用法。

我有一个巨大的 table,其中包含超过 10 或 20k 个条目。 例如,当我编写一个 List DBFoos 时,其中 Foo 是数据库中的对象。

如果用户只读且模式仅用于 类 之间的通信,那么使用弱引用而不是 Factory/Singelton 模式的优势在哪里,因为下载对象需要一段时间?

编辑

我在 MSDN

上找到了一个例子

他们使用弱引用字典,例如:

public class Cache
{
    // Dictionary to contain the cache.
    public static Dictionary<int, WeakReference> _cache;

如果我在此缓存我的对象而不是使用像这样的 Singelton 对象:

public class Foo{
private static Foo foo;
static Foo getreference(){

if(foo == null){
 foo = new Foo();
 return foo;
} 
else 
 return foo;
}
}

使用 Wea​​kreference 由 GC 收集是否有任何优点或缺点,或者它只是一种复杂的保存方式?

Weakreference是用来做缓存的,你把weakreference存在缓存里让垃圾收集器来做这件事,所以有时候你不得不从数据库中重新加载一些元素。 使用 Singleton 时,它会生成静态缓存,不会被垃圾化,但如果有足够的内存,它可以正常工作。

Is there a practical use for weak references?