I have the following simulation data:
interface ISimulationData { string Name { get; } int[] Heights { get; } int[] Temperatures { get; } }
And the following interface to generate:
interface ISimulation { void Generate(); }
Using the Composite template to represent one or more simulations:
interface ISimpleSimulation : ISimulation { int Height { get; } int Temperature { get; } } interface IMultiSimulation : ISimulation { IEnumerable<ISimulation> GetSimulations(); }
And I have the following factory interface:
interface ISimulationFactory { ISimulation CreateSimulation(ISimulationData simulationData); }
Specific implementations of simulation types:
class SimpleSimulation : ISimpleSimulation { public int Height { get; set; } public int Temperature { get; set; } public SimpleSimulation(int height, int temperature) { Height = height; Temperature = temperature; } public void Generate() {
If more than one height is specified, a simulation is created for each height:
class MultiHeightsSimulation : MultiSimulation { private readonly int[] _heights; public MultiHeightsSimulation(int[] heights) { _heights = heights; } public override IEnumerable<ISimulation> GetSimulations() { ISimulationFactory factory = new SimulationFactory(); foreach (int height in _heights) {
Similarly, if more than one temperature is specified, then a simulation is created for each temperature:
class MultiTemperaturesSimulation : MultiSimulation { private readonly int[] _temperatures; public MultiTemperaturesSimulation(int[] temperatures) { _temperatures = temperatures; } public override IEnumerable<ISimulation> GetSimulations() { ISimulationFactory factory = new SimulationFactory(); foreach (int temperature in _temperatures) {
Specific implementation for factory:
class SimulationFactory : ISimulationFactory { public ISimulation CreateSimulation(ISimulationData simulationData) { if (simulationData.Heights.Length > 1) { return new MultiHeightsSimulation(simulationData.Heights); } if (simulationData.Temperatures.Length > 1) { return new MultiTemperaturesSimulation(simulationData.Temperatures); } return new SimpleSimulation(simulationData.Heights[0], simulationData.Temperatures[0]); } }
Now I am confused as to how to proceed as follows:
- Handling a combined multi-story and multi-temperature scenario?
I showed only the height and temperature, but there are many other parameters that behave the same, so I'm really looking for a suitable solution that handles these scenarios with multiple X, with minimal coupling.
ISimulationFactory the only open method that accepts ISimulationData .
Now you can see that MultiHeightsSimulation and MultiTemperaturesSimulation call the factory to create (simple) simulations with a specific height / temperature. There are currently no such methods provided by the factory, and I was wondering if it makes sense to expose these methods in the factory? Wouldn't that bother ISimulationFactory clients who shouldn't know the details of the factory implementation?
Looking forward to your feedback. Thanks!