Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 653385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:22:35+00:00 2026-05-13T22:22:35+00:00

I have trouble finding way to correctly refactor this code so that there would

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:22:35+00:00Added an answer on May 13, 2026 at 10:22 pm

    The applicable design pattern is Template Method, rather than Strategy.

    About the different type of items, I would try genericizing parseSomething first, like this:

    public <T> List<T> parseSomething(Node n){
      List<T> somethings = new ArrayList<T>();
      initialize();
      sameCodeInBothClasses();
      List<Node> nodes = getChildrenByName(n, "somename");
      for(Node n:nodes){
        method();
        actionA();
        somethings.add(new T(actionB());
      }
      return somethings;
    }
    

    This may not work for you straight away though. You may need to move the generic parameter to the class level.

    Returning List<Object> will not work, as generic collections are invariant: for
    any two distinct types Type1 and Type2, List<Type1> is neither a subtype nor a
    supertype of List<Type2> (even if Type1 and Type2 are related, i.e. one is a subtype of the other!). See an earlier answer of mine for more details on this.

    So if all else fails, the quick and dirty solution would be indeed to use a non generic List.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started using django-piston for creating APIS but I am having trouble finding
I am creating a PHP application and I'm having a bit of trouble finding
I'm writing a FUSE filesystem that does some mapping through sqlite, then passes the
I would like to get started developing native (C/C++) OpenGL ES 2.0 applications for
I want to modify the source code of Nginx (http://nginx.org/download/nginx-0.7.67.tar.gz) so when it serves
My website looks perfect on the iPhone but looks sub-optimal on the iPad. I'm
I've been coding a website for the past 6 months using HTML/CSS/PHP/MySQL/Javascript. I'm coding
I'm having a really rough time wrapping my head around prototypes in JavaScript. Previously
I'm working on a graphical model project with python using NetworkX . NetworkX provides
The Zend Framework is mainly meant for MVC use. One of the very usefull

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.