1) Create interface
interface SectionRemovalRule {
void runRule(long userId) ;
}
2) Create hashmap
private static Map<String, SectionRemovalRule> sectionRemovalMethodMap = new HashMap<String, SectionRemovalRule>();
3) Initialize map
@PostConstruct
void initSectionRemovalMethodMap() {
sectionRemovalMethodMap.put("section1",
new SectionRemovalRule() {
@Override
public void runRule(long sectionId) {
section1Dao
.removeSection(sectionId);
}
});
sectionRemovalMethodMap.put("section2",
new SectionRemovalRule() {
@Override
public void runRule(long sectionId) {
section2Dao
.removeSection(sectionId);
}
});
}
4) Calling the method
sectionRemovalMethodMap.get('yourSectionName').runRule(sectionId);
5) Benefits :
* It helps to avoid if else
* it makes code simple.
* increases performance
interface SectionRemovalRule {
void runRule(long userId) ;
}
2) Create hashmap
private static Map<String, SectionRemovalRule> sectionRemovalMethodMap = new HashMap<String, SectionRemovalRule>();
3) Initialize map
@PostConstruct
void initSectionRemovalMethodMap() {
sectionRemovalMethodMap.put("section1",
new SectionRemovalRule() {
@Override
public void runRule(long sectionId) {
section1Dao
.removeSection(sectionId);
}
});
sectionRemovalMethodMap.put("section2",
new SectionRemovalRule() {
@Override
public void runRule(long sectionId) {
section2Dao
.removeSection(sectionId);
}
});
}
4) Calling the method
sectionRemovalMethodMap.get('yourSectionName').runRule(sectionId);
5) Benefits :
* It helps to avoid if else
* it makes code simple.
* increases performance