q**q 发帖数: 266 | 1 careercup上有这样一道题:implement a singleton design pattern as a template
such that for any given class Foo, you can call Singleton::instance() and
get a pointer to an instance of a singleton of type Foo
答案是用c++给出的。我用java写出下面的代码,但显然不对,请问上面的问题应该怎
么用java实现呢?
另外对下面不对的代码我也有一些疑问:
public class SingleTon {
private static T t; //报语法错误“annot make a static reference to the
non-static type T”,请问这是什么缘故?
public synchronized static T instance() {
if( t == null ) {
t = new T();//即使t非static,此处也无法instantiate,我想是因为很多
class没有default constructor的原因,这样理解是否对?
}
return t;
}
}
谢谢! |
g*****g 发帖数: 34805 | 2 Since Java uses type erasure, generics is a compile time trick.
If it can't decide the type in compile time, it can't do instantiation.
template
the
【在 q**q 的大作中提到】 : careercup上有这样一道题:implement a singleton design pattern as a template : such that for any given class Foo, you can call Singleton::instance() and : get a pointer to an instance of a singleton of type Foo : 答案是用c++给出的。我用java写出下面的代码,但显然不对,请问上面的问题应该怎 : 么用java实现呢? : 另外对下面不对的代码我也有一些疑问: : public class SingleTon { : private static T t; //报语法错误“annot make a static reference to the : non-static type T”,请问这是什么缘故? : public synchronized static T instance() {
|
q**q 发帖数: 266 | 3 明白。那么对于上面描述的问题,能用java实现吗?
【在 g*****g 的大作中提到】 : Since Java uses type erasure, generics is a compile time trick. : If it can't decide the type in compile time, it can't do instantiation. : : template : the
|
m****r 发帖数: 6639 | 4 since you need to return any class, what you need is a map from Class to
Object.
private static Map, or Map, i am not sure.
and use reflection to instantiate the object.
template
the
【在 q**q 的大作中提到】 : careercup上有这样一道题:implement a singleton design pattern as a template : such that for any given class Foo, you can call Singleton::instance() and : get a pointer to an instance of a singleton of type Foo : 答案是用c++给出的。我用java写出下面的代码,但显然不对,请问上面的问题应该怎 : 么用java实现呢? : 另外对下面不对的代码我也有一些疑问: : public class SingleTon { : private static T t; //报语法错误“annot make a static reference to the : non-static type T”,请问这是什么缘故? : public synchronized static T instance() {
|
r*****l 发帖数: 2859 | 5 public class Singleton {
private static final Map, Object> map = new HashMap,
Object>();
public synchronized static T instance(Class c)
throws InstantiationException, IllegalAccessException {
if (!map.containsKey(c)) {
T o = c.newInstance();
map.put(c, o);
}
return (T) map.get(c);
}
}
template
the
【在 q**q 的大作中提到】 : careercup上有这样一道题:implement a singleton design pattern as a template : such that for any given class Foo, you can call Singleton::instance() and : get a pointer to an instance of a singleton of type Foo : 答案是用c++给出的。我用java写出下面的代码,但显然不对,请问上面的问题应该怎 : 么用java实现呢? : 另外对下面不对的代码我也有一些疑问: : public class SingleTon { : private static T t; //报语法错误“annot make a static reference to the : non-static type T”,请问这是什么缘故? : public synchronized static T instance() {
|
q**q 发帖数: 266 | 6 Thank you! I got it.
【在 r*****l 的大作中提到】 : public class Singleton { : private static final Map, Object> map = new HashMap, : Object>(); : public synchronized static T instance(Class c) : throws InstantiationException, IllegalAccessException { : if (!map.containsKey(c)) { : T o = c.newInstance(); : map.put(c, o); : } : return (T) map.get(c);
|
q**q 发帖数: 266 | 7 Thank you!
【在 m****r 的大作中提到】 : since you need to return any class, what you need is a map from Class to : Object. : private static Map, or Map, i am not sure. : and use reflection to instantiate the object. : : template : the
|
r*****l 发帖数: 2859 | 8 懒惰,只给说明,不写code。
【在 m****r 的大作中提到】 : since you need to return any class, what you need is a map from Class to : Object. : private static Map, or Map, i am not sure. : and use reflection to instantiate the object. : : template : the
|
m****r 发帖数: 6639 | 9 我刚起床. 还不在状态.
【在 r*****l 的大作中提到】 : 懒惰,只给说明,不写code。
|
S****h 发帖数: 115 | 10 赞~
【在 r*****l 的大作中提到】 : public class Singleton { : private static final Map, Object> map = new HashMap, : Object>(); : public synchronized static T instance(Class c) : throws InstantiationException, IllegalAccessException { : if (!map.containsKey(c)) { : T o = c.newInstance(); : map.put(c, o); : } : return (T) map.get(c);
|