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 673959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:39:11+00:00 2026-05-14T00:39:11+00:00

These functions may be hard to fit into a specific class, what’s the best

  • 0

These functions may be hard to fit into a specific class,

what’s the best practice to deal with them?

  • 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-14T00:39:12+00:00Added an answer on May 14, 2026 at 12:39 am

    I suppose you mean methods instead of functions? You should really imagine the word static doesn’t exist in Java world, there really isn’t a case where static would actually do you anything good in the long run.

    I’m actually currently facing a similar problem, I have a whole bunch of API methods I want to give out for users to meddle with but I want to hide the actual methods underneath to hide the implementation details.

    The problem of course is that if I just cram everything into one class, it becomes humongous and hard to use even with the best autocompletion tools available. The problem you have most likely really isn’t about where to put those methods but how to present them to the user.

    To solve that I’d suggest you create a hierarchy of objects where you access your helper methods by calling myUtil.someGroup().someMethod(param1, param2); This is actually how some API:s already work, for example popular Web framework Apache Wicket is configured by using Settings object which uses composition to allow itself to have multiple groups of distinct features.

    To really flesh out this from theory to a working example, lets assume you have a bunch of image manipulation methods which either transform the image’s dimensions or change its properties like color, brightness and contrast. Instead of having this:

    public class ImageUtil {
    
        public static BufferedImage adjustHue(float difference,
                                              BufferedImage original) {
            /* ... */
            return adjusted;
        }
    
        public static BufferedImage adjustBrightness(float difference,
                                                     BufferedImage original) {
            /* ... */
            return adjusted;
        }
    
        public static BufferedImage adjustContrast(float difference,
                                                   BufferedImage original) {
            /* ... */
            return adjusted;
        }
    
        public static BufferedImage setHeight(int newHeight,
                                              BufferedImage original) {
            /* ... */
            return adjusted;
        }
    
        public static BufferedImage setWidth(int newWidth,
                                             BufferedImage original) {
            /* ... */
            return adjusted;
        }
    
    }
    

    you should instead have these interfaces to describe each set of operations:

    public interface IImageColorOperations {
    
        BufferedImage adjustHue(float difference, BufferedImage original);
    
        BufferedImage adjustBrightness(float difference, BufferedImage original;)
    
        BufferedImage adjustContrast(float difference, BufferedImage original);
    
    }
    
    public interface IImageDimensionOperations {
    
        BufferedImage setHeight(int newHeight, BufferedImage original);
    
        BufferedImage setWidth(int newWidth, BufferedImage original);
    
    }
    

    and an accompanying separate class for each interface which you instantiate in your "main" image utility class like so:

    public class ImageUtils {
    
        private final IImageDimensionOperations dimension;
        private final IImageColorOperations color;
    
        public ImageUtils() {
            this(new ImageDimensionOperations(),
                 new ImageColorOperations());
        }
    
        /**
         * Parameterized constructor which supports dependency injection, very handy
         * way to ensure that class always has the required accompanying classes and
         * this is easy to mock too for unit tests.
         */
        public ImageUtils(IImageDimensionOperations dimension,
                          IImageColorOperations color) {
            this.dimension = dimension;
            this.color = color;
        }
    
        /* utility methods go here */
    
    }
    

    But wait, this isn’t all! There’s now two paths to go, you can decide for yourself which one you’d like to take.

    First, you can use composition to expose the interface methods directly:

    public class ImageUtils implements IImageDimensionOperations,
                                       IImageColorOperations {
    
        private final IImageDimensionOperations dimension;
        private final IImageColorOperations color;
    
        public ImageUtils() {
            this(new ImageDimensionOperations(),
                 new ImageColorOperations());
        }
        /* etc. */
    }
    

    With this, you just need to delegate the calls to various methods to the actual operation class. Downside to this is that if you add another method, you have to modify both this utility class and the underlying implementation class.

    Your second choice is to expose the operation classes themselves directly (that’s why I added those finals there!):

    public class ImageUtils {
    
        public final IImageDimensionOperations dimension;
        public final IImageColorOperations color;
    
        public ImageUtils() {
            this(new ImageDimensionOperations(),
                 new ImageColorOperations());
        }    
    
        public ImageUtils(IImageDimensionOperations dimension,
                  IImageColorOperations color) {
            this.dimension = dimension;
            this.color = color;
        }
    
        /* Nothing else needed in this class, ever! */
    
    }
    

    by doing this you get those nice looking calls such as

    BufferedImage adjusted = imageUtils.color.adjustHue(3.2f, original);
    

    and when you add some method to either of the interfaces, you already have them available in your image utility class without any additional modification. Yes, generally public fields are a big no-no in Java, however I do think that in this case that’s not such a bad thing, especially with the finals marking the fields as unmodifiable (at least in theory).

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

Sidebar

Related Questions

I'm trying to Unit Test a class that has many internal functions. These obviously
Can anyone provide a clear explanation / example of what these functions do, and
Lets say I need to write several functions processing some data. These functions are
I have an iphone app where I call these three functions in appDidFinishLaunching: glMatrixMode(GL_PROJECTION);
I always tend to forget these built-in Symfony functions for making links.
1) Where does the homepage of your website fit into controllers? I've seen some
Say I want some functions to deal with some file, and I was considering
It seems to me there are other functional dependencies you may wish to declare
What do *args and **kwargs mean in these function definitions? def foo(x, y, *args):
Consider these two function definitions: void foo() { } void foo(void) { } Is

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.