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 {

    @Override
    public Integer countWords(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); }

}

The problem is that in CTypology class i'm required to implement public Integer countWords(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? Thanks.

Aucun commentaire:

Enregistrer un commentaire