简体   繁体   English

我可以用来定义具有相同名称的方法的最佳设计是什么?

[英]What is the best design I can use to define methods with same name?

There is a design problem like this. 有这样的设计问题。

Suppose you have a set of class that implements similar methods but not identical ones. 假设您有一组实现类似方法但不是相同方法的类。

Example : The ClassA has methods like this. 示例:ClassA具有这样的方法。

void Add(string str);
void Delete(string str);
List<string> GetInfo(string name);

Another class, ClassB has the following methods. 另一个类,ClassB有以下方法。

void Add(Dictionary Info);
void Delete(string str);
Dictionary GetInfo(string name);

So the nature of the methods are similar but the return types/ input parameters are different. 因此方法的性质相似,但返回类型/输入参数不同。 If I develop an interface to keep the consistency I can only define Delete operation there. 如果我开发一个接口以保持一致性,我只能在那里定义Delete操作。 Alternatively I can think about a set of independant class without any relationships with each other (Of course no interface implementations) but I don't think it is a good design. 或者,我可以考虑一组独立的类,彼此没有任何关系(当然没有接口实现),但我不认为这是一个好的设计。

  1. What is the approach I can use to implement this? 我可以用什么方法来实现这个?
  2. I am new to generic interfaces. 我是通用接口的新手。 Does it help in this case? 这种情况有帮助吗? If so I am going to learn and implement using them. 如果是这样,我将学习并实施它们。

You can use generic interface here. 您可以在此处使用通用接口。 An example: 一个例子:

interface IModifiable<T>
{
  void Add(T Info);
  void Delete(T item);
  T GetInfo(string name);
}
public class MyClass : IModifiable<List<string>>
{
   public void Add(List<string> list)
   { 
      //do something
   }

   public void Delete(List<string> item)   {  }
   public List<string> GetInfo(string name)  {  }
}

Generics would help you if you can change your design slightly: 如果你可以稍微改变你的设计,泛型会帮助你:

interface IFoo<TKey, TValue>
{
    void Add(TKey name, TValue value);
    void Delete(TKey name);
    IEnumerable<TValue> GetInfo(TKey name);
}

This doesn't quite fit your examples, but very nearly. 这不太适合你的例子,但非常接近。 If you can't make this change then I'd say that your classes aren't similar enough that it makes sense for them to have a common interface. 如果你不能做出这个改变那么我会说你的类不够相似,以至于它们有一个共同的接口是有意义的。

You should also note that this design is very similar to the IDictonary or ILookup interface. 您还应注意,此设计与IDictonary或ILookup接口非常相似。 Perhaps you can use existing interfaces instead of creating a new one. 也许您可以使用现有界面而不是创建新界面。

public interface IInt<T> {
    void Add(T val);
    void Delete(string str);
    T GetInfo(string name);
}

I don't see a problem with those interfaces. 我没有看到这些接口有问题。 But I DO see a problem if you implement both of those two interfaces in the same class. 但是如果你在同一个类中同时实现这两个接口,我会发现一个问题。 You'll break the Single Responsibility Principle by doing so. 你这样做会打破单一责任原则。

Read more here: http://www.objectmentor.com/resources/articles/srp.pdf 在这里阅读更多内容: http//www.objectmentor.com/resources/articles/srp.pdf

You can also read this excellent book about some design principles: http://cdn.cloudfiles.mosso.com/c82752/pablos_solid_ebook.pdf 您还可以阅读这本关于一些设计原则的优秀书籍: http//cdn.cloudfiles.mosso.com/c82752/pablos_solid_ebook.pdf

The problem is quite vaugely defined but from what I understand You have several possibilities first use the Generic Method definition 这个问题很明确,但从我的理解中你有几种可能性首先使用通用方法定义

public void Detele<T>(T toDelete); //optional : where T 

and define it in general interface (or abstract class if it'syour case) 并在通用接口(或抽象类,如果你的情况下)中定义它

Otherwise a very old but still sound technique is method overloading. 否则,一种非常古老但仍然健全的技术是方法重载。 You can define multiple methods with the same name but taking different arguments. 您可以使用相同的名称定义多个方法,但使用不同的参数。 .Net uses this pattern heavilyin classes like StreamReader, ToString etc .Net在StreamReader,ToString等类中使用了这种模式

From the signatures You have provided it looks like You might find a use for that. 从您提供的签名看起来您可能会找到它的用途。

Third option (albeit more difficult to code) is to use the lambda expressions. 第三种选择(尽管更难编码)是使用lambda表达式。

Add<T,P>(Action<T,P> addingAction, T source, P param) ( addingAction(source,param); );
//this is naive p.Add(q) it can be arbitralily more complicated
aObject.Add( (p,q) => p.Add(q), myObj, myParam);

This way You define a generic action of adding which can then be encapsulated in Your objects. 这样您可以定义添加的通用操作,然后可以将其封装在您的对象中。 The signature I provided can easily be changes. 我提供的签名很容易改变。 Also You might not want to execute the action right away, but schedule it for lazy execution, or execute it asynchronously. 此外,您可能不想立即执行操作,而是将其安排为延迟执行,或者异步执行。 The possibilities with lambda expressions are Endless. lambda表达式的可能性是无限的。

Also I have provided an implementation that uses Action delegate. 我还提供了一个使用Action delegate的实现。 You can easily convert this code to use the Expression class, by which You can build Your own delegates during code execution (and cache them after initialization since it is quite a slow process ie reflection and stuff.) I strongly recommend to abuse delegates and expressions when possible. 您可以轻松地将此代码转换为使用Expression类,通过该类可以在代码执行期间构建您自己的委托(并在初始化后缓存它们,因为它是一个非常缓慢的过程,即反射和填充。)我强烈建议滥用委托和表达式如果可能。

Take care Łukasz 小心Łukasz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM