`
dcaoyuan
  • 浏览: 307395 次
社区版块
存档分类
最新评论

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, List

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.

分享到:
评论

相关推荐

    servlet2.4doc

    attributeAdded(ServletContextAttributeEvent) - Method in interface javax.servlet.ServletContextAttributeListener Notification that a new attribute was added to the servlet context. attributeAdded...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    The goal of this guide is to manage this complexity by describing in detail the dos and don'ts of writing C++ code. These rules exist to keep the code base manageable while still allowing coders to ...

    vxworks_kernel_programmers_guide_6.9

    PART I: CORE TECHNOLOGIES 1 Overview ...................................................................................................... 3 1.1 Introduction ............................................

    acpi控制笔记本风扇转速

    code. AcpiEnable will now fail if all of the required ACPI tables are not loaded (FADT, FACS, DSDT). BZ 477 Added #pragma pack(8/4) to acobject.h to ensure that the structures in this header are ...

    BURNINTEST--硬件检测工具

    - Changed the mechanism to check for the required DirectX Direct3D as the previous method did not work on some system (some W2003 servers). - Enhanced the mechanism to report memory hardware errors ...

    ZendFramework中文文档

    Available options (for this frontend in Zend_Cache factory) 4.3.6.3. Examples 4.4. Zend_Cache后端 4.4.1. Zend_Cache_Backend_File 4.4.2. Zend_Cache_Backend_Sqlite 4.4.3. Zend_Cache_Backend_...

    Interface的测试代码,请光临

    oSavedFile.write(inc, 0, insize); } oSavedFile.close(); in.close(); } catch (HttpException e) { // 发生致命的异常,可能是协议不对或者返回的内容有问题 System.out.println("Please ...

    Java邮件开发Fundamentals of the JavaMail API

    stands for Internet Message Access Protocol, and is currently in version 4, also known as IMAP4. When using IMAP, your mail server must support the protocol. You can't just change your program to ...

    VB编程资源大全(英文源码 控制)

    With Sound ) A Very Cute Screen Saver ,with source code, If you want more avi files then please mail me.<END><br>36 , superwriter.zip A complete Wordprocessor, with source code. You can save, open ...

    基于51单片机控制的点阵LED电子显示屏设计说明.doc

    The use of C51 microcontrollers in this design takes advantage of their built-in features, such as the programmable I/O ports, timers, and interrupts, which are essential for the operation of the LED ...

    Bochs - The cross platform IA-32 (x86) emulator

    the HCs independent from the device specific code. - USB MSD: added support for disk image modes (like ATA disks) - USB printer: output file creation failure now causes a disconnect - re-...

    二级减速器课程设计说明书reducer design specification.doc

    (3) in addition, it cultivates our ability to consult and use manuals, atlas and other relevant technical data, as well as the ability in calculation, drawing data processing and computer, aided ...

    Effective C#

    - **Best Practice:** Initialize static fields in the constructor or inline. **Item 14: Minimize Duplicate Initialization Logic** - **Strategy:** Use patterns like the builder pattern to avoid ...

    JAVAC动态编译

    Files.write(tempFile.toPath(), sourceCode.getBytes()); // 设置编译任务 Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(tempFile); compiler.getTask(null, file...

    虚拟网卡驱动源代码(原版)

    * and redistributed in source or binary form, so long as an * acknowledgment appears in derived source files. The citation * should list that the code comes from the book "Linux Device * Drivers" ...

    asp.net 反射 集成

    foreach (MethodInfo method in methods) { Console.WriteLine("方法: " + method.Name); } Console.WriteLine("-----------------------"); } } } ``` 这段代码首先加载了一个名为"MyMath.dll"的程序集,...

    Visual C++ 编程资源大全(英文源码 控件)

    (33KB)<END><br>36,noform.zip This sample shows how to make an application that doesn't have the doc/view architecture but still offers a form in the client area of the application's main window....

    Visual C++ 编程资源大全(英文源码 网络)

    bridge.zip Bridge Pattern - Bridging the gap between Interface and Implementation (40KB)<END><br>76,method.zip Function Pointers to Non-Static Object Methods(24KB)<END><br>77,singleton.zip ...

    JDK7-8新特性介绍

    // Code that might throw IOException or SQLException } ``` #### 二、JDK 8新特性概述 除了JDK 7中已经介绍的新特性外,JDK 8引入了许多重要的更新和改进,其中最突出的是Lambda表达式和Stream API的支持,...

Global site tag (gtag.js) - Google Analytics