抽象类中的Java懒加载单例

时间:2018-09-14 09:19:30

标签: java singleton lazy-loading

我正在尝试使用Java中的Abstract类实现Singleton实例。我读过像将其实现为LazyLoad是最佳实践。我无法做到这一点,因为我对这种模式甚至Java都不熟悉。

  1. getInstance()始终为null,我不确定为什么吗?
  2. 我不认为这是在构造实例后进行的延迟加载。
  3. 除此以外,我们欢迎任何其他评论,因为我是Java的新手,并且以前使用c#。

这就是我所拥有的

接口:

public interface IConditionAppender{
    public String Append();
}

摘要

public abstract AppenderBase {

    private static IConditionAppender instance;

    protected AppenderBase(IConditionAppender instance)
    {
         this.instance = instance;
    }

    public static IConditionAppender getInstance(){ return instance; }
}

实施

public final class AndAppender extends AppenderBase implements IConditionAppender {

private AndAppender()
{
    super(new AndAppender())
}

@Override
public String Append()
{
    return " AND ";
}

}

测试

System.out.println(AndAppender.getInstance().Append());

1 个答案:

答案 0 :(得分:1)

下面的代码可能会帮助您〜

public abstract class AbstractSingleton {

    private static Map<String, AbstractSingleton> registryMap = new HashMap<String, AbstractSingleton>();

    AbstractSingleton() throws SingletonException {
        String clazzName = this.getClass().getName();
        if (registryMap.containsKey(clazzName)) {
            throw new SingletonException("Cannot construct instance for class " + clazzName + ", since an instance already exists!");
        } else {
            synchronized (registryMap) {
                if (registryMap.containsKey(clazzName)) {
                    throw new SingletonException("Cannot construct instance for class " + clazzName + ", since an instance already exists!");
                } else {
                    registryMap.put(clazzName, this);
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    public static <T extends AbstractSingleton> T getInstance(final Class<T> clazz) throws InstantiationException, IllegalAccessException {
        String clazzName = clazz.getName();
        if (!registryMap.containsKey(clazzName)) {
            synchronized (registryMap) {
                if (!registryMap.containsKey(clazzName)) {
                    T instance = clazz.newInstance();
                    return instance;
                }
            }
        }
        return (T) registryMap.get(clazzName);
    }

    public static AbstractSingleton getInstance(final String clazzName)
            throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        if (!registryMap.containsKey(clazzName)) {
            Class<? extends AbstractSingleton> clazz = Class.forName(clazzName).asSubclass(AbstractSingleton.class);
            synchronized (registryMap) {
                if (!registryMap.containsKey(clazzName)) {
                    AbstractSingleton instance = clazz.newInstance();
                    return instance;
                }
            }
        }
        return registryMap.get(clazzName);
    }

    @SuppressWarnings("unchecked")
    public static <T extends AbstractSingleton> T getInstance(final Class<T> clazz, Class<?>[] parameterTypes, Object[] initargs)
            throws SecurityException, NoSuchMethodException, IllegalArgumentException,
            InvocationTargetException, InstantiationException, IllegalAccessException {
        String clazzName = clazz.getName();
        if (!registryMap.containsKey(clazzName)) {
            synchronized (registryMap) {
                if (!registryMap.containsKey(clazzName)) {
                    Constructor<T> constructor = clazz.getConstructor(parameterTypes);
                    T instance = constructor.newInstance(initargs);
                    return instance;
                }
            }
        }
        return (T) registryMap.get(clazzName);
    }

    static class SingletonException extends Exception {
        private static final long serialVersionUID = -8633183690442262445L;

        private SingletonException(String message) {
            super(message);
        }
    }
}

发件人:https://www.cnblogs.com/wang9192/p/3975748.html