The Singleton In My Minds

Definition:Assure a Class has only one Instance and it provides a Public access.


1,  question:Why do we use singleton?

 Answer:

Sometimes we need only one object。For example,threadpool,cache,log object.If we have more than one object,it will produce a lot of problems. For instance,program’s exception action and resources is used excessively. If we did this, it will produce different result that will confuse us and that is not expected.

 

Sometimes we need only one object. For example, in a team we have a team leader and he will make some decisions and be responsible for it. So we do not need second person to make a decision though he is willing to undertake the due obligations.

 

Sometimes we need only one object cause of the object is so huge and it will take up a lot of memory resources.If this object in Method Area (Permanent Generation) of Running Time Data Area of JVM, so it is hardly recycled and reused.

 

2, Question:When JVM starts up, it creates an object. But during the whole time the object is not used only once. So it is so wasteful. What do you think?

 

Answer: Don’t worries. When we need an instance, then we create it.

 

3, Question:How to assure an object that is instanced only once?

 

Answer:

First, we need to know how to create an object (new Singleton ()). Second, we need a private builder like this private Singleton () {}. Last, we need to a static method like this Singleton.getInstance ().


We need to remember one thing is that we have to request an instance that has been already instantiated if we need it.


A typical practice of Singleton.



1, We need an instance variable that can record Class of Singletons instance decorated by static.

2, We need a builder that can create an object decorated by private. So only Class of Singleton can use this method of builder.

3, We need a normal method that can really crate the instance decorated by public. So Just like global variable you can use this method if you need everywhere. Please attention, if we do not need this instance, it will not be created forever because we use lazy instantiate. It is important for an very big instance to use lazy instantiate because when we need this object , we create this instance and it will make full use of memory.


We have face some problems.


1, How to process multithreading?


We need to add a key word synchronized in method of getInstance. This can solve this problem but it will really bring very bad performance.


So we need to eagerly create the instance.



When JVM load this Class, it will create the instance right now. So it is the only one instance of this Class.


We also can use double-checked locking.


This is what we want because it synchronize only the first time. But if we do not care about the performance, we do not need to use the double-checked locking.


We can use static code because when JVM load this Class it will execute the static code and it will assure that this instance is only the one.




We also can use JVM load Class is a single thread so it can assure that it is a unique instance.




Note:

1,How to solve multithreading question?

2,When do we use the eagerly or lazy instantiate?

3,Understand the forms of how to write a singleton and why do choose it.


























猜你喜欢

转载自blog.csdn.net/outsanding/article/details/80563994
my