lazy-initialization

Singleton object- In static block or in getInstance(); which should be used

穿精又带淫゛_ 提交于 2019-12-07 03:25:14
问题 Below are two ways to implement a singleton. What are the advantages and disadvantages of each? Static initialization: class Singleton { private Singleton instance; static { instance = new Singleton(); } public Singleton getInstance() { return instance; } } Lazy initialization is: class Singleton { private Singleton instance; public Singleton getInstance(){ if (instance == null) instance = new Singleton(); return instance; } } 回答1: Synchronized Accessor public class Singleton { private static

Why do the courses at Stanford use the lazy initialisation? [duplicate]

我是研究僧i 提交于 2019-12-06 04:11:30
This question already has answers here : When to use lazy instantiation in iOS? (4 answers) Closed 5 years ago . Why does the course at Stanford use the lazy initialization for all getters? Is this correct? Does it have any real advantage? One advantage (for me) is that the init method can become much shorter and you need not check if a variable is allocated. The idea is to load resources on demand. This way everything loads faster and when needed. In the cases it's not used, it doesn't allocate additional memory. 来源: https://stackoverflow.com/questions/21764666/why-do-the-courses-at-stanford

How to initialize an array whose size is initially unknown?

拟墨画扇 提交于 2019-12-05 16:26:40
Say I have this: int x; int x = (State Determined By Program); const char * pArray[(const int)x]; // ?? How would I initialize pArray before using it? Because the initial size of the Array is determined by user input Thanks! You cannot initialize an array at compile-time if you are determining the size at run-time. But depending on what you are trying to do, a non-const pointer to const data may provide you with what you're going for. const char * pArray = new const char[determine_size()]; A more complete example: int determine_size() { return 5; } const char * const allocate_a( int size ) {

Overriding property getters with lazy loading in Objective-C

纵饮孤独 提交于 2019-12-05 07:09:24
I usually lazy instantiate my @property objects in their getter methods like this: @interface MyGenericClass : UIViewController @property(nonatomic, readonly) UIImageView *infoImageView // ... @implementation GenericClass - (UIImageView *)infoImageView { if (!_infoImageView) { _infoImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"PlaceholderInfoImage"]]; } return _infoImageView; } But when subclassing, I would often like to override some of the @properties to be more subclass specific. So I'd like to change the instantiation and do something like: @interface

Singleton object- In static block or in getInstance(); which should be used

我的未来我决定 提交于 2019-12-05 06:20:10
Below are two ways to implement a singleton. What are the advantages and disadvantages of each? Static initialization: class Singleton { private Singleton instance; static { instance = new Singleton(); } public Singleton getInstance() { return instance; } } Lazy initialization is: class Singleton { private Singleton instance; public Singleton getInstance(){ if (instance == null) instance = new Singleton(); return instance; } } Alexander Synchronized Accessor public class Singleton { private static Singleton instance; public static synchronized Singleton getInstance() { if (instance == null) {

gcc attributes for init-on-first-use functions

隐身守侯 提交于 2019-12-05 04:31:35
I've been using the gcc const and pure attributes for functions which return a pointer to "constant" data that's allocated and initialized on the first use, i.e. where the function will return the same value each time it's called. As an example (not my usage case, but a well-known example) think of a function that allocates and computes trig lookup tables on the first call and just returns a pointer to the existing tables after the first call. The problem: I've been told this usage is incorrect because these attributes forbid side effects, and that the compiler could even optimize out the call

Swift Lazy and Optional properties

℡╲_俬逩灬. 提交于 2019-12-04 10:55:31
What is the difference between a Lazy or Optional property in Swift? For example, if someone is building a navigation bar that comes in from the side, I think that should all be within one UIViewController . The user might never open the menu but sometimes they will. var menu: NavigationBar? lazy var menu: NavigationBar = NavigationBar.initialize() Both of the optional I think are good code, because they don't create the view unless its needed. I understand Optional means there might be a value it might be nil . I also understand Lazy means don't worry about it until I need it. Specific

Spring 3.0 lazy-init not honoured for DefaultMessageListenerContainer?

自古美人都是妖i 提交于 2019-12-04 04:05:59
问题 I've setup a spring config for JMS. Things work fine, except I can't seem to get it to lazy load (notice the default-lazy-init true in the code below). If I comment out the jmsContainer(DMLC) from my config below, lazy loading works as expected. Otherwise, it will instantiate the DMLC, which in turn creates the queue and connection factory. What am I missing? jmsContext.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www

Why would I want to re-implement lazy?

懵懂的女人 提交于 2019-12-04 01:31:34
I was reading the section on Lazyness [sic] over at Twitter's Effective Scala page, which includes this suggestion (emphasis is mine): Use lazy fields for this purpose [computing and caching values on-demand], but avoid using lazyness when lazyness is required by semantics . In these cases it's better to be explicit since it makes the cost model explicit, and side effects can be controlled more precisely. I don't understand why they would make this claim. Why would it be better to avoid using the lazy keyword for cases when laziness is required by the semantics (meaning that it's necessary for

On lazy instantiation and convenience methods

半城伤御伤魂 提交于 2019-12-03 18:05:35
问题 Assume you have a Singleton Constants class, instance of which you'd like to use throughout your application. In someClass , therefore we can reference [Constants instance] someCleverConstant]; Typing this gets old really quick and it would be nice to get a shortcut to the instance. In someClass , we can declare @property (nonatomic, weak, readonly) Constants *constants; And a getter to the instance -(Constants*) constants { if (constants == nil) constants = [Constants instance]; return