Reputation: 36905
There is a class, which has two methods. (Full source can be found here)
public string SearchAnime(string searchTerm)
{
// returns string
}
public AnimeSearchResponse SearchAnimeDeserialized(string searchTerm)
{
// returns an AnimeSearchResponse type object instance
}
I am trying to create Asynchronous versions of above two methods.
That would mean methods like SearchAnimeAsync
and SearchAnimeDeserializedAsync
need to be created.
There is a plan to add more SearchAnime*
methods that return XML or JSON data.
As I add more SearchAnime*
methods, the number of methods will increase two folds due to Async counterparts.
(SearchAnimeXml & SearchAnimeXmlAsync and SearchAnimeJson & SearchAnimeJsonAsync)
It gets worse because the class also contains SearchManga
and SearchMangaDeserialized
, for which I will create Async versions.
Are there any solutions or design patterns that'd ease the explosion of methods?
Upvotes: 0
Views: 91
Reputation: 6463
One solution that comes to my mind is to create a Searcher
abstract base class (with methods Search
and SearchAsync
) and a ISearchResponse
interface (specifying behavior common to all responses). Then each of the Searcher
-derived classes would have only two exposed methods.
public interface ISearchResponse
{
void Foo();
}
public class AnimeSearchResponse : ISearchResponse
{
public void Foo() {}
}
public abstract class Searcher
{
abstract ISearchResponse Search();
abstract ISearchResponse SearchAsync();
}
public class AnimeSearcher : Searcher
{
public override ISearchResponse Search(string searchTerm)
{
return new AnimeSearchResponse(searchTerm);
}
public override ISearchResponse SearchAsync(string searchTerm)
{
return new AnimeSearchResponse(searchTerm);
}
}
You will end up with a lot of classes instead of a lot of methods, but the advantage is that the derived types will be more specialized and can be substituted easily by one another if needed. Somewhere in the application you can choose which concrete type of Searcher
you want to use, using for example the Factory method design pattern.
Upvotes: 3