jeudi 6 février 2020

mitigate params in polymorphism as child requires diffrent params

I have one interface, implemented in 4 classes.There are some overrided method that do almost same work in every child class. But problem is, there are some params in some methods that are mandatory for some children and unused for some children, It overlaps each other. I have to override method in all children that do not use that param and pass null too in some cases. I want to mitigate those params in such case that no null is passed, all useable params should be passed to overrided method. My code is so complex, I tried to put example simple

public interface ParentInterface {
    void func1(String f1p1, String f1p2);

    void func2(String f2p1, String f2p2, String f2p3);

}
public class Child1 implements ParentInterface {
    @Override
    public void func1(String f1p1, String f1p2) {
        System.out.println(f1p1 + f1p2);
    }

    @Override
    public void func2(String f2p1, String f2p2, String f2p3) {
        System.out.println(f2p1 + f2p3);
    }

}
public class Child2 implements ParentInterface {
    @Override
    public void func1(String f1p1, String f1p2) {
        System.out.println(f1p2);
    }

    @Override
    public void func2(String f2p1, String f2p2, String f2p3) {
        System.out.println(f2p1 + f2p2 + f2p3);
    }
}   
 public static void main(String[] argh) {
        ParentInterface pi1=new Child1();
        pi1.func1("c1f1p1 ","c1f1p2 ");
        pi1.func2("c1f2p1 ",null , "c1f2p3");
        ParentInterface pi2=new Child2();
        pi2.func1(null,"c2f1p2 ");
        pi2.func2("c2f2p1 ", "c2f2p2 ", "c2f2p3");
}

Is there any design patttern for this so that mitigation of null values can be possible?

Aucun commentaire:

Enregistrer un commentaire