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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:14:21+00:00 2026-06-12T04:14:21+00:00

I’m working on a legacy Java application, that deals with fruits and vegetables, let’s

  • 0

I’m working on a legacy Java application, that deals with “fruits” and “vegetables”, let’s say, for the sake of the question.
They are treated as different things internally, cause they don’t have all methods/properties in common, but a lot of things are DONE very similar to both of them.

So, we have a ton of methods doSomethingWithAFruit(Fruit f) and doSomethingWithAVegetable(Veg v), that use the proper doOtherStuffWithAFruit(Fruit f) / doOtherStuffWithAVeg(Veg v). And those are very similar, except that methods that do things with fruits only call the methods that do things with fruits, and the same thing for vegetables.

I want to refactor this to reduce the duplication, but I’m not sure what is the best way to accomplish that. I’ve read a bit about some design patterns, but I don’t know if it has made any clearer to me. (I can recognize some patterns in the code I use, but I don’t really know when I should be applying a pattern to improve things around. Maybe I should be reading more about refactoring itself…)

I was thinking of these two options:

1. Creating a class that can have an instance of either a Fruit or a Vegetable and pass it around to the methods, trying to minimize the duplication. It would go like this:

public void doSomething(Plant p) {
   // do the stuff that is  common, and then...
   if (p.hasFruit()) {
       doThingWithFruit(p.getFruit());
   } else {
       doThingWithVegetable(p.getVegetable());
   }
}

This would get things a bit better, but I don’t know… it still feels wrong.

2. The other alternative I thought was to put an interface in Fruit and Vegetable with the stuff that is common to them, and use that to pass it around. I feel this is the cleaner approach, although I will have to use instanceof and cast to Fruit/Vegetable when it needs stuff that is specific to them.

So, what more can I do here? And what are the shortcomings of these approaches?

UPDATE: Note that the question is a bit simplified, I’m looking for way to do things WITH the “plants”, that is, code that mostly “uses” them instead of doing things TO them. Having said that, those similar methods I refer to cannot be inside the “Plants” classes, and they usually have another argument, like:

public void createSomethingUsingFruit(Something s, Fruit f);
public void createSomethingUsingVegetable(Something s, Vegetable v);

Namely, those methods have other concerns besides Fruits/Vegetables, and aren’t really appropriated to be in any Fruit/Vegetable class.

UPDATE 2: Most code in those methods only reads state from the Fruit/Vegetable objects, and create instances of other classes according to the appropriate type, store in the database and so on — from my answer to a question in the comments that I think it’s important.

  • 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-12T04:14:22+00:00Added an answer on June 12, 2026 at 4:14 am

    Another option you can use, or perhaps include it as part of your solution, is to ask the consumer if it can manage the object that you are passing it. At this point, it becomes the consumer’s responsibility to ensure it knows how to handle the object you are sending it.

    For instance, if your consumer is called Eat, you would do something like:

    Consumer e = new Eat();
    Consumer w = new Water();
    if( e.canProcess( myFruit ) )
       e.doSomethingWith( myFruit );
    else if ( w.canProcess( myFruit ) )
       w.doSomethingWith( myFruit );
    
    .... etc
    

    But then you end up with a lot of it/else classes, so you create yourself a Factory which determines which consumer you want. Your Factory basically does the if/else branching to determine which consumer can handle the object you pass, and returns the consumer to you.

    So it looks something like

    public class Factory {
       public static Consumer getConsumer( Object o ){
        Consumer e = new Eat();
        Consumer w = new Water();
        if( e.canProcess( o ) )
           return e;
        else if ( w.canProcess( o ) )
           return w;
       }
    }
    

    Then your code becomes:

    Consumer c = Factory.getConsumer( myFruit );
    c.doSomethingWith( myFruit );
    

    Of course in the canProcess method of the consumer, it would be basically an instanceof or some other function your derive to determine if it can handle your class.

    public class Eat implements Consumer{
       public boolean canProcess(Object o ){
         return o instanceof Fruit;
       }
    }
    

    So you end up shifting the responsibility from your class to a Factory class to determine which objects can be handled. The trick, of course, is that all Consumers must implement a common interface.

    I realize that my pseudo-code is very basic, but it is just to point out the general idea. This may or may not work in your case and/or become overkill depending on how your classes are structured, but if well designed, can significantly improve readability of your code, and truely keep all logic for each type self-contained in their own class without instanceof and if/then scattered everywhere.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this

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.