How to correctly refactor this?
- by kane77
I have trouble finding way to correctly refactor this code so that there would be as little duplicate code as possible, I have a couple of methods like this (pseudocode):
public List<Something> parseSomething(Node n){
List<Something> somethings = new ArrayList<Something>();
initialize();
sameCodeInBothClasses();
List<Node> nodes = getChildrenByName(n, "somename");
for(Node n:nodes){
method();
actionA();
somethings.add(new Something(actionB());
}
return somethings;
}
methods sameCodeInBothClasses() are same in all classes but where it differs is what hapens in for loop actionA() and it also adds an element to the List of different type.
Should I use Strategy pattern for the different parts inside loop?
What about the return value (The type of list differs), should the method return just List<Object> that I would then cast to appropriate type? Should I pass the class I want to return as parameter?