Good or bad practice? Initializing objects in getter

前端 未结 9 1774

I have a strange habit it seems... according to my co-worker at least. We\'ve been working on a small project together. The way I wrote the classes is (simplified example):<

9条回答
  •  执笔经年
    2020-12-07 07:43

    Do you consider implementing such pattern using Lazy?

    In addition to easy creation of lazy-loaded objects, you get thread safety while the object is initialized:

    • http://msdn.microsoft.com/en-us/library/dd642331.aspx

    As others said, you lazily-load objects if they're really resource-heavy or it takes some time to load them during object construction-time.

提交回复
热议问题