我必须创建一个单例类,除此之外我必须确保不超过 它创建的11个实例的10个实例创建它应该抛出异常的11个实例,我已经想出了下面的设计请指出它是正确的te问题在我看来是它会抛出异常的地方,请建议。
public final class LazySingleton {
private static volatile LazySingleton instance = null;
private int counter = 0; // use to track the count of instance
// private constructor
private LazySingleton() {
}
public static LazySingleton getInstance() {
if (instance == null || counter<=10) {
synchronized (LazySingleton.class) {
instance = new LazySingleton();
counter ++;
}
}
return instance;
}
}
答案 0 :(得分:1)
我认为您正在寻找multiton pattern。在Java中,这可能类似于
public final class LazySingleton {
private static final int MAX = 10;
private static LazySingleton[] instances = new LazySingleton[MAX];
static {
for (int i = 0; i < MAX; i++) {
instances[i] = new LazySingleton(i);
}
}
private int count;
private static int counter = 0; // use to track the count of instance
// private constructor
private LazySingleton(int count) {
this.count = count;
}
public static synchronized LazySingleton getInstance() {
if (counter < MAX) {
return instances[counter++];
}
throw new RuntimeException("Out of new instances.");
}
}
答案 1 :(得分:0)
将计数器设为static
变量。这样它就可以与所有其他实例共享。如果在一个实例中你增加变量,其他人也会看到它。
将增量放在构造函数中,最后用if语句检查,如果counter
大于10,如果是,则抛出异常
答案 2 :(得分:0)
将counter
变为static
变量,以便LazySingleton
的所有实例共享相同的值。然后在LazySingleton
的构造函数中,您可以检查
if (counter > 10) {
//throw desired exception
}
同时确保在构造函数中增加counter
。
答案 3 :(得分:0)
我认为这不再是单身,这是一个工厂类,因为你无法访问其他最后一个对象,你只能参考当前的对象。
购买方式,你必须设置counter static,也将if语句更改为
synchronized (LazySingleton.class) {
if (instance == null || counter < 10) {
instance = new LazySingleton();
counter ++;
}
else{
throw new yourCustomException();
}
}
答案 4 :(得分:0)
这个怎么样::
你要制作单身的班级。
private static SingleTon _inst = null;
private static Map<SingleTon, Integer> map = new HashMap<SingleTon, Integer>();
private SingleTon()
{
}
public static synchronized SingleTon _instance()throws Exception
{
if(map.size()>0)
{
System.out.println("Okay Object is there");
if(!map.containsValue(10))
{
int temp = map.get(_inst)+1;
map.put(_inst, temp);
return _inst;
}else
{
throw new Exception("you reached limit");
}
}else
{
_inst = new SingleTon();
map.put(_inst,1);
return _inst;
}
}
调用线程..尝试使用不同的线程。
for(int i=0;i<11;i++)
{
System.out.println("*** Calling NUmber "+i+" Object ::: "+SingleTon._instance());
}
输出
*** Calling NUmber 0 Object ::: com.comparison.test.SingleTon@1befab0
Okay Object is there
*** Calling NUmber 1 Object ::: com.comparison.test.SingleTon@1befab0
Okay Object is there
*** Calling NUmber 2 Object ::: com.comparison.test.SingleTon@1befab0
Okay Object is there
*** Calling NUmber 3 Object ::: com.comparison.test.SingleTon@1befab0
Okay Object is there
*** Calling NUmber 4 Object ::: com.comparison.test.SingleTon@1befab0
Okay Object is there
*** Calling NUmber 5 Object ::: com.comparison.test.SingleTon@1befab0
Okay Object is there
*** Calling NUmber 6 Object ::: com.comparison.test.SingleTon@1befab0
Okay Object is there
*** Calling NUmber 7 Object ::: com.comparison.test.SingleTon@1befab0
Okay Object is there
*** Calling NUmber 8 Object ::: com.comparison.test.SingleTon@1befab0
Okay Object is there
*** Calling NUmber 9 Object ::: com.comparison.test.SingleTon@1befab0
Okay Object is there
Exception occuredyou reached limit
java.lang.Exception: you reached limit
at com.comparison.test.SingleTon._instance(SingleTon.java:31)
at com.comparison.test.Hell.main(Hell.java:97)