samedi 25 novembre 2017

Best design method to allow user to change a class method - Delegates/Inheritance/Interface?

I'm writing class Movies that, for simplicity sake, is responsible adding/removing/keeping a set of movies. Movie instance has many params such as length, color, genre, etc. Movies should also allow a user to call Movie bestMovie() which will return 'the best' movie in the set.

Assuming that I already have two ways of choosing a best movie implemented (one based on highest IMDB ranking, the other on Rotten Tomatoes) but I want to allow the user to add new types of decision algorithms (e.g. based on length + season of the year) for example. What is the best design to allow this?

Thoughts that I had:

  1. Allow the user to inherit from Movies and override bestMovie(). Feels like a wrong usage of inheritance.
  2. Create an interface called MovieSelection and implement class IMDBMovieSelection. The user can then add class SeasonMovieSelection. Movies Ctor will then get MovieSelection as param which it will use in bestMovie() - It feels wrong to create objects just for "signaling" a method to my main object
  3. Use delegates/anonymous functions when calling bestMovie(<anonymous function>) (similar to what I would use with List.Sort(IComparable)) - Feels not scalable and hard to maintain, especially if the function is a complicated one.
  4. Provide ability to extend the class using a partial class - not even sure if that's a real option

Would appreciate any insights here. I'm sure I'm not the first one to face this issue, but didn't know what exactly to search for.

Thanks

Aucun commentaire:

Enregistrer un commentaire