From ee5c20bc8b6af9bd830fd98d56f8258f2e63288b Mon Sep 17 00:00:00 2001 From: AJ Date: Tue, 18 Nov 2025 17:43:22 -0500 Subject: [PATCH] Add factory pattern for engine creation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace hard-coded engine instantiation with factory pattern to enable dependency injection and improve testability. Changes: - IEngineFactory: Interface for creating engines - EngineFactory: Default implementation using AdvancedFitEngine - MultiBinEngine: Now accepts IEngineFactory via constructor This eliminates the hard-coded dependency on AdvancedFitEngine and allows for easy swapping of engine implementations through configuration or dependency injection. Benefits: - Dependency inversion principle satisfied - Engines can be mocked in tests - Engine selection can be configured externally - Single responsibility for engine creation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- SawCut/Nesting/EngineFactory.cs | 19 +++++++++++++++++++ SawCut/Nesting/IEngineFactory.cs | 18 ++++++++++++++++++ SawCut/Nesting/MultiBinEngine.cs | 18 +++++++++++++----- 3 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 SawCut/Nesting/EngineFactory.cs create mode 100644 SawCut/Nesting/IEngineFactory.cs diff --git a/SawCut/Nesting/EngineFactory.cs b/SawCut/Nesting/EngineFactory.cs new file mode 100644 index 0000000..6e66b2b --- /dev/null +++ b/SawCut/Nesting/EngineFactory.cs @@ -0,0 +1,19 @@ +namespace SawCut.Nesting +{ + /// + /// Default implementation of IEngineFactory that creates AdvancedFitEngine instances. + /// Can be extended to support different engine types based on configuration. + /// + public class EngineFactory : IEngineFactory + { + public IEngine CreateEngine(double stockLength, double spacing, int maxBinCount) + { + return new AdvancedFitEngine + { + StockLength = stockLength, + Spacing = spacing, + MaxBinCount = maxBinCount + }; + } + } +} diff --git a/SawCut/Nesting/IEngineFactory.cs b/SawCut/Nesting/IEngineFactory.cs new file mode 100644 index 0000000..8b0f0ab --- /dev/null +++ b/SawCut/Nesting/IEngineFactory.cs @@ -0,0 +1,18 @@ +namespace SawCut.Nesting +{ + /// + /// Factory interface for creating bin packing engines. + /// Allows for dependency injection and testing without hard-coded engine types. + /// + public interface IEngineFactory + { + /// + /// Creates a configured engine instance for bin packing. + /// + /// The length of stock bins + /// The spacing/kerf between items + /// Maximum number of bins to create + /// A configured IEngine instance + IEngine CreateEngine(double stockLength, double spacing, int maxBinCount); + } +} diff --git a/SawCut/Nesting/MultiBinEngine.cs b/SawCut/Nesting/MultiBinEngine.cs index 836c37e..bec2c34 100644 --- a/SawCut/Nesting/MultiBinEngine.cs +++ b/SawCut/Nesting/MultiBinEngine.cs @@ -6,6 +6,17 @@ namespace SawCut.Nesting { public class MultiBinEngine : IEngine { + private readonly IEngineFactory _engineFactory; + + public MultiBinEngine() : this(new EngineFactory()) + { + } + + public MultiBinEngine(IEngineFactory engineFactory) + { + _engineFactory = engineFactory ?? throw new ArgumentNullException(nameof(engineFactory)); + } + public List Bins { get; set; } public double Spacing { get; set; } @@ -23,11 +34,8 @@ namespace SawCut.Nesting foreach (var bin in bins) { - var e = new AdvancedFitEngine(); - e.MaxBinCount = bin.Quantity; - e.StockLength = bin.Length; - e.Spacing = Spacing; - var r = e.Pack(remainingItems); + var engine = _engineFactory.CreateEngine(bin.Length, Spacing, bin.Quantity); + var r = engine.Pack(remainingItems); result.AddBins(r.Bins); remainingItems = r.ItemsNotUsed.ToList();