It looks like you are trying to convey general information from thing to inspector . To do this, you need methods:
- Get objects to check.
- Inspect them
So, let's define two interfaces:
One that can be used to scan objects.
interface IInspectable { string Value { get; } void Taste();
and another that can be used to retrieve all the objects from the thing that need to be checked.
interface IThing { IEnumerable<IInspectable> GetStuffForInspection(); }
we can implement our classes
class Candy : IThing, IInspectable { public IEnumerable<IInspectable> GetStuffForInspection() { if (IsWrapped && IsHard) yield return this; } public String Value { ... } public void Taste() { ... } } class Chocolate : Sweet, IThing { public IEnumerable<IInspectable> GetStuffForInspection() { Calories.Where(c => !c.IsGood); } } class Calorie : IInspectable { public String Value { ... } public void Taste() { ... } }
allowing you to delete all if statements
foreach (var thing in thingsToCheck) { foreach (var inspectable in thing.GetStuffForInspection()) { var inspected = inspector.Inspect(inspectable.Value); if (inspected != inspectable.Value) { validationResults.Add(new ...); if (additionalOp) { inspectable.Taste(); } } } }
source share