Skip to main content
Commonmark migration
Source Link

Let's say I have a class hierarchy like this:

enter image description here

Now let's suppose I add later a new interface Mechanic, responsible for repairing a vehicle. Of course someone capable of repairing a car won't fix your broken bus. So you would have the BusMechanic and CarMechanic as implementations. My question would be, how do you create the right kind of mech ?

I see either :

The Vehicle interface could instantiate the right mech

I could add a method to the vehicle interface Mechanic createMechanic() which would create a new Mechanic instance with the right class and parameters

I find that it breaks SRP since that means that a vh has to handle its mech which is kinda nonsense.

 

Also it clutters the interface.

The Vehicle interface could provide the factory

Another possibility would be to add a method MechanicFactory getMechFactory(). This would fix the SRP issue, but I still don't really like it.

The Vehicle interface would still get cluttered

 

The chain of calls could get really long and not really readable, like customer.getVehicleFleet().getVehicleRegistry().getVehiculeForPlate(xxx).getMechanicFactory().createMechanic().getToolManager().getToolFactory().getRequiredTools()

 

The vehicle providing the factory for the mechs is still a non-sense

Using a map/enum

I could also map the classes implementing Vehicle with the right mechanic implementation like :

Map.of(Car.class, CarMechanic.class, 
       Bus.class, BusMechanic.class)

That would mean that someone subclassing a vehicle would also have to find all the relevant locations to map its class to the correct implementations

So far, the 2nd approach seems the better one, albeit far from ideal

Let's say I have a class hierarchy like this:

enter image description here

Now let's suppose I add later a new interface Mechanic, responsible for repairing a vehicle. Of course someone capable of repairing a car won't fix your broken bus. So you would have the BusMechanic and CarMechanic as implementations. My question would be, how do you create the right kind of mech ?

I see either :

The Vehicle interface could instantiate the right mech

I could add a method to the vehicle interface Mechanic createMechanic() which would create a new Mechanic instance with the right class and parameters

I find that it breaks SRP since that means that a vh has to handle its mech which is kinda nonsense.

 

Also it clutters the interface.

The Vehicle interface could provide the factory

Another possibility would be to add a method MechanicFactory getMechFactory(). This would fix the SRP issue, but I still don't really like it.

The Vehicle interface would still get cluttered

 

The chain of calls could get really long and not really readable, like customer.getVehicleFleet().getVehicleRegistry().getVehiculeForPlate(xxx).getMechanicFactory().createMechanic().getToolManager().getToolFactory().getRequiredTools()

 

The vehicle providing the factory for the mechs is still a non-sense

Using a map/enum

I could also map the classes implementing Vehicle with the right mechanic implementation like :

Map.of(Car.class, CarMechanic.class, 
       Bus.class, BusMechanic.class)

That would mean that someone subclassing a vehicle would also have to find all the relevant locations to map its class to the correct implementations

So far, the 2nd approach seems the better one, albeit far from ideal

Let's say I have a class hierarchy like this:

enter image description here

Now let's suppose I add later a new interface Mechanic, responsible for repairing a vehicle. Of course someone capable of repairing a car won't fix your broken bus. So you would have the BusMechanic and CarMechanic as implementations. My question would be, how do you create the right kind of mech ?

I see either :

The Vehicle interface could instantiate the right mech

I could add a method to the vehicle interface Mechanic createMechanic() which would create a new Mechanic instance with the right class and parameters

I find that it breaks SRP since that means that a vh has to handle its mech which is kinda nonsense.

Also it clutters the interface.

The Vehicle interface could provide the factory

Another possibility would be to add a method MechanicFactory getMechFactory(). This would fix the SRP issue, but I still don't really like it.

The Vehicle interface would still get cluttered

The chain of calls could get really long and not really readable, like customer.getVehicleFleet().getVehicleRegistry().getVehiculeForPlate(xxx).getMechanicFactory().createMechanic().getToolManager().getToolFactory().getRequiredTools()

The vehicle providing the factory for the mechs is still a non-sense

Using a map/enum

I could also map the classes implementing Vehicle with the right mechanic implementation like :

Map.of(Car.class, CarMechanic.class, 
       Bus.class, BusMechanic.class)

That would mean that someone subclassing a vehicle would also have to find all the relevant locations to map its class to the correct implementations

So far, the 2nd approach seems the better one, albeit far from ideal

added 1 character in body
Source Link
Vinz243
  • 217
  • 1
  • 9

Let's say I have a class hierarchy like this:

enter image description here

Now let's suppose I add later a new interface Mechanic, responsible for repairing a vehicle. Of course someone capable of repairing a car won't fix your broken bus. So you would have the BusMechanic and CarMechanic as implementations. My question would be, how do you create the right kind of mech ?

I see either :

The Vehicle interface could instantiate the right mech

I could add a method to the vehicle interface Mechanic createMechanic() which would create a new Mechanic instance with the right class and parameters

I find that it breaks SRP since that means that a vh has to handle its mech which is kinda nonsense.

Also it clutters the interface.

The Vehicle interface could provide the factory

Another possibility would be to add a method MechanicFactory getMechFactory(). This would fix the SRP issue, but I still don't really like it.

The Vehicle interface would still get cluttered

The chain of calls could get really long and not really readable, like customer.getVehicleFleet().getVehicleRegistry().getVehiculeForPlate(xxx).getMechanicFactory().createMechanic().getToolManager().getToolFactory().getRequiredTools()

The vehicle providing the factory for the mechs is still a non-sense

Using a map/enum

I could also map the classes implementing Vehicle with the right mechanic implementation like :

Map.of(Car.class, CarMechanic.class, 
       Bus.class, BusMechanic.class)

That would mean that someone subclassing a vehicle would also have to find all the relevant locations to map its class to the correct implementationimplementations

So far, the 2nd approach seems the better one, albeit far from ideal

Let's say I have a class hierarchy like this:

enter image description here

Now let's suppose I add later a new interface Mechanic, responsible for repairing a vehicle. Of course someone capable of repairing a car won't fix your broken bus. So you would have the BusMechanic and CarMechanic as implementations. My question would be, how do you create the right kind of mech ?

I see either :

The Vehicle interface could instantiate the right mech

I could add a method to the vehicle interface Mechanic createMechanic() which would create a new Mechanic instance with the right class and parameters

I find that it breaks SRP since that means that a vh has to handle its mech which is kinda nonsense.

Also it clutters the interface.

The Vehicle interface could provide the factory

Another possibility would be to add a method MechanicFactory getMechFactory(). This would fix the SRP issue, but I still don't really like it.

The Vehicle interface would still get cluttered

The chain of calls could get really long and not really readable, like customer.getVehicleFleet().getVehicleRegistry().getVehiculeForPlate(xxx).getMechanicFactory().createMechanic().getToolManager().getToolFactory().getRequiredTools()

The vehicle providing the factory for the mechs is still a non-sense

Using a map/enum

I could also map the classes implementing Vehicle with the right mechanic implementation like :

Map.of(Car.class, CarMechanic.class, 
       Bus.class, BusMechanic.class)

That would mean that someone subclassing a vehicle would also have to find all the relevant locations to map its class to the correct implementation

So far, the 2nd approach seems the better one, albeit far from ideal

Let's say I have a class hierarchy like this:

enter image description here

Now let's suppose I add later a new interface Mechanic, responsible for repairing a vehicle. Of course someone capable of repairing a car won't fix your broken bus. So you would have the BusMechanic and CarMechanic as implementations. My question would be, how do you create the right kind of mech ?

I see either :

The Vehicle interface could instantiate the right mech

I could add a method to the vehicle interface Mechanic createMechanic() which would create a new Mechanic instance with the right class and parameters

I find that it breaks SRP since that means that a vh has to handle its mech which is kinda nonsense.

Also it clutters the interface.

The Vehicle interface could provide the factory

Another possibility would be to add a method MechanicFactory getMechFactory(). This would fix the SRP issue, but I still don't really like it.

The Vehicle interface would still get cluttered

The chain of calls could get really long and not really readable, like customer.getVehicleFleet().getVehicleRegistry().getVehiculeForPlate(xxx).getMechanicFactory().createMechanic().getToolManager().getToolFactory().getRequiredTools()

The vehicle providing the factory for the mechs is still a non-sense

Using a map/enum

I could also map the classes implementing Vehicle with the right mechanic implementation like :

Map.of(Car.class, CarMechanic.class, 
       Bus.class, BusMechanic.class)

That would mean that someone subclassing a vehicle would also have to find all the relevant locations to map its class to the correct implementations

So far, the 2nd approach seems the better one, albeit far from ideal

added 74 characters in body
Source Link
Vinz243
  • 217
  • 1
  • 9

Let's say I have a class hierarchy like this:

enter image description here

Now let's suppose I add later a new interface Mechanic, responsible for repairing a vehicle. Of course someone capable of repairing a car won't fix your broken bus. So you would have the BusMechanic and CarMechanic as implementations. My question would be, how do you create the right kind of mech ?

I see either :

The Vehicle interface could instantiate the right mech

I could add a method to the vehicle interface Mechanic createMechanic() which would create a new Mechanic instance with the right class and parameters

I find that it breaks SRP since that means that a vh has to handle its mech which is kinda nonsense.

Also it clutters the interface.

The Vehicle interface could provide the factory

Another possibility would be to add a method MechanicFactory getMechFactory(). This would fix the SRP issue, but I still don't really like it.

The Vehicle interface would still get cluttered

The chain of calls could get really long and not really readable, like customer.getVehicleFleet().getVehicleRegistry().getVehiculeForPlate(xxx).getMechanicFactory().createMechanic().getToolManager().getToolFactory().getRequiredTools()

The vehicle providing the factory for the mechs is still a non-sense

Using a map/enum

I could also map the classes implementing Vehicle with the right mechanic implementation like :

Map.of(Car.class, CarMechanic.class, 
       Bus.class, BusMechanic.class)

That would mean that someone subclassing a vehicle would also have to find all the relevant locations to map its class to the correct implementation

So far, the 2nd approach seems the better one, albeit far from ideal

Let's say I have a class hierarchy like this:

enter image description here

Now let's suppose I add later a new interface Mechanic, responsible for repairing a vehicle. Of course someone capable of repairing a car won't fix your broken bus. So you would have the BusMechanic and CarMechanic as implementations. My question would be, how do you create the right kind of mech ?

I see either :

The Vehicle interface could instantiate the right mech

I could add a method to the vehicle interface Mechanic createMechanic() which would create a new Mechanic instance with the right class and parameters

I find that it breaks SRP since that means that a vh has to handle its mech which is kinda nonsense.

Also it clutters the interface.

The Vehicle interface could provide the factory

Another possibility would be to add a method MechanicFactory getMechFactory(). This would fix the SRP issue, but I still don't really like it.

The Vehicle interface would still get cluttered

The chain of calls could get really long and not really readable, like customer.getVehicleFleet().getVehicleRegistry().getVehiculeForPlate(xxx).getMechanicFactory().createMechanic().getToolManager().getToolFactory().getRequiredTools()

Using a map/enum

I could also map the classes implementing Vehicle with the right mechanic implementation like :

Map.of(Car.class, CarMechanic.class, 
       Bus.class, BusMechanic.class)

That would mean that someone subclassing a vehicle would also have to find all the relevant locations to map its class to the correct implementation

So far, the 2nd approach seems the better one, albeit far from ideal

Let's say I have a class hierarchy like this:

enter image description here

Now let's suppose I add later a new interface Mechanic, responsible for repairing a vehicle. Of course someone capable of repairing a car won't fix your broken bus. So you would have the BusMechanic and CarMechanic as implementations. My question would be, how do you create the right kind of mech ?

I see either :

The Vehicle interface could instantiate the right mech

I could add a method to the vehicle interface Mechanic createMechanic() which would create a new Mechanic instance with the right class and parameters

I find that it breaks SRP since that means that a vh has to handle its mech which is kinda nonsense.

Also it clutters the interface.

The Vehicle interface could provide the factory

Another possibility would be to add a method MechanicFactory getMechFactory(). This would fix the SRP issue, but I still don't really like it.

The Vehicle interface would still get cluttered

The chain of calls could get really long and not really readable, like customer.getVehicleFleet().getVehicleRegistry().getVehiculeForPlate(xxx).getMechanicFactory().createMechanic().getToolManager().getToolFactory().getRequiredTools()

The vehicle providing the factory for the mechs is still a non-sense

Using a map/enum

I could also map the classes implementing Vehicle with the right mechanic implementation like :

Map.of(Car.class, CarMechanic.class, 
       Bus.class, BusMechanic.class)

That would mean that someone subclassing a vehicle would also have to find all the relevant locations to map its class to the correct implementation

So far, the 2nd approach seems the better one, albeit far from ideal

Source Link
Vinz243
  • 217
  • 1
  • 9
Loading