且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

" GetOrCreate" - 这是否成语有一个既定的名字吗?

更新时间:2022-10-23 20:10:04

延迟加载



http://en.wikipedia.org/wiki/Lazy%5Floading


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%5Floading