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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:45:09+00:00 2026-06-01T02:45:09+00:00

I have the following Strategy Pattern implemented: public abstract class RetrievalStrategy { public abstract

  • 0

I have the following Strategy Pattern implemented:

public abstract class RetrievalStrategy {
    public abstract List<MyObject> retrieve();
}

public class LimitRetrievalStrategy extends RetrievalStrategy {
    public int limit;
    public LimitRetrievalStrategy(int limit) {
        this.limit = limit;
    }
    public List<MyObject> retrieve() {
        // fill up the list and return it, limiting to 'limit' results
        return new ArrayList<MyObject>(limit);
    }
}

public class SpeedRetrievalStrategy extends RetrievalStrategy {
    public int speed;
    public SpeedRetrievalStrategy(int speed) {
        this.speed = speed;
    }
    public List<MyObject> retrieve() {
        // do something with the speed and return list again
        return new ArrayList<MyObject>();
    }
}

My client application receives either a speed or a limit (and perhaps other parameters) from the user. I want to create a factory class that will return the proper strategy based on parameters that have a value and parameters that don’t. I don’t want the client the decide which Strategy should be returned. Should the factory createStrategy(params) method then have if statements for each parameter and depending on the different combinations (imagine there are other Strategy classes) return the Concrete Strategy class?
What is the proper way to do this?

  • 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-01T02:45:11+00:00Added an answer on June 1, 2026 at 2:45 am

    No, the return value in the method signature should be the common interface or base class.

    The factory must have the if tests to decide on the concrete type to return, but that’s unavoidable, unless you can craft a Map that returns a concrete instance based on a composite key class that encapsulates parameter combinations.

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

Sidebar

Related Questions

I have the following class: @MappedSuperclass public abstract class MappedModel { @Id @GeneratedValue(strategy =
Suppose I have the following abstract class Strategy { val lib: TestingLibrary = ...
I have written the following code: @Entity @Table(name=person) @Inheritance(strategy=InheritanceType.JOINED) public class Person { private
Following is the structure of my class public abstract class BaseUser { protected List<Perm>
I have the following two entities: @Entity public class SupermarketChain { @Id @GeneratedValue(strategy =
I have following structure of my entities: @MappedSuperclass public abstract class BaseEntity { @Id
I have following classes. class A { public: void fun(); } class B: public
I have the following annotated Hibernate entity classes: @Entity public class Cat { @Column(name
I have the following model classes: @Entity @Table(name = title) public final class Title
I have the following XML file: <?xml version=1.0 encoding=utf-8 ?> <strategies> <strategy name=God Class>

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.