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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:36:06+00:00 2026-06-14T23:36:06+00:00

I have 2 separate classes, but they both need to do one thing repetitively,

  • 0

I have 2 separate classes, but they both need to do one thing repetitively, in other words there is a common method. This is the method:

private String createButton (String cls, String value) {
        return "<input type=\"button\" class=\"" + cls + 
                   "\" value=\"" + value + "\" key=\"" + this.id + "\" />";
        }

so this is really just a one line method, so I could copy it into both classes. But I was wondering if there’s a better way of doing that. I really don’t want to have a super class with just that method, obviously. Also, I think it’s silly creating another class just for the button and do: new Button(cls,value), isn’t?

Another option I thought about is to have a utility class for the package, with a mix of helper functions. Does that make sense? Is it being done?

  • 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-14T23:36:07+00:00Added an answer on June 14, 2026 at 11:36 pm

    You could use a utility class… but it might become incohesive.

    public class Utilities {
    
        /* What do these methods have in common? */
    
        public String createButton(...) {
            return "<input type='button' />";
        }
    
        public double calculateCircumference(Circle c) {
            return circle.getRadius() * 2 * Math.PI;
        }
    
    }
    

    This is the kind of code that leads down the dark road to a God Object.

    Instead, consider scoping the purpose and intent of your “utility” class: consider making it a factory – specifically an Abstract Factory so that it retains cohesiveness.

    public class HTMLWidgetFactory implements AbstractWidgetFactory<String> { // the interface might be overkill
    
        /* Oh! This class is clearly used to create HTML controls! */
    
        public String createButton(...) {
            return "<input type='button' />";
        }
    
        public String createImage(...) {
            return "<img src='lena.png' />";
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MainActivity and an AsyncTask MyTask. They are separate classes. In MyTask,
I see most projects creating separate factory classes, so for example, they'll have a
Say I have two separate classes, A and B. I also have Repository class
I have an interface that I want to implement in separate classes after doing
Alright so I have been trying to get all of my separate classes (At
Do I need to have separate/multiple workers to run multiple websites (each with a
What's the best way to have separate CSS for IE and other browsers? I'm
I am a Java programmer and I have always created separate files for Classes,
I have a hard time believing that there isn't an answer here but it
I have a set of xsd files and I need to generate java classes

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.