mercredi 18 mars 2020

Java inherited method

I'm really confused about how implement the following situation. I want to do this:

  1. Integer getScore(String string):

    • ATypology
    • BTypology
  2. boolean isComplete(POJO pojo):

    • ATypology
    • BTypology
    • CTypology

Consider this:

public class ATypology extends TypologyScore {

    @Override
    public Integer getScoreByWords(Integer integer) { return 0; }

    @Override
    public boolean execute(POJO pojo) { /* */ }

}
public class BTypology extends TypologyScore {

    @Override
    public Integer getScoreByWords(Integer integer) { return 1; }

    @Override
    public boolean execute(POJO pojo) { /* */ }

}
public class CTypology extends TypologyOperation {

    // i don't want it
    @Override
    public Integer getScore(String string) { return 0; }

    @Override
    public boolean execute(POJO pojo) { /* */ }

}
public abstract class TypologyScore extends TypologyOperation {

    public Integer getScore(String string) {
        /* ... */
        return this.getScoreByWords(integer);
    }

    // template method
    public abstract Integer getScoreByWords(Integer integer);

}
public abstract class TypologyOperation {

    public abstract Integer getScore(String string);

    public boolean isComplete(POJO pojo) { return this.execute(pojo) && /* ... */; }

    // template method
    public abstract boolean execute(POJO pojo);

}
public class POJO { 

    public Integer getScore() { return typologyOperation.getScore(string); }

    public boolean isComplete() { return typologyOperation.isComplete(this); }

}

Basically i have a serie of conditions that depend on the type of object, 1 common in TypologyScore that let subclasses override (ATypology, BTypology) and other common with a global condition in TypologyOperation that let subclasses override (ATypology, BTypology, CTypology)

But the problem is that in CTypology class i'm required to implement public Integer getScore(String string) but i don't want it because it will never use it. Is there any way to solve this, maybe a design pattern like Visitor pattern?

If so, what would the structure be like?

PS: i rephrase the question since i had some misprints in the previous post. Thanks.

Aucun commentaire:

Enregistrer un commentaire