Handling user strategy choice over many strategy patterns

I’m building a framework with many parallel implementations of strategy patterns, and I want to know the best way to handle the user’s choice of the set of strategies. E.g. I want to calculate the cost of a car, which is made of parts: engine, wheels etc. Each of these pieces can be costed a different way. E.g. for the wheel part we could find volume of rubber * cost of rubber per kg. Or we could find two similar wheels and take the average. For the car seats we might have another combination of methods entirely. The user needs to choose the set of methods they want to use to calculate the cost of the car e.g. the averaging method for the wheels, the mass method for the seats etc. What is the established way for tracking the state of the combination of chosen strategies? My thoughts so far: It wouldn’t be so user friendly for users/client code to have to know the function names of these methods. Regardless they’d need to input their choices somehow, so one would need a structure of strings. Therefore, a map between the strings and the functions would seem the logical way forward? That would also allow refactoring if functions turned required updating. Is strategy even the correct pattern to use here? Strategy method signatures can’t be assumed to be the same, and here the user is actively choosing the strategy. But when I see examples of client code in strategy patterns there is some logic which sets the strategy to use - can this come from a user?

Apr 19, 2025 - 14:55
 0
Handling user strategy choice over many strategy patterns

I’m building a framework with many parallel implementations of strategy patterns, and I want to know the best way to handle the user’s choice of the set of strategies. E.g.

I want to calculate the cost of a car, which is made of parts: engine, wheels etc.

Each of these pieces can be costed a different way. E.g. for the wheel part we could find volume of rubber * cost of rubber per kg. Or we could find two similar wheels and take the average. For the car seats we might have another combination of methods entirely.

The user needs to choose the set of methods they want to use to calculate the cost of the car e.g. the averaging method for the wheels, the mass method for the seats etc.

What is the established way for tracking the state of the combination of chosen strategies?

My thoughts so far:

  1. It wouldn’t be so user friendly for users/client code to have to know the function names of these methods. Regardless they’d need to input their choices somehow, so one would need a structure of strings. Therefore, a map between the strings and the functions would seem the logical way forward? That would also allow refactoring if functions turned required updating.
  2. Is strategy even the correct pattern to use here? Strategy method signatures can’t be assumed to be the same, and here the user is actively choosing the strategy. But when I see examples of client code in strategy patterns there is some logic which sets the strategy to use - can this come from a user?