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

Ask A Question

Stats

  • Questions 497k
  • Answers 497k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Thanks for the reply but I found a very simple… May 16, 2026 at 11:54 am
  • Editorial Team
    Editorial Team added an answer You can return a helper object from function1 by value… May 16, 2026 at 11:54 am
  • Editorial Team
    Editorial Team added an answer I'm a little bit confused. First of all, you're saying… May 16, 2026 at 11:54 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am having trouble finding the correct refactor option for the following scenario: I
I have an algorithm that searches through all of my sites users, finding those
i have one trouble, me need make scrollview with content of pdf(pages), this content
I have trouble to find the kernel API to enable some qdisc policies tc
I have some trouble with regex and php here: <span style=color: blue>word1</span> word by
I have real trouble understanding mod_rewrite, and got some help in creating a RewriteRule
The goal of the assignment that I'm currently working on for my Data Structures
I'm currently looking at various middleware solutions that will allow me to create applications
I feel like the answer to this question is really simple, but I really
I am using BeautifulSoup in Python and am having trouble replacing some tags. I

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.