As in the output above we can see that the value have been printed thrice, which means that on every get call a new instance of CustomObject is being created. From the output, we can simply see that Value: 1 has been printed only once and on every call the hascode of the object is same. We made a new class to lazily initialised the class and also maintained to create the only single instance of a class. As per the code output above, we can verify that value:3 and value:4 has been printed which means that multiple instance of same object has been created.
Also we can make the value property as volatile so that its value is write and read form the main memory only and not from the CPU cache. Note : We can also implement the double check locking using synchronised block to make the code more proficient but to keep the simplicity for this code, i am just making the whole method synchronised.
This Post was first published on my Personal blog website. This is all from this Post, if you find anything wrong or have any other thoughts on the same , please share in the comment box below :. Thoughtworks Insights Careers Products.
Martin Fowler. It corresponds to the real object but not in its full state. It may be empty or it may contain just some fields such as the ID. At the time of creation all we know is that content will be accessed but what action or content is unknown to the user. UID is the unique id for the every particular user.
Mostly it is text but depends on the form. Advantages This approach is a faster application start-up time as it is not required to created and load all of the application objects. Disadvantages The code becomes complicated as we need to check if loading is needed or not. So this may cause slower in the performance. This article is contributed by Saket Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Skip to content. Change Language. Like so:. The configuration method now uses a method reference to createConfiguration. This variant also allows seperate configuration instances for each file name passed, which can be helpful in some situations.
The code is, again, functionally equivalent to its predecessors but now uses a ConcurrentHashMap to do the synchronization work; there's no niggling synchronized keyword left.
The Optional and Map variants may be a picosecond slower than the synchronized block, but who knows if that will ever makes a difference, taking into account that Optional. Here is the output of my inlining log, which suggests that the whole Optional. I am still inclined to make this code shorter. It's a question of honor, but also:. This variant now turned the whole code of the previous variants into a one liner. It still serves the same thread-safe purpose and solves all the other listed requirements.
But instead of using the Configuration object as value, I am using ConfigurationBuilder in my map. By looking into the JavaDoc of its getConfiguration method, I noticed that it's caching its results. This is perfect. Functionally equivalent to the previous idioms, but thanks to Java's method references and some patience to look deeper into the API, everything is in one single, short line now.
0コメント