namespace FoodFactory.Equipment; using System; using System.Collections.Generic; using FoodFactory.Items; public interface IBeltPortHost { IBeltItemInsertLogic ResolveInsertLogic(IBeltPort port); } public class BeltPortHost : IBeltPortHost { private readonly List<(Func match, Func factory)> _bindings = new(); public BeltPortHost Bind( Func match, Func factory) { _bindings.Add((match, factory)); return this; } private HashSet _ports = []; public IBeltItemInsertLogic? Default; /// /// Resolve the insert logic for a port. If no matching bind exists then the default is returned if supplied. Other wise throws an Exception. /// /// /// /// public IBeltItemInsertLogic ResolveInsertLogic(IBeltPort port) { _ports.Add(port); foreach (var (match, factory) in _bindings) { if (match(port)) { return factory(); } } if (Default is null) { throw new Exception($"{nameof(port)} does not have any binding attached that accepts it."); } return Default; } public IEnumerable GetPorts() => _ports; }