2

I have 5 different methods like this

public void met1(){}
public void met2(){}
public void met3(){}
public void met4(){}
public void met5(){}

I want to call this method from 1 to 5 is there any convinient way to do this.

I don't want to call one by one or I don't want to put method call inside other method.

How Can I do this??

akash
  • 22,664
  • 11
  • 59
  • 87
  • 1
    It would be possible with reflection, not directly. But why would you want to do that? – Rohit Jain Mar 19 '14 at 18:27
  • 6
    The fact that you want to do this strongly suggests that something about your design is flawed. Why are you calling 5 methods with sequentially numbered names? Maybe we can help you find a better solution to whatever the problem is. – TypeIA Mar 19 '14 at 18:29
  • Reflection or writing them in sequence are your options. Why do you feel the need to call these methods in this manner? What are you getting at with this? – Makoto Mar 19 '14 at 18:36
  • Every method in sequence checks value and peforms action acording to it on object. – akash Mar 19 '14 at 18:38
  • @TAsk So should the next method in sequence work on the modified object from the previous method? – Rohit Jain Mar 19 '14 at 18:44

5 Answers5

4

I believe you could do it with reflection with something like:

YourClass classInstance = new YourClass();

for (int i = 1; i < 6; i++) {
    Method yourMethod = YourClass.class.getMethod("met" + i);

    method.invoke(instance);
} 

Haven't tested it out, so no guarantees.

Igor
  • 33,276
  • 14
  • 79
  • 112
3

You can use the Execute Around Method pattern.

public class Resource {
    private Resource(){}
    public void opc1(){
        System.out.println("Opc1");
        // use can use cascade pattern( return this)
    }

    public void opc2(){
        System.out.println("Opc2");
        // use can use cascade pattern( return this)
    }

    public void opc3(){
        System.out.println("Opc3");
        // use can use cascade pattern( return this)
    }
    public void closeResource(){
        System.out.println("Release resource");
    }
    public static void use(Consumer<Resource> consumer){
        Resource resource =new Resource();
        consumer.accept(resource);
        resource.closeResource(); // force to execute closeResource method
    }

    public static void main(String[] args) {
        Resource.use(resource -> {
            resource.opc1();
            resource.opc3();
            resource.opc2();
        });
    }
}

More info at https://agiledeveloper.com/

srr7
  • 151
  • 1
  • 11
2

Have you looked into fluent design patterns? http://www.javacodegeeks.com/2013/01/fluent-object-creation.html

Example would be something like this:

myObject.met1().met2().met3().met4().met5().result();
bnem
  • 95
  • 1
  • 7
1

You could do this with reflection as other answers have previously mentioned. Reflection is generally avoided if possible.

In reality the most common design pattern to address your concern would be Chain of Responsibility.
http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern

0

The only way to call methods by name is through reflection. See this article for how to do this:

How do I invoke a Java method when given the method name as a string?

Something like this should work:

 for (int i = 1; i < 6; i++){
      java.lang.reflect.Method method; 
      try {
           method = this.getClass().getMethod("met" + i);
           method.invoke(this);
      } catch (SecurityException e) {
           // ...
      } catch (NoSuchMethodException e) {
           // ...
      }
 }
Community
  • 1
  • 1
Zeki
  • 5,107
  • 1
  • 20
  • 27