Explain in detail what the singleton pattern is? Why use it? how to use?

The first lesson of learning the introduction to design patterns is generally a singleton pattern. Basically every programmer will use the singleton pattern, which is also the simplest design pattern. This article will use Android to explain, and attach the singleton mode implementation method of Object-C and JavaScript at the end of the article. Comments are welcome for inappropriate explanations

What is the singleton pattern? In fact, it is very simple to say. Some classes need to be available no matter where they are used, and some of their attributes need to be shared among different users, and it is not convenient to create multiple times. In this case, we should use the singleton mode.

Suppose we have a tool class Util, which encapsulates the method WriteMyName to write the author's name. The name of the author is output every time it is called. I need to use this tool class when writing the FristActivitiy class, so I decisively call the following statement

 Util util = new Util();
 unil.writeMyName();

This creates a util object.
Then when using the SecondAcitivty class, I used util to write my name on it.
So I wrote Util util = new Util();
at this time I created another util object.
When I write 100 classes, 100 objects will be created. If this object is very large, it will undoubtedly cause memory waste. This is the first problem solved by the singleton mode, resource overhead.
If my name is stored in this class, and I change it one day, then I can only either modify the Unit class, or modify it one by one in the subclass

The singleton design pattern is very simple to understand, that is, there is only one instance of a class, no matter which function you create or use in, there is always only one instantiated object. To put it simply, it is Util util = new Util(
) ;
This statement will only be used once, and we will use this new util when we use it anywhere in the future. How to do it? It is to prevent any function other than util from using this instantiation method, only util can use it, and then pass this object out to outsiders for use. I believe that seeing this, anyone can write a singleton design pattern
public class Util () { private Util () {} - let the constructor private private static final Util util=new Util (); public static Util getutil () {return util;} } This is the simplest singleton design pattern without any requirements. You only need to privatize its constructor, and then write a function that can pass your instantiated object to the outside, and you are done. up. But think about it carefully. The defect of this singleton design pattern is very big, it has no judgment, does this class exist? Is it being created? Is it safe to use in a thread? None of these issues are considered, only created. But this method can actually cope with various simple application scenarios. If you consider using it in a thread, you have to optimize the code. Here is an example written by my colleague as a demonstration `package import android.media.MediaPlayer;








/**

  • Created by YI on 2017/7/31.
    */

public class MediaPlayerInstance { private static MediaPlayerInstance instance = null; private MediaPlayer mediaPlayer = null; private MediaPlayerInstance(){ mediaPlayer = new MediaPlayer(); } public static MediaPlayerInstance getInstance(){ if (instance == null){ synchronized (MediaPlayerInstance.class ){ if(instance == null){ instance = new MediaPlayerInstance(); } } } return instance; } } ` This code looks annoying, because many loops are nested together, it is called double lock mode, which is the first The first time to judge whether this instance exists, if so, we don't need to create it, and the second time to judge whether it is being created, if it is, we don't need to create it. This method has a high probability to ensure that the singleton object we create is unique

















Let's talk about a singleton mode implemented by java construction inner class

public class Util(){
private Util(){}

public static util() getUtil(){
return get.util 
}
private static class get{
private static final Util util = new util();
}
}

Each inner class has no name, so it cannot be called from the outside, and each inner class will be destroyed after finishing its work. This is also the best way to create a singleton pattern in java. Let’s write it here today
.

Guess you like

Origin blog.csdn.net/qq_36740186/article/details/76553310