I need help in refactoring the following piece of code . There are 2 main problems that I want to fix in this code 1. Unsafe type casting in Rule1 implementation 2. Implementation of too many interfaces
public interface Rule {
public void execute(DataProvider dataProvider);
}
public interface DataProvider {
// No common functions as of now
}
public interface Rule1dataProvider extends DataProvider {
// Functions required by Rule1
}
public class Rule1 implements Rule {
public void execute(DataProvider dataProvider) {
ADataProvider rule1DataProvider = <ADataProvider>dataProvider;
// Other logic
}
}
public class EntityADataProvider implements Rule1DataProvider, Rule2DataProvider... {
// Overrides functions of Rule 1 and Rule2
}
I was trying to use generics here, but could not figure out the right way to use them in order to eliminate the type casting
Make Rule generic:
public interface Rule<T extends DataProvider> {
public void execute(T dataProvider);
}
And then
public class Rule1 implements Rule<Rule1dataProvider> {
@Override
public void execute(Rule1dataProvider dataProvider) {
// ...
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.