No Static Method in Interface, So I Write Code As ...
Java does not support static method in interface. But sometimes, I just want a static method to say: PersistenceManager.getDefault(), where PersistenceManager is going to be an interface. I don't like to add one more class named PersistenceManagerFactory, with a method:
public static PersistenceManager PersistenceManagerFactory.getDefault()
So I write code like:
public class PersistenceManager { private static I i; public static I getDefault() { return i == null ? i = ServiceLoader.load(I.class).iterator().next() : i; } /** The interface I, which is actually the PersistenceManager should be: */ public static interface I { void saveQuotes(String symbol, Frequency freq, Listquotes); ListrestoreQuotes(String symbol, Frequency freq); void deleteQuotes(String symbol, Frequency freq, long fromTime); void dropAllQuoteTables(String symbol); void shutdown(); QuotePool getQuotePool(); TickerPool getTickerPool(); } }
Then implement the PersistenceManager.I in another package, like:
public class NetBeansPersistenceManager implements PersistenceManager.I { ... }
And declare it under the META-INF as:
core/src/META-INF/services/org.aiotrade.math.PersistenceManager$I
which contains one line:
org.aiotrade.platform.core.netbeans.NetBeansPersistenceManager
I can call PersistenceManager.getDefault().showdown() now.