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 <noreply@anthropic.com>
49 lines
1.3 KiB
C#
49 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
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<MultiBin> Bins { get; set; }
|
|
|
|
public double Spacing { get; set; }
|
|
|
|
public Result Pack(List<BinItem> items)
|
|
{
|
|
var bins = Bins
|
|
.Where(b => b.Length > 0)
|
|
.OrderBy(b => b.Priority)
|
|
.ThenBy(b => b.Length)
|
|
.ToList();
|
|
|
|
var result = new Result();
|
|
var remainingItems = new List<BinItem>(items);
|
|
|
|
foreach (var bin in bins)
|
|
{
|
|
var engine = _engineFactory.CreateEngine(bin.Length, Spacing, bin.Quantity);
|
|
var r = engine.Pack(remainingItems);
|
|
|
|
result.AddBins(r.Bins);
|
|
remainingItems = r.ItemsNotUsed.ToList();
|
|
}
|
|
|
|
result.AddItemsNotUsed(remainingItems);
|
|
|
|
return result;
|
|
}
|
|
}
|
|
} |