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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:10:49+00:00 2026-05-31T08:10:49+00:00

I think the following might be a good fit for the Strategy pattern but

  • 0

I think the following might be a good fit for the Strategy pattern but I’m not sure if that’s correct and if so, how to properly implement.

I have a method that will accept a Category and a Sort. Currently Category and Sort are enums, so something like this:

public enum Category{
  Hot,
  New
}

public enum Sort{
  New,
  Rising,      
  ThisWeek
}

public String getUrl(Category category, Sort sort){
   validateCategorySortCombo(category, sort);
}

Notice the validateCategorySortCombo(Category, Sort). First I must validate the combination
of Category + Sort as some are invalid. For example:

if(Category == "Hot" && Sort == "Rising"){
    ThrowInvalidCategorySortCombo("Can't use hot category with rising sort")
}  

There will be several of these checks to ensure the combinations are valid. This seems like a code smell because as other categories and sorts are introduced, I would have to open this class up and modify it which breaks the open/closed principle.

The second code smell comes when I have to inspect both Category and Sort to construct my Url which will be passed back. Currently I’m using a series of switch statements:

String url;
switch(category){
  case Hot:
     url += "www.site.com/hot/";
  case New:
     url += "www.site.com/new/";
}

switch(sort){
  case New:
    url += "?sort=rising";
  case ThisYear:
    url += "?sort=thisyear";
}

Again, as new categories and sorts are introduced, I would have to open this class and update the switch statements.

Couple of questions:

  1. What design pattern would be used to solve my problem?
  2. How would I go about implementing the design pattern?
  • 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-31T08:10:50+00:00Added an answer on May 31, 2026 at 8:10 am

    First, I would suggest that you leave your mind open to multiple design patterns until you’re writing some code. Allow your code to tell you which pattern would work best. If you pick one before hand, you could end up trying to ‘force’ your code to use a pattern that may not have been the best option.

    Having that said, a couple of patterns that may end up being useful in your situation would be Strategy and Chain of Responsibility.

    Moving on…

    I think you need to take a step back and come up with what your design goals are based on how you expect the system to change over time. For example, it sounds like you’re definitely expecting to be adding new Categories and Sorts. Based on that, two goals your design may have would be:

    1. minimize modifications to validation code when adding a new Sort/Category
    2. minimize modifications to url building code when adding a new Sort/Category

    From those goals, come up with a generic approach. For example, my thoughts are…

    1. If I separate each validation out into its own class, I won’t need to modify existing validation algorithms when adding a new Category/Sort. All I’d have to do is add new validation algorithm or possibly delete an existing one if it no longer applies. (This could be overkill, but I don’t know how complicated your validation algorithms are or will get)
    2. If each Category/Sort knows how to provide its own piece of the url, each new category or sort should have no impact on the ability to get the url representations of existing categories or sorts.

    Now you can start thinking more about implementation (where you have many options). For example, you could expand your enums so that each value already knows its url component like so:

    public enum Sort{
      New("?sort=new"),
      Rising("?sort=rising"),      
      ThisWeek("?sort=thisweek");
    
    
      private String urlComponent;
      public Sort(String urlComponent) {this.urlComponent = urlComponent;)
      public getUrlComponent() {return this.urlComponent;}
    }
    

    Going this route, you could always call mySort.getUrlComponent(), which wouldn’t need to change when adding/removing Sort enum values. This would address your second concern specifically.

    I’m not going to be able to give you the best implementation or set of design patterns because I don’t know everything you do about the system you’re building, but I hope my thoughts here will help.

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

Sidebar

Related Questions

The following C++ code i think is correct, but produce some warnings when compiled
Good morning SO, I have a class that contains the following static array: public
I think I might need to do something like the following pseudo-code in my
Given I have two File objects I can think of the following implementation: public
The following script is returning false, when I think that it should be returning
I have heard several people claiming that you can not scale the JVM heap
I've been following a lot of good tutorials on building rails apps but I
I realize that this might be a duplicate question but this question is very
Does anyone else think that escaping characters in very short strings make them not
I have the following code in one of my Django templates that I want

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.