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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:57:26+00:00 2026-05-27T06:57:26+00:00

I have a method like this: public void foo(String str){ do.something(); do.anotherthing(); do.somethingelse(); if(str.compareTo(choiceA)==0){

  • 0

I have a method like this:

   public void foo(String str){

    do.something();
    do.anotherthing();
    do.somethingelse();

    if(str.compareTo("choiceA")==0){
    do.somethingforA();
    }
    else if(str.compareTo("choiceB")==0){
    doanother.somethingforB();
    doelse();
    }
    else if(str.compareTo("choiceC")==0){
    dosomeother.somethingforC();
    doblahblah();
    writeblabla();
    }

}

i want to design the code as defining different classes or methods for A, B and C.

but there is many lines (first three lines of method) that A, B and C sharing.

if i generate different method for A, B and C, i will have to change every method when i have to maintain in the future, thus i wont be able to get the help of designing code with a design pattern approach.

what do you advise?

Thanks for any idea.

  • 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-27T06:57:27+00:00Added an answer on May 27, 2026 at 6:57 am

    What you looking for is polymorphism.

    There are two “proper” object-oriented ways to do what you want. My personal preference goes to the second one, I’ll tell you why.

    • Inheritance: Have a superclass (maybe abstract) in which your method does all the common stuff (do.something();, do.anotherthing(); and do.somethingelse();. Then this method should call another abstract method. You then define three subclasses of that class, each for one of the different choices (“choice A”, “choice B” and “choice C”). Each of your subclass implement the method in a different way, and does what you need to do.

    • Composition: Instead of having subclasses, have an interface injected into the object that contains method foo. This interfaces then has three different implementations, one for “choice A”, one for “choice B” and one for “choice C”.

    Personally I prefer composition over inheritance, because it usually produces a code that is more readable, less complicated, less buggy and a lot more testable (if you use an interface as suggested, then you can mock it in your unit-tests of the main class). It also ensures separation of concerns.

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

Sidebar

Related Questions

When I have a method like this: public static void foo(String param) throws IOException
If I have a method like this: public void DoSomething(int Count, string[] Lines) {
I have a method that looks like this: public static String escape(String text) {
i have a class that has something like this public class Foo { public
Assume we have a method like this: public IEnumerable<T> FirstMethod() { var entities =
I have a method in a url rewriting module that looks like this public
I have a method that looks like: T[] field; public Method(IList<T> argument) { this.field
I have a C++ method signature that looks like this: static extern void ImageProcessing(
I have an example class defined like below: public class FooBar { void method1(Foo
So I have a class of overloaded methods like this: class Foo { public

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.