jeudi 11 mai 2017

Java design: Method Implementation

This is an extension of my previous question here. If I were to implement a method called getGasMileage for a Car POJO, where's the best place to keep that method. I see 4 design strategies:

  1. Put it in a Utility class
  2. Make it a Function
  3. Using the strategy pattern, create a Strategy interface and the implementation will be called CarStrategy, pass the strategy object into Car through the constructor and have a getMileage method inside of the Car class that calls the CarStrategy object method
  4. Create a simple interface with a method like double getMileage(Car car)

What would be the tradeoffs of these approaches?

Aucun commentaire:

Enregistrer un commentaire