Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

singleton

Singleton Design Pattern is basically limiting our class so that 
whoever is using that class can only create 1 instance from that class.
       
I create a private constructor which limits access to the instance of the class.
Than I create a getter method where we specify how to create&use the instance.
    - I am using JAVA Encapsulation OOP concept.
Comment

singleton

public final class Singleton {

  private static final Singleton INSTANCE = new Singleton();

  private Singleton() {}

  public static Singleton getInstance() { 
    return INSTANCE;
  }
}
Comment

singleton

let instance;
let counter = 0;

class Counter {
  constructor() {
    if (instance) {
      throw new Error("You can only create one instance!");
    }
    instance = this;
  }
}

const singletonCounter = Object.freeze(new Counter());
export default singletonCounter;
Comment

PREVIOUS NEXT
Code Example
Java :: android activity transition 
Java :: throw exception in spring boot with message and geeter se 
Java :: contain java 
Java :: how to initialize a variable in java 
Java :: java replaceall single character 
Java :: find subarray with given sum 
Java :: list of arrays 
Java :: types of exception in java 
Java :: camunda 
Java :: get the average of an array in java 
Java :: Java TestNG Data Provider example 
Java :: what does system.out.println(y + " ") result 
Java :: java truncate bigdecimal 
Java :: kingkaihockey 
Java :: start hadoop and yarn with java 
Java :: java transformar string a url 
Java :: java modulus opperation 
Java :: Printing Hexadecimal Code 
Java :: influx cli with docker container 
Java :: XmlRootElement Object to String 
Java :: Java create an object of the non-static class Reptile 
Java :: java object class 
Java :: class c { public static void main(string[] args){ system.out.println("hello"+args[0]);}} output 
Java :: convert boolean to Boolean class 
Java :: java hashmap get nonexistent key 
Java :: change order of words in string java 
Java :: which exception is thrown when java is out of memory 
Java :: Add space to the left and right sides of a cell 
Java :: java static nested class 
Java :: merced A class 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =