设计模式-返回类型不同时使用什么 [英] Design Patterns - what to use when the return type is different

查看:125
本文介绍了设计模式-返回类型不同时使用什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在具有不同类型的搜索(例如网络,图片等)的情况下,您将如何构建此解决方案. 因此实际上输入的内容是相同的,但结果根据选择的搜索类型而有所不同

How would you architect this solution where you have different type of search, like web, image etc. So in effect the input is same but the result are different according to search type selected

我可以考虑使用Strategy n Factory处理输入并选择不同的搜索算法,但是如何处理返回类型?

I can think of Strategy n Factory to handle input and select different search algorithm but how to handle return type?

预先感谢

推荐答案

您可以使用BaseSearchResult类,也可以使用结果interfaced,以便不同类型的搜索类可以返回相同类型的结果.

You can either have a BaseSearchResult kind of class or have the result interfaced, so that different types of search classes can return same type of result.

您也可以使用泛型,如下所示:

you can also go with generics, something like this:

class SearchByType1<T>
{
   public T ExecuteSearch()
   {
   }
}

通过基类,我的意思是,如果您可以对所有类型的搜索结果具有一些共同的和必不可少的属性,则可以创建一个基类并通过执行搜索来返回该基类,这与接口类似.

By base class I mean if you could have some common and essential properties of all kinds of search result you can create a baseclass and return that by executing searches, similar idea with interfaces.

这篇关于设计模式-返回类型不同时使用什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆