views:

66

answers:

2

Hi,

I need some advise regarding approach in Java, as the delegates are bit different than .NET one. I wold like to create some interfaces that has the same name and same method name but only thing that differ them is number of parameters. Something like Actions in .NET

Some code samples.

What works for now is the abstract class implemented like this.

public abstract class AbstractValidators {

   public <T1> boolean isValid(T1 t1) {
      return false;
   }

   public <T1,T2> boolean isValid(T1 t1, T2 t2) {
      return false;
   }

  //And so one

} 

Then in some class we can do something like this.

public class SomeClass {

 AbstractValidators validateStrVsInter = new AbstractValidators() {

   public <String,Integer> boolean isValid(String t1, Integer t2) { //Don't be fool by colors the String and Integer are only names of generic parameters
         return true;
   }

 };


 public void doStaff() {

   this.validateStrVsInter.<String,Integer>isValid("String", 100); // return true;

 }

}

That work but is not nice solution IMHO what i need is something like various interafaces that could be assigned to one..

 public interface IValidator<T1> {
      public boolean isValid(T1 t);
    }

 public interface IValidator<T1,T2> {
   public boolean isValid(T1 t, T2 t2);
 }

Some ideas ?

EDIT:

What the goal is ?

Very simple to have possibility to change definition

isValid(String t1, Integer t2) { }

into

isValid(String t1, Integer t2, Double t3) { }

and in invocation

   validator.isValid("1",2);

to

  validator.isValid("1",2, 3.0);

Without changing class import etc.

A: 

You are aware that you can create an interface implementing multiple other interfaces essentially joining them?

Thorbjørn Ravn Andersen
Yes. But then I'm forced to implement every method, than specific one. I would like to pick implement one and perform operation on it.
Vash
Then do as in Swing and make an adapter with empty implementations for every method. You then just override the one you need.
Thorbjørn Ravn Andersen
A: 

Use the packagename in the implementation of the interface.

public class interfaces implements package.a.IValidator, package.b.IValidator
Mark Baijens