package com.fish.object;/* single-case design pattern: Ensure that a class has only one object in memory. Mode: Mode is the fixed step to solve a kind of problem. The concept of mode originated from the construction industry .... the steps of building a house are the same.: the foundation-----> pouring pillars-------> cover---------> Drywall--------- > Capping----> Decoration-----Stay 23 Design patterns in the software industry: single case design mode template design mode Decorator Design Patterns Observer design Patterns Factory Design Patterns Single-case design mode steps: A hungry man single case design mode 1. privatization constructor. 2. declares a reference type variable of this class, and uses that variable to point to this class of object. 3. provides a public static way to get objects of this class. Lazy Single-case design mode: 1. privatization constructor. 2. declares a reference type variable for this class, but does not create an object, 3. provides public static method to get the object of this class , get the first to determine whether you have created this class object  , if you have already created, then directly return the object, if not created, then first create the object of this class, then back again. recommends using: a hungry man single-case design mode. because the lazy singleton design pattern will exist threadSecurity issues, there is no guarantee that a class has only one object in memory. */ //a hungry man single case design mode ----> Ensure that there is only one object in memory. class single { // declares a reference type variable of this class and uses it to point to this class of objects Private static single s = new single (); // privatization constructor private single () { } // Provides a public static way to get objects of this class public static single getinstance () { return s; }}// Lazy Single example design mode ----> guarantees that single has only one object in memory. class single2 { // declares a reference type variable of this class and does not create an object of this class private static Single2 s; // privatized the constructors private Single2 () { } // public static single2 gEtinstance () { if (s == null) { s = new single2 (); } return s; }}class demo5 { public static void main (String[] args) { single2 s1 = single2.getinstance (); single2 s2 = Single2.getinstance (); system.out.println ("is that the same object? " + (S1 == S2)); }}
This article from "Small Fish Blog" blog, declined reprint!
Java single-Instance design pattern