gqli
gqli

Reputation: 1045

How to call a method by the order of parameters in OOP?

Suppose I have four classes A, B, C and Container

and I have the following objects and How to achieve the following output by only adding code to the container class?

the first call by "con" object should correspond to the the first parameter.

Your answer will be greatly appreciated !

A  a = new A();    
B  b = new B();    
C  c = new C();

Container  con = new Container(a,b,c);

con.calling();
con.calling();
con.calling();

the expected output is :

calling from A
calling from B
calling from C

public class A {    
    public void callA() {
        System.out.println("calling from A");
    }
}

public class B {        
    public void callB() {
        System.out.println("calling from B");
    }
}

public class C {
    public void callC() {
        System.out.println("calling from C");
    }
}

public class Container {        

    public Container(A a, B b, C c) {

    }

    public void calling(){

    }    
}

Upvotes: 1

Views: 67

Answers (2)

melli-182
melli-182

Reputation: 1234

I would add a variable to take into account which time is the calling method executed, this could be the Container class:

class Container {

    int times = 0;

    A objectA;
    B objectB;
    C objectC;


    public Container(A a, B b, C c) {
        this.objectA = a;
        this.objectB = b;
        this.objectC = c;
    }

    public void Calling(){
        if(this.times==0){
            a.callA();
        }
        if(this.times==1){
            b.callB();
        }
        if(this.times==2){
            c.callC();
        }
        this.times++;
    }
}

Upvotes: 1

Andremoniy
Andremoniy

Reputation: 34900

Looks very similar to Iterator pattern/interface. Here instead of calling() method it will be next().

I can sketch it for you, something like:

import java.util.Arrays;
import java.util.Iterator;

public class Container implements Iterator {

    private Iterator<?> iter;

    public Container(Object ...values) {
        this.iter = Arrays.asList(values).iterator();
    }

    @Override
    public boolean hasNext() {
        return iter.hasNext();
    }

    @Override
    public Object next() {
        return iter.next();
    }
}

UPDATE After some scrutiny consideration on you question, I've decided to expand my answer. Generally decision above with Iterator will give you only rough idea. You probably want more specific decision, and here it is with java-8 method references:

1) Container with Runnable instances:

import java.util.Arrays;
import java.util.Iterator;

public class Container implements Iterator {

    private Iterator<Runnable> iter;

    public Container(Runnable ...values) {
        this.iter = Arrays.asList(values).iterator();
    }

    @Override
    public boolean hasNext() {
        return iter.hasNext();
    }

    @Override
    public Object next() {
        Runnable next = iter.next();
        next.run();
        return next;
    }
}

And sample code to test this stuff:

Container container = new Container(new A()::callA, new B()::callB, new C()::callC);

container.next();
container.next();
container.next();

In this case it will not only return you next instance, but also invoke the desired method.

Upvotes: 4

Related Questions