Can someone help me with the code below?

I have 2 classes Controller1 and Controller2 implementing the interface Controllers as below:

package com.controllers
public interface Controllers{
    public void method1(*****);
}
------------------------------
package com.controllers
public class Controller1{
    public void method1(com.model1.Module1 module);
}
------------------------------
package com.controllers
public class Controller2{
    public void method1(com.model2.Module1 module);
}
------------------------------

I have 2 packages which have the same classes (both classes have same methods as well) as below

package com.model1
public class Module1{

}
------------------------
package com.model1
public class Module2{

}
-----------------------
package com.model2
public class Module1{

}
------------------------
package com.model2
public class Module2{

}

I am using a Factory class to get the instance of either Controller1 or Controller2 at the runtime, so the argument types for method1 in the inherited classes will vary. But I am unable to so, as a method signature cannot be overridden.

Can someone suggest me with an alternative? Thanks...

3 Answers

0
Community On

I have 2 packages which have the same classes(both classes have same methods as well)

Why you didn't created common parent interface for Module1, Module2 then?

It will be looks like

package com.model1
public class Module1 implements Model {

}
------------------------
package com.model1
public class Module2 implements Model{

}
-----------------------
package com.model2
public class Module1 implements Model{

}
------------------------
package com.model2
public class Module2 implements Model{

}

And

package com.controllers
public interface Controllers{
    public void method1(Model model);
}

But may be I missed something in your question

0
Community On

Anyway, if you don't want to use Module1 or Module2 methods you can create an empty interface with name Method and that's will be a solution. If you want use their methods you can do the same and add something like that (read about reflection in Java)

package com.controllers
public interface Controllers{
    public void method1(Model model) {
        if (model instanceof com.model1.Module1) {
            com.model1.Module1 module = (com.model1.Module1) model;
            ...
        } else if (model instanceof com.model2.Module1) {
            com.model2.Module1 module = (com.model2.Module1) model;
            ...
        }
    }
}
0
Ludov Dmitrii On

Why don't you put some generics in a use?

like this:

public class Example {

    public static void main(String[] args) {

        Object module = new Random().nextBoolean()? new Module2(): new Module1();
        Factory factory = new Factory();
        Controllers produce1 = factory.produce(module.getClass());
        produce1.method1(module);

    }
}

class Factory {

    Controllers produce(Class<?> clz) {
        if (Module1.class.equals(clz)) return new Controller1();
        if (Module2.class.equals(clz)) return new Controller2();
        throw new IllegalArgumentException();
    }

}

interface Controllers<T> {
    void method1(T t);
}

class Controller1 implements Controllers<Module1> {

    public void method1(Module1 integer) {
        System.out.println("in 1!");
    }
}

class Controller2 implements Controllers<Module2> {

    public void method1(Module2 module) {
        System.out.println("in 2!");
    }
}


class Module1 {

}

class Module2 {

}