Strategy Design Pattern LLD

Strategy Design Pattern LLD

·

3 min read

Strategy design pattern is one of the behavioral design pattern.

I am writing this blog because many of us don’t know when and where to implement strategy design pattern in LLD interviews or real-time use cases.


Use Case :

  1. Search for a cab within x radius. (Cab Booking LLD)

  2. Calculate the total fare. (Cab Booking LLD)

  3. Recommend items. (E-Com LLD or question recommendation like Leetcode LLD)

  4. Find a parking slot for a vehicle(Parking lot LLD)

  5. Payment strategy.

  6. Answer scoring strategy(based on some rule)

And many more.


Let’s take two problems and implement them.

Search cab within x radius. (Cab Booking LLD)

Searcing cabs within the radius depends upon multiple strategies like
cab/driver with the highest ratings, nearest cab to the user, cab conditions, or no requirement.

interface CabMatchingStrategy{
  Cab matchCab(List<Cab> allCabsInTheRedius);
}

DefaultCabMatchingStrategy: Find any cab and return cab details to the client.

class DefaultCabMatchingStrategy implements CabMatchingStrategy{
  public Cab matchCab(List<Cab> allCabsInTheRedius){
    // implemetation
  }
}

CabRatingMatchingStrategy: Find a cab with the highest rating.

class CabRatingMatchingStrategy implements CabMatchingStrategy{
  public Cab matchCab(List<Cab> allCabsInTheRedius){
    // implemetation
  }
}

NearestCabMatchingStrategy: Find the nearest cab to the user

class NearestCabMatchingStrategy implements CabMatchingStrategy{
  public Cab matchCab(List<Cab> allCabsInTheRedius){
    // Search nearest cab
  }
}

How do we leverage CabMatchingStrategy at runtime?

public class CabSeachService{
  private final CabMatchingStrategy cabMatchingStrategy;

  public CabSeachService(CabMatchingStrategy cabMatchingStrategy){
    this.cabMatchingStrategy = cabMatchingStrategy;
  }
  // book cab
  public Cab searchCab(Long lat, Long lon, CabType cabType, User user){
    /* find all cab within x radius from lat, long and then apply cab macthing 
       strategy */
    cabMatchingStrategy.matchCab(allCabsInTheRedius);
  }
}

You can change CabMatchingStrategy at runtime.

CabSeachService defaultCabMatchingSearchService = new CabSeachService(new DefaultCabMatchingStrategy());
CabSeachService nearestCabMatchingSearchService = new CabSeachService(new NearestCabMatchingStrategy());
CabSeachService ratingCabMatchingSearchService = new CabSeachService(new CabRatingMatchingStrategy());

2. Recommend Questions once the user submits the answer.

It is similar to Leetcode or any other coding platform, once you submit the answer platform recommends a few questions. Question recommendation can be done with multiple strategies.

Strategies:
1. Recommend similar-level unsolved questions
2. Based on user past solved questions
3. Based on the number of questions solved by the user.

Questions recommended by Leetcode.

public interface ProblemRecommendStrategy {
   List<Problem> recommend(Problem problem, User forUser);
}

DefaultProblemRecommendStrategy: Recommend any x number of questions.

public class DefaultProblemRecommendStrategy implements ProblemRecommendStrategy {
    @Override
    public List<Problem> recommend(Problem problem, User forUser) {
        // TODO
    }
}

ProblemLevelRecommendStrategy: Recommend any x number of questions based on a similar level(Easy, Medium, or Hard).

public class ProblemLevelRecommendStrategy implements ProblemRecommendStrategy {
    @Override
    public List<Problem> recommend(Problem problem, User forUser) {
       // TODO
    }
}

How to use these strategies, It is completely based on your use case, i am taking a sample example once the user submits the answer we have to recommend them with x questions.

class QuestionSubmissionService {
  private final ProblemRecommendStrategy problemRecommendStrategy;

  public QuestionSubmissionService(ProblemRecommendStrategy problemRecommendStrategy){
    this.problemRecommendStrategy = problemRecommendStrategy;
  }

  List<Question> submit(Problem problem, Answer answer, User byUser){
     // TODO answer submission 
     return problemRecommendStrategy.recommend(problem, byUser);
  }
}

You can change CabMatchingStrategy at runtime.

QuestionSubmissionService  defaultProblemRecommendStrategyService = new QuestionSubmissionService(new DefaultProblemRecommendStrategy()); 
QuestionSubmissionService  problemLevelRecommendStrategyService = new QuestionSubmissionService(new ProblemLevelRecommendStrategy());

It’s your time to implement strategy design pattern in low-level/machine coding interviews.

Hope you enjoyed reading! Your support means a lot. Feel free to like and share if you find it valuable. Thanks for your time! 🙏

Did you find this article valuable?

Support MUKUL JHA by becoming a sponsor. Any amount is appreciated!