你在工厂方法里面 实例多个单列,每个单列的实现都是不一样的。
比如:public interface Single {
public void doAction();
}
public class SingleA implements Single { public void doAction() { }
}
public class SingleB implements Single { public void doAction() { }
}public class SingleFactory {
public static Single getInstance(int type){
if (type == 1) {
return new SingleA ();
}
else if (type == 2) {
return new SingleB (); } }
}
这样不知道是不是你想要得。
比如:public interface Single {
public void doAction();
}
public class SingleA implements Single { public void doAction() { }
}
public class SingleB implements Single { public void doAction() { }
}public class SingleFactory {
public static Single getInstance(int type){
if (type == 1) {
return new SingleA ();
}
else if (type == 2) {
return new SingleB (); } }
}
这样不知道是不是你想要得。
public class SingleFactory {
static Single a;
static Single b;
public static Single getInstance(int type){
if (type == 1) {
if (a == null) {
a = new SingleA ();
}
return a ;
}
else if (type == 2) {
if (b == null) {
b = new SingleB ();
}
return b;
}
}
}