0

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

Monolith
  • 1,067
  • 1
  • 13
  • 29
Pranay
  • 21
  • 3

3 Answers3

0

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

Vladooha
  • 3
  • 3
  • Module1 and Module2 have different methods. Creating a single interface is not a good idea I guess. – Pranay Apr 02 '19 at 12:45
0

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;
            ...
        }
    }
}
Vladooha
  • 3
  • 3
0

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 {

}
Ludov Dmitrii
  • 425
  • 5
  • 9
  • Module1 and Module2 have different methods. So, its better to create separate interface for each Module from all the packages(If it is that so, I will have to create 9 interfaces). – Pranay Apr 03 '19 at 07:34
  • Also, the object produce1 will be initialized dynamically. In this case, how do I declare the reference variable? Thanks – Pranay Apr 03 '19 at 07:36
  • updated my example, you need to change parameter of method `produce(Class> clz)` and you can do whatever you want in runtime. – Ludov Dmitrii Apr 03 '19 at 13:38