Chathuranga Chandrasekara
Chathuranga Chandrasekara

Reputation: 20956

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.

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

Another class, ClassB has the following methods.

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. 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.

Upvotes: 5

Views: 191

Answers (5)

munissor
munissor

Reputation: 3785

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

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838974

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. Perhaps you can use existing interfaces instead of creating a new one.

Upvotes: 3

luckyluke
luckyluke

Reputation: 1563

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

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.

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.

Also I have provided an implementation that uses 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.

Take care Łukasz

Upvotes: 0

Cheng Chen
Cheng Chen

Reputation: 43523

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)  {  }
}

Upvotes: 8

jgauffin
jgauffin

Reputation: 101176

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

You can also read this excellent book about some design principles: http://cdn.cloudfiles.mosso.com/c82752/pablos_solid_ebook.pdf

Upvotes: 0

Related Questions