I'm working on a model that do some stuff with a bunch of different Vehicles. Every Vehicle is supposed to do stuff, but each Vehicle type does a different stuff. So I implemented it this way, using .NET Framework:
abstract class Vehicle
{
abstract void DoStuff()
}
class Car : Vehicle
{
override void DoStuff()
{
//Do some Car stuff here
}
}
class Motorcycle : Vehicle
{
override void DoStuff()
{
//Do some Motorcycle stuff here
}
}
class Model
{
RunModel(Vehicle[] vehicleCollection)
{
foreach(Vehicle currentVehicle in vehicleCollection)
{
currentVehicle.DoStuff()
}
}
}
This is the core funcionallity of my program and it's working as expected. Now I'm supposed to output reports based on the stuff each Vehicle has done. Each type of Vehicle is supposed to ouput a different kind of Report, so I made a similar solution for it:
abstract class Vehicle
{
abstract void DoStuff();
abstract Report GetReport();
}
class Car : Vehicle
{
override Report GetReport()
{
return new CarReport(this);
}
}
class Motorcycle : Vehicle
{
override Report GetReport()
{
return new MotorcycleReport(this);
}
}
abstract class Report
{
int Foo {get; set;}
Report (Vehicle _vehicle)
{
Foo = _vehicle.CommonProperty;
}
}
class CarReport : Report
{
string Bar {get; set;}
CarReport(Car _car) : base(_car)
{
Bar = _car.CarPropoerty;
}
}
class MotorcycleReport : Report
{
bool Baz {get; set;}
MotorcycleReport(Motorcycle _cycle) : base(_cycle)
{
Baz= _cycle.MotorcyclePropoerty;
}
}
class Model
{
RunModel(Vehicle[] vehicleCollection)
{
foreach(Vehicle currentVehicle in vehicleCollection)
{
currentVehicle.DoStuff()
currentVehicle.GetReport()
}
}
}
This is working fine too, but the problem is that Car and Motorcycle now depends on CarReport and MotorcycleReport. Since this is non-core functionallity to my program and the Report structure may change a lot in future versions, I'd like to implement it in a way that the Reports depends on the Vehicles, but the Vehicles do not depend on the Reports.
I've tried an external overloaded method that gets a Vehicle and outputs the proper Report Or passing an abstract Report (or interface IReport) to the Vehicle "GetReport" method But since my RunModel method doesn't know what type of Vehicle it is dealing with, I can't find a way to map it to the right Report type.
Is there a way to avoid this two-way dependency?