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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:58:32+00:00 2026-06-10T10:58:32+00:00

I have an interface specifying method that takes a generic type as input which

  • 0

I have an interface specifying method that takes a generic type as input which is used to create a URL.

interface UrlGenerator<T> {

    String prepareUrl( T input );

}

There is one implementation that doesn’t need the parameter. It uses Void for generic type T.

class StaticUrlGenerator implements UrlGenerator<Void> {

    private final String url;

    public StaticUrlGenerator( String url ) {
        this.url = url;
    }

    @Override
    public String prepareUrl( Void nothing ) {
        return url;
    }

}

The StaticUrlGenerator is awkward to use, as it requires null as an argument to the prepareUrl method.

I could lose the input parameter:

interface UrlGenerator<T> {

    String prepareUrl( T input );

}

Now I have to pass the required input to the implementing class in other way (in constructor). This way I lose the stateless nature of the class, I have to recreate it with different constructor arguments every time I want to change the input.

class SchedulePageUrlGenerator implements UrlGenerator {

    public static final String QUERY_STRING_BASE = "?from=";

    private final String showingBaseUrl;
    private final LocalDate date;

    public SchedulePageUrlGenerator( String showingBaseUrl, LocalDate date ) {
        this.showingBaseUrl = showingBaseUrl;
        this.date = date;
    }

    @Override
    public String prepareUrl() {
        DateTimeFormatter fmt = DateTimeFormat.forPattern( "yyyy-MM-dd" );
        String dateStr = fmt.print( date );
        return showingBaseUrl + QUERY_STRING_BASE + dateStr;
    }

}

I think there must be something fundamentally wrong with my design.

  • 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-10T10:58:34+00:00Added an answer on June 10, 2026 at 10:58 am

    I think there must be something fundamentally wrong with my design.

    The only thing wrong is that you are trying to conflate a one argument method and a zero argument method. You just can’t do it in Java … without opening the door to other problems.

    Basically you’ve got three choices:

    • Stick with your current approach and explicitly pass null in the Void case.

    • Add a second (no argument) method to the interface to deal with the Void case, and make it call the one argument method with a null. Your code needs to cope with a null when T is not Void, but it did anyway.

    • Refactor the interfaces so that there are two distinct interfaces, one with a String prepareUrl() and the other with String prepareUrl(T), and implement the former as a special case class.

    Personally, options 2 is slightly better than option 1, but the 3rd option is probably going to lead to other problems; e.g. that particular method with its two variants will be an impediment to polymorphic method calls over the entire space of T types.

    (Varargs is a bad idea, because that opens the door for multiple arguments which is probably meaningless for your problem.)

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

Sidebar

Related Questions

I have a few generic classes that implement a common non-generic interface. I create
I have interface IConfigurationSource { .... } and class FileConfigurationSource : IConfigurationSource { FileConfigurationSource(string
Let's say we have interface window_creator that responsible for creation of windows. For simplicity
I have next interface public interface IMyInterface { string this[string key] { get; set;
I have an interface hierarchy similar to - interface Type { Type copy(); };
I have an interface IUserLocation and a concrete type UserLocation . When I use
I have a method I'd like to mock: public interface IServiceBus { void Subscribe<T>(ISubscribeTo<T>
I have a generic interface for a mathematics library, something like this: [ContractClass(typeof(MathsDoubleContracts))] public
I have an implementation like that: public interface IGenericRepository { //... void Update<T>(T entity,
Lets say I have a method: someMethod(X anObject) Where X is a type of

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.