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

  • SEARCH
  • Home
  • 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 8510189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:38:10+00:00 2026-06-11T03:38:10+00:00

I am currently designing a class which heavily makes use of reflection by manipulating

  • 0

I am currently designing a class which heavily makes use of reflection by manipulating the declared fields. Hence, a lot of methods have something in common in terms of their body, which is (hopefully) illustrated by this java code:

import java.lang.reflect.Field;

public class foo {
    public void foo1(/* arguments1 */) {
        for (Field f : getClass().getDeclaredFields()) {
            // do some stuff using arguments1
        }
    }

    public void foo2(/* arguments2 */) {
        for (Field f : getClass().getDeclaredFields()) {
            // do some stuff using arguments2
        }
    }

    public void foo3(/* arguments3 */) {
        for (Field f : getClass().getDeclaredFields()) {
            // do some stuff using arguments3
        }
    }

    //and so on...
}

Depending on how many methods this class will finally contain, could this be considered a design flaw? If I want to use getFields() instead of getDeclaredFields() for example, I would need to replace every occurrence of getDeclaredFields(). This does not sound like good programming practice to me. In my case this might not be a very realistic scenario, but for the sake of interest I would like to know if there is a design pattern or a concept which tackles this problem.

[EDIT]

To avoid additional misunderstandings: The operations inside the loop depend on the arguments given by foo1, foo2 etc.. and those arguments are not always the same for each method. I illustrated this fact poorly, sry. I improved the given code to demonstrate it better.

  • 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-06-11T03:38:11+00:00Added an answer on June 11, 2026 at 3:38 am

    You might want to define an interface for the body of the loop:

    interface FieldOperation {
        void doSomeStuff(Field f);
    }
    

    Then you can write a single looping method in place of foo1, foo2, and foo3:

    public void foo(/* arguments */, FieldOperation op) {
        for (Field f : getClass().getDeclaredFields()) {
            op.doSomeStuff(f);
        }
    }
    

    You can then instantiate several FieldOperation objects:

    FieldOperation foo1Operation = new FieldOperation() {
        void doSomeStuff(Field f) {
            // do some stuff that used to be in foo1()
        }
    }
    // etc.
    

    This scales nicely and separates the logic of which fields to access from the operation that you want to do on each field.

    EDIT If each foo* requires a different set of arguments, I’d suggest packaging them up as classes:

    class Foo1Args { . . . }
    class Foo2Args { . . . }
    class Foo3Args { . . . }
    

    Then you can make your interface generic:

    interface FieldOperation<T> {
        void doSomeStuff(Field f, T args);
    }
    

    and define foo to be a generic method:

    public <T> void foo(T args, FieldOperation<T> op) {
        for (Field f : getClass().getDeclaredFields()) {
            op.doSomeStuff(f, args);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently designing a website which uses this: <li class=current> . I have
I'm currently designing a custom tab control class which derives from System.Windows.Forms.Control. The problem
I am currently in the process of designing a system which will use multiple
I have a class LocationHelper which I am designing to obtain a user location.
I am currently designing a class library that will provide data to a web
Possible Duplicate: StructureMap singleton usage (A class implementing two interface) I'm currently designing a
I'm currently designing an application which I will ultimately want to move to Windows
I have defined a class in C++ which holds an array of scalars of
I have a type class Atomic , which defines functions for converting certain types
I currently have an object Tag defined as follows: public class Tag { public

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.