1、 Concept :

java The singleton pattern is a common design pattern , There are three singleton patterns : An example of lazy style 、 Hungry han singleton 、 There are three types of registration .
Singleton mode has the following characteristics :
1、 A singleton class can have only one instance .
2、 Singleton classes must create their own unique instances .
3、 The singleton class must provide this instance to all other objects .

The singleton pattern ensures that a class has only one instance , And instantiate it yourself and provide it to the entire system . In computer system , Thread pool 、 cache 、 Log object 、 Dialog box 、 The printer 、 The driver object of a graphics card is often designed as a singleton . These applications have more or less the function of resource manager . Each computer can have several printers , But only one Printer Spooler, To avoid two print jobs being output to the printer at the same time . Each computer can have several communication ports , The system should centrally manage these communication ports , To avoid a communication port being called by two requests at the same time . All in all , The singleton mode is chosen to avoid inconsistent states , Avoid political bulls .

2、 Illustrate with examples :

2.1 An example of lazy style

 /**
* Singleton mode test :
* --- The sluggard model
* @author Little wind and little spirit
*
*/
public class SingleMode { /*
* First step : Create a private object of its own : Not initialized
*/
private static SingleMode single=null; /**
* The second step : Privatized constructor : So that the class cannot be new Object out
*/
private SingleMode(){} /**
* The third step : A static method to get private objects
*/
public static SingleMode getInstance(){ if(single==null){
single=new SingleMode();
}
return single;
}
}

2.2 Hungry han singleton

 /**
* Singleton mode test :
* --- Starving model
* @author Little wind and little spirit
*
*/
public class SingleMode_EH {
/*
* First step : Create a private object of its own : initialization
*/
private static SingleMode_EH single_=new SingleMode_EH(); /**
* The second step : Privatized constructor : So that the class cannot be new Object out
*/
private SingleMode_EH(){} /**
* The third step : A static method to get private objects
*/
public static SingleMode_EH getInstance(){ return single_;
}
}

2.3 Registration form

 package mode_single;
import java.util.HashMap;
import java.util.Map; /**
* Singleton mode test :
* --- Registered singleton mode
* @author Little wind and little spirit
*
*/
public class SingleMode_DJS {
private static Map<String,SingleMode_DJS> map = new HashMap<String,SingleMode_DJS>();
/**
* A static block : Store the instance in the table in advance Map in
*/
static{
SingleMode_DJS single = new SingleMode_DJS();
map.put(single.getClass().getName(), single);
}
/**
* Constructors : The protected
*/
protected SingleMode_DJS(){}
/**
* Static factory method , Return the only instance of this class
* @param name Map Instances in the collection " key ": The name of
* @return Returns the registered singleton object
*/
public static SingleMode_DJS getInstance(String name) {
if(name == null) {
name = SingleMode_DJS.class.getName();
}
if(map.get(name) == null) {
try{
map.put(name, (SingleMode_DJS) Class.forName(name).newInstance());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return map.get(name);
}
}

Java Design patterns ( Learn to organize )--- More articles on singleton patterns

  1. Java Design pattern learning record - The singleton pattern

    Preface I've introduced and learned two creative patterns , Today, let's learn about another very common creation pattern , The singleton pattern . Singleton mode is also called singleton mode ( Or monomer mode ), The main function is to control the number of instances of a certain type , And there's only one . The singleton pattern ...

  2. Java Design pattern learning 01—— The singleton pattern ( turn )

    The original address :http://blog.csdn.net/xu__cg/article/details/70182988 Java Singleton pattern is a common and simple design pattern . The singleton pattern , As the name suggests, a class can only have one instance ...

  3. Java Design patterns learning resources summary

    This article records Java Design patterns learning books . Tutorial resources . This share will continue to update : 1. Design pattern book I searched Douban for , Discover that design patterns run through all aspects of human life . Or go back Java Come with programming . I'm going to categorize first , Browse again , from ...

  4. Java Design pattern 《 The singleton pattern 》 And application scenarios

    Abstract : Original works , Can be reprinted , But please mark the source address :http://www.cnblogs.com/V1haoge/p/6510196.html The so-called single example , It means single instance , There is only one class instance , This single example shouldn't be ...

  5. C# Design pattern learning notes - The singleton pattern ( turn )

    C# Design pattern learning notes - The singleton pattern  http://www.cnblogs.com/xun126/archive/2011/03/09/1970807.html I'm learning design patterns recently , When you learn to create patterns , meet ...

  6. Java Design pattern learning record - Template method pattern

    Preface Template method pattern , Define the skeleton of an algorithm in operation , Instead, defer some steps to subclasses . So that subclasses can redefine some specific steps of an algorithm without changing the structure of the algorithm . Template method pattern The concept is introduced Template method pattern , Actually, it's very easy to understand , Specifically ...

  7. Java Design pattern learning record - The state pattern

    Preface State mode is a kind of behavior mode , It is used to solve the problems of complex object state transition and encapsulation in various states . The state pattern is to separate the state of an object from the object , Encapsulate into specialized state classes , So that the state of the object can be flexible . In this way, on the client side ...

  8. Java Design pattern learning record - Observer mode

    Preface The observer pattern is also one of the object behavior patterns , It's also called publishing - subscribe (Publish/Subscribe) Pattern . Model - View (Model/View) Pattern . What we use most at present are all kinds of MQ(Message Queue ...

  9. Java Design pattern learning record - Memo mode

    Preface This time I'm going to introduce the memo model , It's also a pattern of behavior . Now people's smartphones will have a memo function , Everybody can use it , Just to remember something , In case you forget later . So what does the memo model look like ? It's not with cell phones ...

  10. Java Design pattern learning record - Iterator pattern

    Preface This time we're going to talk about the iterator pattern , It's also a pattern of behavior . Now I think blogging is a bit of a deal , A while ago, one article a day , In fact, I didn't understand it thoroughly, so I wrote it down , And I didn't read it many times after I wrote it , Last time I was asked in the interview java Medium I/ ...

Random recommendation

  1. stay Oracle Linux Upper use DTrace Relevant guidance for

    If you use it Oracle Linux, because sun By Oracle After the acquisition ,Oracle Linux Version of DTrace Can be directly in Oracle Download on the official website . Download address http://www.oracle.co ...

  2. Several sql sentence

    1. That's ok . Column conversion -- Transfer line column ), subject ), fraction int) ) ) ) ) ) ) ) ) ) -- Method 1 select full name , end) as Chinese language and literature , end) as mathematics , end) as Physics f ...

  3. 《C++ Primer》 Reading notes — Chapter two Variables and basic types

    Statement : The content of this article is collected from <C++ Primer Chinese version ( The first 5 edition )>, The copyright belongs to the original book . The best way to learn a programming language is to practice programming . 1.8 Bit char The actual scope of the type computer representation is -12 ...

  4. SQL Server -- Recall notes ( 5、 ... and ):T-SQL Programming , System variables , Business , The cursor , trigger

    SQL Server -- Recall notes ( 5、 ... and ):T-SQL Programming , System variables , Business , The cursor , trigger 1. T-SQL Programming (1) Declare variables declare @age int (2) Assign values to variables (3)while loop ...

  5. Practice every day ACM

    2019.04.15 The first 1000 topic :A+B Problem Problem DescriptionCalculate A + B. InputEach line will contain two in ...

  6. Light detectors LightProbe[Unity]

    The purpose of the light detector , The words in the manual are : Although lightmapping adds greatly to the realism of a scene, it has the disadvan ...

  7. Flume exec test

    Environmental Science :ubuntu 1604 Software :①apache-flume-1.7.0-bin.tar.gz, Decompress and put it in /usr/local/  below .②sudo apt-get install apache2 ...

  8. Something worth learning Unity course ( Very practical, including the source code )

    *********************** Project source code ******************************** 1. come 2. Sleeping and having fun 3. Fishing with a thousand guns 4. Photon official FSP Example ...

  9. Safe Or Unsafe--hdu2527( Huffman tree asks for WPL)

    Topic link :http://acm.hdu.edu.cn/showproblem.php?pid=2527 Simulate with priority queues #include<iostream> #include<std ...

  10. Simply understand HTTPS turn

    Original address :http://www.nowamagic.net/librarys/veda/detail/2394 We all know HTTPS Be able to encrypt information , To prevent sensitive information from being acquired by third parties . So many bank websites or e-mails, etc ...