How to correctly refactor this?

Posted by kane77 on Stack Overflow See other posts from Stack Overflow or by kane77
Published on 2010-03-07T20:40:18Z Indexed on 2010/03/08 0:12 UTC
Read the original article Hit count: 648

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about oop

Related posts about java