Which two are true about Singletons?

Which two are true about Singletons?

Which two are true about Singletons?

A.
A Singleton improves a class’s cohesion.

B.
Singletons can be designed to be thread-safe.

C.
A Singleton implements a factory method.

D.
A Singleton has only the default constructor.

E.
A Singleton must implement serializable.



Leave a Reply 2

Your email address will not be published. Required fields are marked *


ZBoston

ZBoston

A Singleton object is one for which there is meant to be only one instance available. As such, thread safety controls not only could be but should be implemented to ensure multiple threads do not incidentally create several instances.

Likewise, if several threads or objects access the same object, and there is only supposed to be one instance, a factory method is the simplest way to give objects access to that instance.

leo yu

leo yu

why singleton method must apply singleton factory method? the most usual implementation of singleton is as below which is not on factory method.

private volatile static Singleton mInstance;

private Singleton(){}

public static Singleton getInstance()
{
if(mInstance==null) {
synchronized (Singleton.class) {
// only pay synchronization penalty once
if(mInstance==null){
mInstance=new Singleton();
}
}
}
return mInstance;
}