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:
- Allow the user to inherit from
Movies
and override bestMovie(). Feels like a wrong usage of inheritance. - Create an interface called
MovieSelection
and implementclass IMDBMovieSelection
. The user can then addclass SeasonMovieSelection
.Movies
Ctor will then getMovieSelection
as param which it will use inbestMovie()
- It feels wrong to create objects just for "signaling" a method to my main object - Use delegates/anonymous functions when calling
bestMovie(<anonymous function>)
(similar to what I would use withList.Sort(IComparable)
) - Feels not scalable and hard to maintain, especially if the function is a complicated one. - 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