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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:52:11+00:00 2026-05-15T17:52:11+00:00

So I did some refactoring and two of my classes now look exactly the

  • 0

So I did some refactoring and two of my classes now look exactly the same except for their constructors.

The classes wrap an API object that isn’t very pretty, and add some functionality that belong at the edge of the API.

class A extends API {
  public A {
    this.APIOption = Option1;
    this.AnotherAPIOption = Option2;
    // a few more
  }

  public ArrayList<String> somethingTheAPIDoesntDo() {
    // Do something the API doesn't provide but I use alot
  }
  // Other shared methods
}

class B extends API {
  public B {
    this.APIOption = Option3;
    this.AnotherAPIOption = Option4;
    // a few more
  }

  public ArrayList<String> somethingTheAPIDoesntDo() {
    // Do something the API doesn't provide but I use alot
  }
  // Other shared methods
}

Does it make sense to push the common code between the two to an Abstract Base class, and have the subclasses only implement their constructors with the specialized option settings?
It makes sense on paper, but something feels weird/counterintuitive about it. Am I missing a pattern here?

Possible DRYer Solution

class A extends BetterAPIBase {
  public A {
    this.APIOption = Option1;
    this.AnotherAPIOption = Option2;
    // a few more
  }
}

class B extends BetterAPIBase {
  public B {
    this.APIOption = Option3;
    this.AnotherAPIOption = Option4;
    // a few more
  }
}    

abstract class BetterAPIBase extends API {
  public Better APIBase() {}
  public ArrayList<String> somethingTheAPIDoesntDo() {
    // Do something the API doesn't provide but I use alot
  }
  // Other methods
}

EDIT

Static Factory Pattern is nice, but I think I may also add an Interface that includes the common methods I added.

I would make the BetterAPI class also implement IBetterAPI, which would only expose the methods I added wherever I declare the instance’s type as IBetterAPI.

interface IBetterAPI{
      public ArrayList<String> somethingTheAPIDoesntDo();
      // other methods I added in BetterAPI
}

//somewhere else:
IBetterAPI niceApi = BetterAPI.createWithOptionSetA();
niceApi.somethingTheAPIDoesntDo();

// Can't do this, nice and hidden.
niceApi.somethingBaseAPIDoes(string uglyOptions, bool adNauseum); 
  • 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-15T17:52:12+00:00Added an answer on May 15, 2026 at 5:52 pm

    Wouldn’t it be simpler to have one class with a parametrized (possibly private) constructor and multiple static factory methods?

    class BetterAPI extends API {
      private BetterAPI(APIOption option, AnotherAPIOption anotherOption) {
        this.APIOption = option;
        this.AnotherAPIOption = anotherOption;
      }
    
      public static BetterAPI createWithOptionSetA() {
        return new BetterAPI(Option1, Option2);
      }
    
      public static BetterAPI createWithOptionSetB() {
        return new BetterAPI(Option3, Option4);
      }
    
      // ...
    }
    

    The core of your problem seems to be that you can’t have multiple parameterless constructors in the same class. Static factory methods offer a nice solution to this.

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

Sidebar

Related Questions

I recently did some refactoring of my mvc application and realized that there are
I'm refactoring some legacy code, and I keep running into this Object. I would
Oops this happened again. I did some refactoring today when i moved code from
I am refactoring some code that I did not write, and I found a
I did some searching on here and couldn't find the answer I exactly needed.
On our web-application, we did some refactoring on the Java beans, and due to
Did some searches here & on the 'net and haven't found a good answer
Did some googling and couldn't find a clear answer on this. My assumption is
I did some research and found that the only way to vertically center a
I did some all day learning and I figured out how to add rows

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.