静态变量的泛型类

时间:2015-08-31 19:31:51

标签: java enums

我正在尝试为下面的代码编写更多泛型类,以便最终开发人员可以更改EvenTypes中定义的类型和静态变量,并为它们提供在EventType中添加或删除静态变量的选项。

我也想到了如下事件类型的通用枚举。

public interface EventType<T extends Enum<T>>
{
    String name();

    String getFriendlyName();

    Class<T> getDeclaringClass();

    T getRaw();

    public String getFullClassName();
}

原始代码

    public class Event implements Serializable
    {

        private String eventId;
        private String eventType;
            .....

}

public class EventTypes
{// below static variables can be changed by the developer based on requirements
    public static final String DO_FILE_CONVERSION = "100";
    public static final String DO_REMOVE_FILE = "101";
    public static final String DO_COPY_FILE = "102";
    .....

    }


public class EventProcessorFactory
    {

        @SuppressWarnings("rawtypes")
        public IEventProcessor createProcessor(EventType eventType)
                throws EventException
        {
            String eventProcessorClassName = (getEvenClassName  based on type from properties files);
            Class eventProcessorClazz = Class.forName(eventProcessorClassName);
return (IEventProcessor) eventProcessorClazz.newInstance();
            }
    }

properties.file
----
100=FileConversion.class
101=FileRemove.class
102= FileCopy.class

1 个答案:

答案 0 :(得分:0)

将Event设为接口并从Serializable扩展它。

然后使用枚举来实现该Event接口。例如:

FormA form_a = new FormA { Size = new Size(530, 421) };                

FormB install_screens_active = Application.OpenForms["FormB"];
FormA.Show();

if (install_screens_active != null)
{ 
    install_screens_active.Close();
}

通过这种方式,您可以从实现中分离出您的接口(即您希望事件能够执行的任何操作)(事件存在并实现它们)。