为了提高我的编码知识,我想建立一个包含尽可能多的模式,容器和类似内容的库。
我不想实现模式本身,而是想要描述它们。例如,我不希望该库包含观察者,但是当主项目需要观察者时,我可以仅添加extends observer
或implements observer
,并且IDE应该自动完成它。可能。
要开始简单(我至少如此认为),我想从Singleton的(反)模式开始。
我创建了一个接口iSingleton { iSingleton getInstance(); }
和一个抽象类
public abstract class Singleton implements iSingleton
{
static Singleton instance;
protected Singleton() {}
}
当然,我创建了一个实现测试类
public class Test extends Singleton
{
private Test();
public Test getInstance()
{
if (instance == null)
{
instance = new Test();
}
return (Test) instance;
}
}
但这给了一些问题
private Test
我想要的是一个类/接口,该类可以告诉我并允许构建类似的东西
public Implementation extends/implements Singleton
{
static Implementation instance;
private Implementation()
{
// It's a singleton, use getInstance()
}
public static Implementation getInstance()
{
if (instance == null)
{
instance = new Implementation()
}
return instance
}
}
我想要的细节
如何创建类似的东西,甚至有可能?