Best Practices of Java Singleton Design Pattern

Singleton Pattern

We have different approaches but all of them have following common concepts to implement Singleton pattern:

  • Private constructor which restricts instantiation of the class from other classes.
  • Private static variable of the same class that is the only instance of the class.
  • Public static method that returns the instance of the class, this is the global access point for outer world to get the instance of the singleton class.

Where do you use the Singleton pattern?

Most of the scenarios, Singleton classes are created for resources such as File System, Database connections etc…

Continue reading

Advertisements

PHP Closure and Lazy Loading

What is the Closures?

Let’s define firstly what is the anonymous function? If you are a front-end developer and have worked with JavaScript, you should be already familiar with anonymous functions. These pretty useful to write short inline functions, define callback etc.

An anonymous function is a function that was declared without any named identifier to refer to it. Here is the PHP implementation of anonymous function:

<?php
$anonymousfunctionReference = function(){
    echo "Anonymous function called"
};

echo $anonymousfunctionReference();

Continue reading