Java – handle enumerations (iterations, deserialization) that implement common interfaces

I have an Android application that can load data from a web source and display it I organize each API method, and these source codes are supported in the enumeration constants of each source partition For example, sourcea provides sports and weather data, and sourceb provides stock and news data

public enum ServiceA implements ServiceMethod {
    GET_SPORT(...) {...},GET_WEATHER(...) {...};
    ...
}

public enum ServiceB implements ServiceMethod {
    GET_STOCK(...) {...},GET_NEWS(...) {...};
    ...
}

Each enumeration class also has private members to handle service APIs (different APIs are used for different sources)

And servicemethod

ServiceMethod method;
... // initializing
String cleanAndFancyData = method.queryData(); // e.g.

No, I need to store user history, so I must serialize and deserialize the servicemethod variable and enumerate all constants in all enumerations that implement this interface Serialization seems simple and impossible

Now I just need to manually iterate through the enumeration:

Collection<ServiceMethod> allMethods = new ArrayList<ServiceMethod>();
allMethods.addAll(EnumSet.allOf(ServiceA.class));
allMethods.addAll(EnumSet.allOf(ServiceB.class));

Is there a cleaner, better way? Or am I completely wrong? In addition, is there any way to ensure that all servicemethods have unique names (across all enumerations)?

Solution

You can use strategy pattern to combine ServiceA and serviceb into one service

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>