" GetOrCreate" - 那个成语有一个既定的名字吗? [英] "GetOrCreate" - does that idiom have an established name?
本文介绍了" GetOrCreate" - 那个成语有一个既定的名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class FooBarDictionary
{
私人字典< String,FooBar> fooBars;
...
FooBar GetOrCreate(String key)
{
FooBar fooBar;
if(!fooBars.TryGetValue(key,out fooBar))
{
fooBar = new FooBar();
fooBars.Add(key,fooBar);
}
return fooBar;
}
}
有没有任何一种已建立的名字? p>
(是的,它是用C#编写的,但它可以轻松地传输到C ++,因此标签。)
解决方案
Lazy Loading
http://en.wikipedia.org/wiki/Lazy_loading
Ok, consider this common idiom that most of us have used many times (I assume):
class FooBarDictionary
{
private Dictionary<String, FooBar> fooBars;
...
FooBar GetOrCreate(String key)
{
FooBar fooBar;
if (!fooBars.TryGetValue(key, out fooBar))
{
fooBar = new FooBar();
fooBars.Add(key, fooBar);
}
return fooBar;
}
}
Does it have any kind of established name?
(Yes, it's written in C#, but it can be "easily" transferred to C++. Hence that tag.)
解决方案
Lazy Loading
http://en.wikipedia.org/wiki/Lazy_loading
这篇关于" GetOrCreate" - 那个成语有一个既定的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文