Strategy Pattern
# Class implementing Ride Matching Service
class RideMatchingService:
def match_rider(self, rider_location, matching_type):
# Match rider using different hardcoded strategies
if matching_type == "NEAREST":
# Find nearest driver
print(f"Matching rider at {rider_location} with nearest driver.")
elif matching_type == "SURGE_PRIORITY":
# Match based on surge area logic
print(f"Matching rider at {rider_location} based on surge pricing priority.")
elif matching_type == "AIRPORT_QUEUE":
# Use FIFO-based airport queue logic
print(f"Matching rider at {rider_location} from airport queue.")
else:
print("Invalid matching strategy provided.")
# Client Code
def main():
service = RideMatchingService()
# Try different strategies
service.match_rider("Downtown", "NEAREST")
service.match_rider("City Center", "SURGE_PRIORITY")
service.match_rider("Airport Terminal 1", "AIRPORT_QUEUE")
if __name__ == "__main__":
main()
Issue
Explanation
Problem in Old Approach
How Strategy Pattern Solves It
Pros and Cons

Last updated