Design patterns: cataloged, familiar, repeatedly used code design lessons

The singleton pattern

Purpose: To make an object of a class a unique instance in the system of that class

Definition: A class has one and only one instance, and its own instantiation is provided to the entire system

Important: 1. Only one instance of a class can exist

2. You must create an instance

3. You must provide this instance to the entire system yourself

Implementation: 1. Provide only private constructors

2. A static private object that contains a class

3. Provide a static public method to create and obtain static private objects

Advantages: 1. Only one object can be stored in memory, saving memory space

2. Avoid frequent object creation and destruction to improve performance

3. Avoid multiple occupation of shared resources

Disadvantages: 1. Difficult to expand

2. If an instantiated object is not used for a long time, the system collects the object as garbage by default, causing object status loss

Usage scenario: 1. Creating an object occupies too many resources, but the object needs to be created

2. Uniformly read and write system resources, such as read and write configuration information

3. If multiple instances exist, program logic errors may occur, such as number generator

Code implementation scheme:

One, hungry:

Instantiation during object creation

Thread safety

package com.imooc.singleton; Public class SlingletonOne {//1. Create private constructor in class private SlingletonOne() {} //2. Private static SlingletonOne instance=new SlingletonOne(); Create a public static method that returns a static instance object //4. When this method is statically decorated, there is one more method that can be called directly by the class name in addition to the object: the class name. Public static SlingletonOne getInstance() {return instance; } } package com.imooc.test; import com.imooc.singleton.SlingletonOne; public class Test { public static void main(String[] args) { SlingletonOne one=SlingletonOne.getInstance(); SlingletonOne two=SlingletonOne.getInstance(); System.out.println(one); system.out.println (one); System.out.println(two); }}Copy the code

Two, lazy style:

Thread risk

Instantiation in a static public method

package com.imooc.singleton; Public class singletonTwo {//1 public class singletonTwo {//1 Create private singletonTwo() {} //2. Private static singletonTwo Instance =null; Public static singletonTwo getInstance() {if(instance==null) {instance=new singletonTwo(); } return instance; } } singletonTwo one=singletonTwo.getInstance(); singletonTwo two=singletonTwo.getInstance(); System.out.println(one); System.out.println(two);Copy the code

Do class members ina singleton need final decoration?

private static final SlingletonOne instance=new SlingletonOne();
Copy the code

1. We cannot add final to lazy expressions. Static members modified by final should be implemented in static code blocks if they are not instantiated when they are defined

2. If you have handier code that only creates and returns methods for class instances, you can add final qualifiers