DEV Community

Discussion on: Casting stinks. Generic classes are worse.

courier10pt profile image
Bob van Hoove • Edited on

Perhaps I'm glossing over things.. correct me if I am:

If I look at this:

public void Print<L, E, I V>(VehicleFactory<L, E, I, V> factory)
  where L : VehicleAssemblyLine<E, I, V>
  where E : Engineer
  where I : VehicleAssemblyLineInstructions
  where V : Vehicle
  Console.WriteLine($"Welcome to {factory.FactoryName}");

This looks like you want to host a variety of L, E, I and V instances. But the thing that really varies is the kind of Sedan.

If you abstract the thing that's similar yet varies, you'll end up with a less elaborate type signature and the class could look roughly like this:

public Factory<ISedan>
    public AssemblyLine<ISedan> assemblyLine; 

    // the lead comes with the team
    public EngineeringTeam<ISedan> engineers; 

    public ISedan Create(Instruction<ISedan> instruction) 
    { /* ... */ }