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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:03:27+00:00 2026-05-23T17:03:27+00:00

I should use polymorphism over conditionals but can I use it in below case

  • 0

I should use polymorphism over conditionals but can I use it in below case ?

Below code returns a Screen object depending on contents of jsonObject.

public static Screen getNextScreen(){

        JSONObject jsonObject = RatingsUtils.getCurrentJsonObjectFromServer();

        if(isNextProgramScreen(jsonObject)) {
            ParentRatingsObject parentRatingsObject = JsonBusinessObjectFactory.createParentRatingsObject(jsonObject);
            return new NextProgramScreen(parentRatingsObject);
        } 
        else if(isTimerScreen(jsonObject)) {
            ChildWithParentRatingsObject childWithParentRatingsObject = JsonBusinessObjectFactory.createChildWithParentRatingsObject(jsonObject);
            return new TimerScreen(childWithParentRatingsObject);
        } 
        else if(isNextContestantPreJudgeScreen(jsonObject)) {
            ChildWithParentRatingsObject childWithParentRatingsObject = JsonBusinessObjectFactory.createChildWithParentRatingsObject(jsonObject);
            return new NextContestantPreJudgingScreen(childWithParentRatingsObject);
        } 
        else if(isNextContestantJudgeScreen(jsonObject)) {
            ChildWithParentRatingsObject childWithParentRatingsObject = JsonBusinessObjectFactory.createChildWithParentRatingsObject(jsonObject);
            return new TimerScreen(childWithParentRatingsObject);
        } 
        else {
            return null;
        }
}
  • 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-23T17:03:28+00:00Added an answer on May 23, 2026 at 5:03 pm

    Absolutely. I’ve been taking the polymorphic approach more often myself, and I really like it. Because of Java the language, this will look kind of bloated. We really need lambda expressions to do this properly!

    private static List<ScreenProvider> screenProviders = screenProviders();
    
    public static Screen getNextScreen(JSONObject jsonObject) {
        for (ScreenProvider screenProvider : screenProviders) {
            if (screenProvider.supports(jsonObject)) {
                return screenProvider.getScreen(jsonObject);
            }
        }
        return null;
    }
    
    interface ScreenProvider {
        boolean supports(JSONObject jsonObject);
        Screen getScreen(JSONObject jsonObject);
    }
    
    private static List<ScreenProvider> screenProviders() {
        return Arrays.asList(
                new ScreenProvider() {
                    public boolean supports(JSONObject jsonObject) {
                        return isNextProgramScreen(jsonObject);
                    }
    
                    public Screen getScreen(JSONObject jsonObject) {
                        ParentRatingsObject parentRatingsObject = JsonBusinessObjectFactory.createParentRatingsObject(jsonObject);
                        return new NextProgramScreen(parentRatingsObject);
                    }
                },
                new ScreenProvider() {
                    public boolean supports(JSONObject jsonObject) {
                        return isTimerScreen(jsonObject);
                    }
    
                    public Screen getScreen(JSONObject jsonObject) {
                        ChildWithParentRatingsObject childWithParentRatingsObject = JsonBusinessObjectFactory.createChildWithParentRatingsObject(jsonObject);
                        return new TimerScreen(childWithParentRatingsObject);
                    }
                },
                new ScreenProvider() {
                    public boolean supports(JSONObject jsonObject) {
                        return isNextContestantPreJudgeScreen(jsonObject);
                    }
    
                    public Screen getScreen(JSONObject jsonObject) {
                        ChildWithParentRatingsObject childWithParentRatingsObject = JsonBusinessObjectFactory.createChildWithParentRatingsObject(jsonObject);
                        return new NextContestantPreJudgingScreen(childWithParentRatingsObject);
                    }
                },
                new ScreenProvider() {
                    public boolean supports(JSONObject jsonObject) {
                        return isNextContestantJudgeScreen(jsonObject);
                    }
    
                    public Screen getScreen(JSONObject jsonObject) {
                        ChildWithParentRatingsObject childWithParentRatingsObject = JsonBusinessObjectFactory.createChildWithParentRatingsObject(jsonObject);
                        return new TimerScreen(childWithParentRatingsObject);
                    }
                }
        );
    }
    

    A huge benefit of this approach is the simplicity of your getNextScreen() method. With some thought, it may be possible to make the screenProviders() method more compact–perhaps by adding an abstract class that implements ScreenProvider and pulls out some of the work.

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

Sidebar

Related Questions

I should use JSF 1.1 Mojarra implemetnation in job project. But I need a
I thought that I should use NetworkInterface::getDisplayName. I got some name, but this name
I wonder if I should use signal and slots with my own console object?
I realize that we should use BigDecimal for all monetary values, but what about
Can someone explain to me if I should use bcpow() instead of pow() and
How to write this code in php? What i should use? CURL? fsockopen ?
MSDN says that you should use structs when you need lightweight objects. Are there
What GUI should use to run my JUnit tests, and how exactly do I
What kind of collection I should use to convert NameValue collection to be bindable
I dont know if I should use the httpcontext caching or the Enterprise Library

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.