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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:31:33+00:00 2026-06-11T15:31:33+00:00

Please see the code below . Here based on the String constants , I

  • 0

Please see the code below . Here based on the String constants , I am instantiating different types of component classes . Now there are atleast 15 different types of String constants . So if I follow this pattern there will be 15 different cases and those many if -else blocks . Is there a better way of doing this ? I want to have the flexibility of being able to add and delete cases by doing the minimum possible code change.

public UIComponent initCellEditor(String editorType) {
        UIComponent editControl = null;
        if ("TbComboBoxCellType".equals(editorType)) {
          editControl = new WebListEntryField();
          editControl.setId("ComboBox");
        } else if ("TbStringCellType".equals(editorType)) {
          editControl = new WebInputEntryField();
          editControl.setId("String");
        } else if ("TbDateCellType".equals(editorType)) {
          editControl = new WebDateEntryField();
          editControl.setId("Date");
        } else if ("TbDateTimeCellType".equals(editorType)) {
          editControl = new WebDateTimeEntryField();
          editControl.setId("DateTime");
        } else {
          //default editor is allways a text input
          editControl = new WebInputEntryField();
          editControl.setId("Input");
        }
        return editControl;
      }

P.S: We are using JDK 6 . So can’t use switch on String feature.

  • 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-11T15:31:35+00:00Added an answer on June 11, 2026 at 3:31 pm

    you could transform those string constants to Enums, and add a builder method on the enum, for example

    public enum CellType {
          TbComboBox {
             @Override
             public UIComponent newComponent() {
                return new xxx();
             }
          },
          TbDate {
             @Override
             public UIComponent newComponent() {
                return new xxx();
             }
          }
    
          public abstract UIComponent newComponent();
    }
    

    The beauty of this approach is that it replaces IFs with polymorphism (which in my opnion is very OO).


    Sorry, I just realised that you have a default type (the last else in your code), so you might need to add an if somewhere :(.

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

Sidebar

Related Questions

Please see my code below. ifstream myLibFile (libs//%s , line); // Compile failed here
Noob question here. Please see code below. Where can I read more on this?
Delphi XE. Windows 7. There is a function (please see a code below) or
I have a function(please see code below) which reads some data from the web.
Please see the code below: #include <iostream> #include <stdlib.h> #include <time.h> using namespace std;
I've a problem with CakePHP __() function. Please see the code below : $options
I am using vb.net code. I have grid view, please see the code below:
Please see below code: thrust::device_vector<int>::iterator whereToBegin = copyListOfNgramCounteachdoc.begin(); end = thrust::unique_by_key(end.first, end.first + numUniqueNgrams,end.second);
Please see the code snippet below : #include <iostream> using namespace std; int main()
Please see my pseudo-code below. The code comments should explain my problem. I'm new

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.